📖 reshape
test_matrix = [[1,2,3,4],[1,2,5,8]]
np.array(test_matrix).shape
(2, 4)
np.array(test_matrix).reshape(8,)
array([1, 2, 3, 4, 1, 2, 5, 8])
np.array(test_matrix).reshape(8,).shape
(8,)
np.array(test_matrix).reshape(2,4).shape
(2, 4)
np.array(test_matrix).reshape(-1,2).shape
(4, 2)
np.array(test_matrix).reshape(2,2,2)
array([[[1, 2], [3, 4], [[1, 2], [5, 8]]])
np.array(test_matrix).reshape(2,2,2).shape
(2, 2, 2)
📖 flatten
test_matrix = [[[1,2,3,4], [1,2,5,8]], [[1,2,3,4], [1,2,5,8]]]
np.array(test_matrix).flatten()
array([1, 2, 3, 4, 1, 2, 5, 8, 1, 2, 3, 4, 1, 2, 5, 8])
📖 indexing for numpy array
a = np.array([[1, 2, 3], [4.5, 5, 6]], int)
test_example
array([1, 2, 3], [4, 5, 6])
test_example[0][0]
1
test_example[0,0]
1
test_example[0,0] = 12 # Matrix 0,0 에 12 할당
test_example
array([12, 2, 3], [ 4, 5, 6])
test_example[0][0] = 5 # Matrix 0,0 에 5 할당
test_example[0,0]
5
📖 slicing for numpy array
# example 1.
a = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]], int)
a[:,2:] # 전체 Row의 2열 이상
a[1,1:3] # 1 Row의 1열 ~ 2열
a[1:3] # 1 Row ~ 2Row의 전체
# example 2.
test_example = np.array([[1,2,5,8], [1,2,5,8], [1,2,5,8], [1,2,5,8]], int)
test_example[:2,:]
array([[1,2,5,8], [1,2,5,8]])
test_example = np.array([[1,2,3,4,5],[6,7,8,9,10]],int)
test_example[:,2:] # 전체 Row의 2열 이상
array([[3, 4, 5], [8, 9, 10]])
test_example[1,1:3] # 1 Row의 1열 ~ 2열
array([7, 8])
test_example[1:3] # 1 Row ~ 2 Row의 전체
array([[ 6, 7, 8, 9, 10]])
(출처 : https://www.slideshare.net/PyData/introduction-to-numpy)
📖 arange
# arange : List range와 같은 효과, integer로 0부터 29까지 배열 추출
np.arange(30)
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29])
# floating point도 표시 가능함
np.arange(0, 5, 0.5)
arary([ 0. , 0.5, 1. , 1.5, 2. , 2.5, 3. , 3.5, 4. , 4.5])
np.arange(30).reshape(5,6)
array([[ 0, 1, 2, 3, 4, 5], [ 6, 7, 8, 9, 10, 11], [12, 13, 14, 15, 16, 17], [18, 19, 20, 21, 22, 23], [24, 25, 26, 27, 28, 29]])
📖 ones, zeros and empty
np.zeros(shape, dtype, order)
np.ones(shape, dtype, order)
np.empty(shape, dtype, order)
📖 something_like
test_matrix = np.arange(30).reshape(5,6)
np.ones_like(test_matrix)
array([[1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1]])
📖 identity
np.identity(3, dtype=np.int8)
array([[1., 0., 0.], [0., 1., 0.], [0., 0., 1.]], dtype=np.int8)
np.identity(5)
array([[1., 0., 0., 0., 0.], [0., 1., 0., 0., 0.], [0., 0., 1., 0., 0.], [0., 0., 0., 1., 0.], [0., 0., 0., 0., 1.]])
📖 eye
np.eye(3)
array([[1., 0., 0.], [0., 1., 0.], [0., 0., 1.]])
np.eye(3,5,k=2)
array([[0., 0., 1., 0., 0.], [0., 0., 0., 1., 0.], [0., 0., 0., 0., 1.]])
np.eye(N=3, M=5, dtype=np.int8)
array([[1, 0, 0, 0, 0], [0, 1, 0, 0, 0], [0, 0, 1, 0, 0]], dtype=int8)
📖 diag
matrix = np.arange(9).reshape(3,3)
np.diag(matrix)
array([0, 4, 8])
np.diag(matrix, k=1)
array([1, 5])
📖 random sampling
# 균등 분포
np.random.uniform(0,1,10).reshape(2,5)
array([[0.09264564, 0.53338537, 0.38126359, 0.8284562 , 0.35628035], [0.89126266, 0.62572127, 0.94019628, 0.43529235, 0.47640554]])
# 정규 분포
np.random.normal(0,1,10).reshape(2,5)
array([[ 0.28386836, -0.00801268, 1.55591156, -0.22063183, 0.37625121], [ 0.50501499, 0.11751482, -1.28867115, 0.02533356, -0.70049923]])
📖 sum
test_array = np.arange(1,11)
test_array
array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
test_array.sum(dtype=np.float)
55.0
📖 axis
test_array = np.arange(1,13).reshape(3,4)
test_array
array([[ 1, 2, 3, 4], [ 5, 6, 7, 8], [ 9, 10, 11, 12]])
(test_array.sum(axis=1),test_array.sum(axis=0))
(array([10, 26, 42]), array([15, 18, 21, 24]))
third_order_tensor.sum(axis=2)
array([[10, 26, 42], [10, 26, 42], [10, 26, 42]])
third_order_tensor.sum(axis=1)
array([[15, 18, 21, 24], [15, 18, 21, 24], [15, 18, 21, 24]])
third_order_tensor.sum(axis=0)
array([[ 3, 6, 9, 12], [15, 18, 21, 24], [27, 30, 33, 36]])
📖 mean & std
test_array = np.arange(1,13).reshape(3,4)
test_array
array([[ 1, 2, 3, 4], [ 5, 6, 7, 8], [ 9, 10, 11, 12]])
test_array.mean(), test_array.mean(axis=0)
(6.5, array([5., 6., 7., 8.]))
test_array.std(), test_array.std(axis=0)
(3.452052529534663, array([3.26598632, 3.26598632, 3.26598632, 3.26598632]))
📖 mathematical functions
np.exp(test_array), np.sqrt(test_array)
(array([[2.71828183e+00, 7.38905610e+00, 2.00855369e+01, 5.45981500e+01], [1.48413159e+02, 4.03428793e+02, 1.09663316e+03, 2.98095799e+03], [8.10308393e+03, 2.20264658e+04, 5.98741417e+04, 1.62754791e+05]]), array([[1. , 1.41421356, 1.73205081, 2. ], [2.23606798, 2.44948974, 2.64575131, 2.82842712], [3. , 3.16227766, 3.31662479, 3.46410162]]))
📖 concatnate
a = np.array([1, 2, 3])
b = np.array([2, 3, 4])
np.vstack((a,b))
array([[1, 2, 3], [2, 3, 4]])
a = np.array([[1], [2], [3]])
b = np.array([[2], [3], [4]])
np.hstack((a,b))
array([[1, 2], [2, 3], [3, 4]])
a = np.array([[1, 2, 3]])
b = np.array([[2, 3, 4]])
np.concatenate((a,b), axis=0)
array([[1, 2, 3], [2, 3, 4]])
a = np.array([[1, 2],[3, 4]])
b = np.array([[5, 6]])
np.concatenate((a,b.T), axis=1)
array([[1, 2, 5], [3, 4, 6]])
📖 Operations b/t arrays
test_a = np.array([[1,2,3],[4,5,6]], float)
test_a + test_a
array([[ 2., 4., 6.], [ 8., 10., 12.]])
test_a - test_a
array([[0., 0., 0.], [0., 0., 0.]])
test_a * test_a
array([[ 1., 4., 9.], [16., 25., 36.]])
📖 Element-wise operations
matrix_a = np.arange(1,13).reshape(3,4)
matrix_a * matrix_a
array([[ 1, 4, 9, 16], [ 25, 36, 49, 64], [ 81, 100, 121, 144]])
📖 Dot product
test_a = np.arange(1,7).reshape(2,3)
test_b = np.arange(7,13).reshape(3,2)
test_a.dot(test_b)
array([[ 58, 64], [139, 154]])
📖 transpose
test_a.transpose()
array([[1, 4], [2, 5], [3, 6]])
test_a.T
array([[1, 4], [2, 5], [3, 6]])
test_a.T.dot(test_a)
array([[17, 22, 27], [22, 29, 36], [27, 36, 45]])
📖 broadcasting
test_matrix = np.array([[1,2,3],[4,5,6]], float)
scalar = 3
# Matrix - Scalar 덧셈
test_matrix + scalar
array([[4., 5., 6.], [7., 8., 9.]])
# Matrix - Scalar 뺄셈
test_matrix - scalar
array([[-2., -1., 0.], [ 1., 2., 3.]])
# Matrix - Scalar 곱셈
test_matrix * 5
array([[ 5., 10., 15.], [20., 25., 30.]])
# Matrix - Scalar 나눗셈
test_matrix / 5
array([[0.2, 0.4, 0.6], [0.8, 1. , 1.2]])
# Matrix - Scalar 몫
test_matrix // 0.2
array([[ 4., 9., 14.], [19., 24., 29.]])
# Matrix - Scalar 제곱
test_matrix ** 2
array([[ 1., 4., 9.], [16., 25., 36.]])
test_matrix = np.arange(1,13).reshape(4,3)
test_vector = np.arange(10,40,10)
test_matrix + test_vector
array([[11, 22, 33], [14, 25, 36], [17, 28, 39], [20, 31, 42]])
📖 numpy performance #1
def sclar_vector_product(scalar, vector):
result = []
for value in vector:
result.append(scalar * value)
return result
iternation_max = 100000000
vector = list(range(iternation_max))
scalar = 2
%timeit sclar_vector_product(scalar, vector) # for loop을 이용한 성능
%timeit [scalar * value for value in range(iternation_max)] # list comprehension을 이용한 성능
%timeit np.arange(iternation_max) * scalar # numpy를 이용한 성능
📖 numpy performance #2
<이 게시물은 최성철 교수님의 numpy 강의 자료를 참고하여 작성되었습니다.>
본 포스트의 학습 내용은 [부스트캠프 AI Tech 5기] Pre-Course 강의 내용을 바탕으로 작성되었습니다.
부스트캠프 AI Tech 5기 Pre-Course는 일정 기간 동안에만 운영되는 강의이며,
AI 관련 강의를 학습하고자 하시는 분들은 부스트코스 AI 강좌에서 기간 제한 없이 학습하실 수 있습니다.
(https://www.boostcourse.org/)