AI 데이터 학습을 시킬 때 다차원 배열과 대용량 데이터를 통해서 진행한다.
이 과정에서 Python list는 너무 느리기때문에 C언어 기반 NumPy list를 사용하는 편이다.
📌 NumPy list가 뭔데?
NumPy (Numerical Python)는 수치 계산할 때 사용하는 라이브러리인데 그 안에 ndarray라고 불리는 다차원 배열을 통해 선형대수 계산을 진행한다.
데이터를 참조형태로 가지고 있는 Python list에 비해 ndarray는 연속적인 메모리 블록에 값 형태로 가지고 있어 훨씬 계산이 빠르게 끝낼 수 있다.
- Python list는 runtime에 타입을 확인하고 결정해야하기 때문에 ndarray보다 속도가 느림.
- ndarray는 cpu에서 데이터를 가져오는 cache hit rate가 높음.
Python list는 item들의 type이 서로 다를 수 있지만, ndarray는 item들의 type은 모두 동일해야한다.
어떻게 보면 Python 답지 않는 형태일 수 있지만, 과학이나 공학 계산에서는 타입 일관성이 중요하기때문에 데이터 학습에서는 필요하다.
- NumPy list는 RANK(차원)에 따라 불리는 이름이 있다.
0-rank : scalar
1-rank : vector
2-rank : matrix
3-rank : 3-tensor
n-rank : n-tensor
📌 NumPy list vs Python list 연산 방식
- Python list : a, b
리스트끼리 더하면 결합이 일어난다.
- ndarray : na, nb
리스트끼리 더하면 원소별 덧셈이 수행된다.
ndarray는 작은 배열은 큰 배열에 "broadcasting"을 통해서 덧셈이 수행된다.
Broadcasting — NumPy v2.3 Manual
Broadcasting The term broadcasting describes how NumPy treats arrays with different shapes during arithmetic operations. Subject to certain constraints, the smaller array is “broadcast” across the larger array so that they have compatible shapes. Broad
numpy.org
📌 NumPy list 복사 방식
복사 형태는 3가지로 대입, 얕은 복사, 깊은 복사 형태가 있다.
일반적인 복사(대입) 형태를 ndarray na = [1,2,3,4,5]를 nb로 복사하는 형태는 nb = na 이다.
이 경우는 같은 객체를 참조하고 있는 형태이다.
nb[0] 값을 변경해주니까 같은 객체이므로 na도 같이 변경되게 된다.
얕은 복사는 view()함수를 사용하여 새로운 객체를 만들지만 값 자체는 포인터로 연결되어있는 형태를 말한다.
na와 nb는 같은 객체가 아니지만 nb[0]의 값을 변경하였을 때 na 값도 변경되게 된다.
view()는 ndarray의 shape를 변경하며 배열 연산을 해야하는데 값 자체를 복사할 필요가 없을때 많이 사용한다.
깊은 복사는 copy()함수를 사용하여 새로운 객체와 새로운 값으로 만들어낸다.
nb[0]의 값을 변경했을 때 이번에는 na 값은 변하지 않는다. na와 nb는 같은 객체도 아니다.
Copies and views — NumPy v2.3 Manual
Copies and views When operating on NumPy arrays, it is possible to access the internal data buffer directly using a view without copying data around. This ensures good performance but can also cause unwanted problems if the user is not aware of how this wo
numpy.org
📌 다음에 더 자세히 볼 내용!
- Python list와 tuple의 메모리 관리 방식
Memory Management in Lists and Tuples using Python - GeeksforGeeks
Your All-in-One Learning Portal: GeeksforGeeks is a comprehensive educational platform that empowers learners across domains-spanning computer science and programming, school education, upskilling, commerce, software tools, competitive exams, and more.
www.geeksforgeeks.org
- NumPy에서 array랑 matrix랑 둘 중 뭘 사용해야하나요? 라는 내용에서 array를 사용하세요! 라고 분명하게 말해주고 있다.
The array is thus much more advisable to use. Indeed, we intend to deprecate matrix eventually.
NumPy for MATLAB users — NumPy v2.3 Manual
In NumPy, the basic type is a multidimensional array. Array assignments in NumPy are usually stored as n-dimensional arrays with the minimum type required to hold the objects in sequence, unless you specify the number of dimensions and type. NumPy performs
numpy.org
'AI > Python' 카테고리의 다른 글
[Python] datetime 모듈 (0) | 2025.09.25 |
---|---|
[Python] ascii code <-> string 변환 방법 (0) | 2025.09.10 |
[Python] 입력방법 input(), sys.stdin.readline(), strip() (0) | 2025.09.09 |
[Python] 모듈 만들기 (0) | 2025.09.05 |
[Python] 지수 표기 방법 + 2진수, 8진수, 16진수 변환 (0) | 2025.09.03 |