Numpy
는Vector
,Matrix
등 수치 연산을 수행하는 수치해석 및 통계 관련 작업에 사용되는 파이썬 패키지이다.
List
에 비해 빠르고, 메모리 효율적이다.for
문이나 List comprehension
을 사용하지 않고 배열에 대한 처리가 가능.C
또는 포트란으로 구현 되어있어 빠른 연산이 가능하다.이 글은 네이버 부스트코스 강의 중 최성철교수님의 강의를 바탕으로 학습하며 작성했습니다.
import numpy as np
기본적으로 쓰이는 모듈이다 보니 np
로 호출하는 것이 관례라고 함.
import numpy as np
test_array = np.array(["1", "4", 5, 8], float) # float type array
# 위의 예시처럼 string 타입의 요소가 있어도 float로 자동 형변환 된다.
# test_array = np.array(["1", "4", 5, 8], np.float32)
# 보통 이렇게 크기를 정해서 사용.
python은 원래 실행 시점에 data의 type을 결정하는데, numpy에서는 그것을 허용하지 않음. (Dynamic typing을 지원하지 않는다.)
때문에 하나의 data type만 배열에 넣을 수 있고, C의 array를 사용하여 배열을 생성한다.
numpy array
의dimension
(차원) 구성.
test_array = np.array(["1", "3", 5, 7, 9], float)
print(test_array)
print(test_array.shape)
[1. 3. 5. 7. 9.]
# tuple type return
(5,)
위의 경우는 vector shape
이고,
test_array = np.array([["1", "3", 5, 7, 9]], float)
print(test_array)
print(test_array.shape)
[[1. 3. 5. 7. 9.]]
(1, 5)
이 경우에는 1행 5열의 Matrix shape
이다.
만약 3차원 tensor
의 depth
가 3이라면,
그때의 shape
는 (3, 1, 5)이다.
(dimension
이 높아질수록shape
의 요소가 하나씩 뒤로 밀리는 모습을 보임 (5,) -> (1, 5) -> (3, 1, 5))
즉,
test_array = np.array([[[1, 3, 5, 7, 9], [1, 3, 5, 7, 9], [1, 3, 5, 7, 9], [1, 3, 5, 7, 9]],
[[1, 3, 5, 7, 9], [1, 3, 5, 7, 9], [1, 3, 5, 7, 9], [1, 3, 5, 7, 9]],
[[1, 3, 5, 7, 9], [1, 3, 5, 7, 9], [1, 3, 5, 7, 9], [1, 3, 5, 7, 9]]], float)
print(test_array)
print(test_array.shape)
[[[1. 3. 5. 7. 9.]
[1. 3. 5. 7. 9.]
[1. 3. 5. 7. 9.]
[1. 3. 5. 7. 9.]]
[[1. 3. 5. 7. 9.]
[1. 3. 5. 7. 9.]
[1. 3. 5. 7. 9.]
[1. 3. 5. 7. 9.]]
[[1. 3. 5. 7. 9.]
[1. 3. 5. 7. 9.]
[1. 3. 5. 7. 9.]
[1. 3. 5. 7. 9.]]]
(3, 4, 5)
ndim
은 dimension
의 수,size
는 data
의 수를 반환한다.test_array = np.array([[[1, 3, 5, 7, 9], [1, 3, 5, 7, 9], [1, 3, 5, 7, 9], [1, 3, 5, 7, 9]],
[[1, 3, 5, 7, 9], [1, 3, 5, 7, 9], [1, 3, 5, 7, 9], [1, 3, 5, 7, 9]],
[[1, 3, 5, 7, 9], [1, 3, 5, 7, 9], [1, 3, 5, 7, 9], [1, 3, 5, 7, 9]]], float)
print(test_array.ndim)
print(test_array.size)
3 # number of dimension
60 # number of data
numpy array
의 shape
을 변경한다. (element
의 수는 동일함)before = np.array([[[1, 2, 3, 4], [5, 6, 7, 8]]], np.float32)
print("before: ")
print(before)
after = before.reshape(2, 2, 2)
print("after: ")
print(after)
before:
[[[1. 2. 3. 4.]
[5. 6. 7. 8.]]]
after:
[[[1. 2.]
[3. 4.]]
[[5. 6.]
[7. 8.]]]
reshape
을 할 때 한 axis
의 수를 정확히 모를때 -1
로 하면
size
에 맞춰 알아서 변경 됨.
before = np.array([[[1, 2, 3, 4], [5, 6, 7, 8]]], np.float32)
print("before: ")
print(before)
after = before.reshape(-1, 2)
print("after: ")
print(after)
before:
[[[1. 2. 3. 4.]
[5. 6. 7. 8.]]]
after:
[[1. 2.]
[3. 4.]
[5. 6.]
[7. 8.]]
reshape
으로도 할 수 있음)before = np.array([[[1, 2, 3, 4], [5, 6, 7, 8]]], np.float32)
print("before: ")
print(before)
after = before.flatten()
print("after: ")
print(after)
before:
[[[1. 2. 3. 4.]
[5. 6. 7. 8.]]]
after:
[1. 2. 3. 4. 5. 6. 7. 8.]
Indexing
array[1][2]
를 array[1,2]
식으로도 indexing
가능.Slicing
row
와 col
부분을 나눠서 slicing
이 가능.np_array = np.array([[1, 2, 3, 4], [5, 6, 7, 8]], np.float32)
print(np_array) # 전체 array
print(np_array[:,2:4]) # row: 전체, col: 2~3열
print(np_array[1,1:3]) # row: 1열, col: 1~2열
[[1. 2. 3. 4.]
[5. 6. 7. 8.]]
[[3. 4.]
[7. 8.]]
[6. 7.]
array
의 범위를 지정하여, list
를 생성.# int type 0 ~ 9
>>> np.arange(10)
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
# arange(start, end, step)
>>> np.arange(0, 5, 0.5)
array([0. , 0.5, 1. , 1.5, 2. , 2.5, 3. , 3.5, 4. , 4.5])
# reshape 같이 사용.
>>> np.arange(10).reshape(-1, 5)
array([[0, 1, 2, 3, 4],
[5, 6, 7, 8, 9]])
array
생성.>>> np.zeros(10, np.int8)
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0], dtype=int8)
>>> np.ones(10)
array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])
>>> np.zeros((3, 4))
array([[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.]])
>>> np.ones((3, 4), dtype = np.int8)
array([[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 1, 1, 1]], dtype=int8)
>>> np.identity(3)
array([[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]])
>>> np.identity(n=3, dtype=np.int8)
array([[1, 0, 0],
[0, 1, 0],
[0, 0, 1]], dtype=int8)
>>> np.eye(3, 5)
array([[1., 0., 0., 0., 0.],
[0., 1., 0., 0., 0.],
[0., 0., 1., 0., 0.]])
# k값으로 시작 index 변경 가능.
>>> np.eye(3, 5, k = 2)
array([[0., 0., 1., 0., 0.],
[0., 0., 0., 1., 0.],
[0., 0., 0., 0., 1.]])
>>> np_array = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])
>>> np_array
array([[1, 2, 3, 4],
[5, 6, 7, 8]])
>>> np.diag(np_array)
array([1, 6])
# k값으로 시작 index 변경 가능.
>>> np.diag(np_array, k = 2)
array([3, 8])
>>> np_array = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])
>>> np_array.sum()
36
dimension
축.np_array = np.array([1, 2, 3, 4, 5, 6, 7, 8])
print(np_array)
print(np_array.shape)
np_array = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])
print(np_array)
print(np_array.shape)
np_array = np.array([[[1, 2, 3, 4], [5, 6, 7, 8]],
[[1, 2, 3, 4], [5, 6, 7, 8]],
[[1, 2, 3, 4], [5, 6, 7, 8]]])
print(np_array)
print(np_array.shape)
[1 2 3 4 5 6 7 8]
# axis = 0
(8,)
[[1 2 3 4]
[5 6 7 8]]
# (axis = 0, axis = 1)
(2, 4)
[[[1 2 3 4]
[5 6 7 8]]
[[1 2 3 4]
[5 6 7 8]]
[[1 2 3 4]
[5 6 7 8]]]
# (axis = 0, axis = 1, axis = 2)
(3, 2, 4)
이렇게 가장 늦게 생긴 shape
이 axis = 0
이다.
axis
를 기준으로 연산을 하면,
>>> np_array = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])
>>> np_array
array([[1, 2, 3, 4],
[5, 6, 7, 8]])
# row를 기준으로 연산
>>> np_array.sum(axis = 0)
array([ 6, 8, 10, 12])
# col를 기준으로 연산
>>> np_array.sum(axis = 1)
array([10, 26])
이외에도 지수, 로그, 삼각, 하이퍼볼릭 함수들을 사용할 수 있다.
numpy array
를 합치는 함수.vector
를 축 기준으로 합치는 함수.>>> a = np.array([1, 2, 3, 4])
>>> b = np.array([4, 5, 6, 7])
>>> a
array([1, 2, 3, 4])
>>> b
array([4, 5, 6, 7])
# row
>>> np.vstack((a,b))
array([[1, 2, 3, 4],
[4, 5, 6, 7]])
# col
>>> np.hstack((a,b))
array([1, 2, 3, 4, 4, 5, 6, 7])
axis
값을 기준으로 합치는 함수.>>> a = np.array([[1, 2, 3, 4]])
>>> b = np.array([[4, 5, 6, 7]])
>>> a
array([[1, 2, 3, 4]])
>>> b
array([[4, 5, 6, 7]])
>>> np.concatenate((a,b), axis=0)
array([[1, 2, 3, 4],
[4, 5, 6, 7]])
array
끼리 기본적인 사칙연산을 지원.>>> np_array = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])
# 같은 자리에 있는 원소끼리 연산.
>>> np_array + np_array
array([[ 2, 4, 6, 8],
[10, 12, 14, 16]])
shape
이 다른 경우 연산을 지원.>>> a = np.array([[1, 2, 3], [4, 5, 6]], float)
>>> a
array([[1., 2., 3.],
[4., 5., 6.]])
# matrix + scalar
>>> a + 1
array([[2., 3., 4.],
[5., 6., 7.]])
>>> b = np.array([3, 2, 1], float)
>>> b
array([3., 2., 1.])
# matrix + vector
>>> a + b
array([[4., 4., 4.],
[7., 7., 7.]])
>>> a = np.array([[1, 2, 3], [4, 5, 6]], float)
>>> a
array([[1., 2., 3.],
[4., 5., 6.]])
>>> b = np.array([[1, 2], [1, 1], [1, 1]], float)
>>> b
array([[1., 2.],
[1., 1.],
[1., 1.]])
>>> a.dot(b)
array([[ 6., 7.],
[15., 19.]])
>>> a = np.array([[1, 2, 3], [4, 5, 6]], float)
>>> a
array([[1., 2., 3.],
[4., 5., 6.]])
>>> a.transpose()
array([[1., 4.],
[2., 5.],
[3., 6.]])
>>> a.T
array([[1., 4.],
[2., 5.],
[3., 6.]])
array
의 데이터가 전부(and) 조건에 만족하는지 여부 반환.array
의 데이터가 일부(or) 조건에 만족하는지 여부 반환.>>> a = np.arange(10)
>>> a
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> np.all(a < 10)
True
>>> np.all(a > 3)
False
>>> np.any(a > 3)
True
>>> np.any(a > 10)
False
# 이렇게 사용할 수 있다.
>>> a > 5
array([False, False, False, False, False, False, True, True, True, True])
index
를 반환하므로 유용하게 사용.>>> a = np.arange(10)
>>> a
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> np.where(a > 5, 1, 0)
array([0, 0, 0, 0, 0, 0, 1, 1, 1, 1])
# a > 5 를 만족하는 index를 반환.
>>> np.where(a > 5)
(array([6, 7, 8, 9]),)
max
, min
값의 index
를 반환한다.>>> a = np.array([2, 4, 6, 8, 10, 1, 3, 5, 7, 9], np.int8)
>>> np.argmax(a)
4
>>> np.argmin(a)
5
# axis를 기준으로 비교할 수 있음.
>>> a = np.array([[1, 9, 6, 4], [3, 6, 2, 8]])
>>> a
array([[1, 9, 6, 4],
[3, 6, 2, 8]])
>>> np.argmax(a, axis=0)
array([1, 0, 0, 1])
>>> np.argmax(a, axis=1)
array([1, 3])
True
인 index
의 element
만 추출할 때 사용.>>> a = np.arange(8)
>>> a
array([0, 1, 2, 3, 4, 5, 6, 7])
>>> a > 2
array([False, False, False, True, True, True, True, True])
# index에 True만 넣어줌.
>>> a[a > 2]
array([3, 4, 5, 6, 7])
>>> index = a > 2
>>> a[index]
array([3, 4, 5, 6, 7])
array
자체를 index
로 사용해 element
를 추출할 수 있다.>>> a = np.array([1, 5, 10, 42, 2, 3, 79, 28])
>>> b = np.array([3, 3, 3, 0, 0, 7, 7, 1])
# b array를 a의 index로 사용함.
>>> a[b]
array([42, 42, 42, 1, 1, 28, 28, 5])
참고.
https://ko.wikipedia.org/wiki/NumPy
https://laboputer.github.io/machine-learning/2020/04/25/numpy-quickstart/#item1