NumPy 공식 가이드
https://numpy.org/doc/stable/reference/index.html
pip install numpy==1.23.5
import numpy as np
my_list = [1,2,3,4]
arr_1 = np.array(my_list)
print(arr_1) # output is [1,2,3,4]
type(arr_1) # output is numpy.ndarray
arr_2 = np.array([
[1,2,3],
[2,3,4],
])
print(arr_2) # output is [[1 2 3]
[2 3 4]]
type(arr_2) # output is numpy.ndarray
arr_3 = np.array([
[[1,2,3], [3,4,5], [10,20,30]],
[[4,5,6], [7,9,8], [15,54,78]],
[[1,2,3], [3,4,5], [5121,999,989.0]]
])
print(arr_3) # output is
# [[[1.000e+00 2.000e+00 3.000e+00]
# [3.000e+00 4.000e+00 5.000e+00]
# [1.000e+01 2.000e+01 3.000e+01]]
# [[4.000e+00 5.000e+00 6.000e+00]
# [7.000e+00 9.000e+00 8.000e+00]
# [1.500e+01 5.400e+01 7.800e+01]]
# [[1.000e+00 2.000e+00 3.000e+00]
# [3.000e+00 4.000e+00 5.000e+00]
# [5.121e+03 9.990e+02 9.890e+02]]]
np.ones((shape), type)
# 행3, 열4인 integer type array 생성하기
arr_4 = np.ones((3,4), 'int)
print(arr_4)
np.zeros((shape), type)
# 3 X 5 X 5의 float타입의 array 생성하기
arr_5 = np.zeros((3,5,5), 'float')
print(arr_5)
np.random.random((shape))
# 0이상 1미만의 실수값으로 채운 4 X 5 array 생성
arr_6 = np.random.random((4,5))
print(arr_6)
array변수명.shape
print(arr_1.shape)
print(arr_2.shape)
print(arr_3.shape)
array변수명.dtype
예제
print(arr_1.dtype)
print(arr_3.dtype)
array변수명.reshape(shape)
예제1) 행,열을 명시적으로 입력하여 shape 변경
arr = np.array([
[1,2,3,4,5,6],
[10,20,30,40,50,60],
[100,50,35,48,64,70],
[10,20,30,40,50,60]
])
arr.shape # output is (4,6)
arr_2 = arr.reshape(3,8)
print(arr_2) # output is
# [[ 1 2 3 4 5 6 10 20]
# [ 30 40 50 60 100 50 35 48]
# [ 64 70 10 20 30 40 50 60]]
print(arr_2.shape) # output is (3,8)
예제2) 행,열 중 하나의 값만 넣어서 shape 변경
arr_3 = arr_2.reshape(2,-1)
print(arr_3.shape) # output is (2,12)
arr_4 = arr_2.reshape(-1, 6)
print(arr_4.shape) # output is (4,6)
array변수명[row_number, column_number]
예제
arr = np.array([[ 7, 34, 94, 24, 60],
[56, 34, 87, 59, 66],
[14, 44, 19, 51, 78],
[90, 22, 43, 91, 62],
[84, 78, 22, 8, 62]])
arr[0] # output is array([ 7, 34, 94, 24, 60])
arr[3] # output is array([90, 22, 43, 91, 62])
arr[3,3] # output is 91
arr[3][3] # output is 91
arr[-2][-2] # output is 91
array변수명[row, column]
을 잘라서 리턴한다.arr= np.array([[98, 73, 15, 74, 5],
[16, 26, 75, 79, 30],
[39, 65, 54, 72, 82],
[70, 63, 79, 24, 86],
[91, 13, 28, 87, 52]])
arr[0:3]
# output is array([[98, 73, 15, 74, 5],
# [16, 26, 75, 79, 30],
# [39, 65, 54, 72, 82]])
arr[2:-1]
# output is array([[39, 65, 54, 72, 82],
# [70, 63, 79, 24, 86]])
arr[:, 2:-1]
# output is array([[15, 74],
# [75, 79],
# [54, 72],
# [79, 24],
# [28, 87]])
array변수명[조건]
조건에 따라 인덱싱하면 무조건 1차원 array 형태로 반환된다.
arr= np.array([[98, 73, 15, 74, 5],
[16, 26, 75, 79, 30],
[39, 65, 54, 72, 82],
[70, 63, 79, 24, 86],
[91, 13, 28, 87, 52]])
arr[ arr < 20 ]
# output is array([15, 5, 16, 13])
np.where(조건, 조건이 True일때 값, 조건이 False일때 값)
arr= np.array([[98, 73, 15, 74, 5],
[16, 26, 75, 79, 30],
[39, 65, 54, 72, 82],
[70, 63, 79, 24, 86],
[91, 13, 28, 87, 52]])
arr_2 = np.where(arr > 70, 0, arr)
print(arr_2)
# output is array([[ 0, 0, 15, 0, 5],
# [16, 26, 0, 0, 30],
# [39, 65, 54, 0, 0],
# [70, 63, 0, 24, 0],
# [ 0, 13, 28, 0, 52]])
arr_3 = np.where(arr > 70, arr, 1)
print(arr_3)
# output is array([[98, 73, 1, 74, 1],
# [ 1, 1, 75, 79, 1],
# [ 1, 1, 1, 72, 82],
# [ 1, 1, 79, 1, 86],
# [91, 1, 1, 87, 1]])
arr_4 = np.where(arr > 70, arr, -arr)
print(arr_4)
# output is array([[ 98, 73, -15, 74, -5],
# [-16, -26, 75, 79, -30],
# [-39, -65, -54, 72, 82],
# [-70, -63, 79, -24, 86],
# [ 91, -13, -28, 87, -52]])
array끼리 shape이 동일해야 한다.
x = np.array([[98, 73, 15, 74, 5],
[16, 26, 75, 79, 30],
[39, 65, 54, 72, 82],
[70, 63, 79, 24, 86],
[91, 13, 28, 87, 52]])
y = np.array([[12, 57, 61, 13, 29],
[ 9, 96, 5, 30, 21],
[ 9, 90, 5, 18, 45],
[75, 11, 79, 33, 31],
[ 5, 24, 1, 12, 93]])
덧셈: x + y
뺄셈: x - y
곱셈: x * y
나눗셈: x / y
np.dot(array변수명1, array변수명2)
dot product 연산은 vector의 외적을 의미한다.
arr_1 = np.random.randint(1, 10, (2, 4))
arr_2 = np.random.randint(1, 10, (4, 2))
print(np.dot(arr_1, arr_2))
array변수명.T
arr = np.array([[1, 2, 3,],
[4, 5, 6]])
print(arr.T) # output is array([[1, 4],
# [2, 5],
# [3, 6]])
np.random.shuffle(array변수명)
arr = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
np.random.shuffle(arr)
print(arr) # output is [[4 5 6]
# [7 8 9]
# [1 2 3]]
np.random.choice(변수명, 추출개수)
추출된 요소를 다시 array에 넣고 추출한다. 즉, 중복 추출 허용
arr = np.array([[98, 73, 15, 74, 5],
[16, 26, 75, 79, 30],
[91, 13, 28, 87, 52]])
# 1차원으로 변환
arr = arr.faltten()
np.random.choice(arr,3)
np.random.choice(변수명, 추출개수, replace = False)
추출된 요소는 다시 array에 넣지 않는다. 즉, 중복 추출 불가
arr = np.array([[98, 73, 15, 74, 5],
[16, 26, 75, 79, 30],
[91, 13, 28, 87, 52]])
# 1차원으로 변환
arr = arr.flatten()
np.random.choice(arr,3, replace = False)