np.ndarray
ex 1 )
# 벡터 (1d)
vector = np.array([1, 2, 3, 4, 5])
print('vector :', vector)
print('vector.shape :', vector.shape)
print('vector.dtype :', vector.dtype)
print('vector.size :', vector.size)
print('vector.ndim :', vector.ndim)
vector : [1 2 3 4 5]
vector.shape : (5,)
vector.dtype : int32
vector.size : 5
vector.ndim : 1
ex 2)
# 행렬 (2d)
matrix = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print('matrix :\n', matrix)
print('matrix.shape :', matrix.shape)
print('matrix.dtype :', matrix.dtype)
print('matrix.size :', matrix.size)
print('matrix.ndim :', matrix.ndim)
print('matrix[0][1] or [0, 1] :', matrix[0][1], matrix[0, 1])
matrix :
[[1 2 3]
[4 5 6]
[7 8 9]]
matrix.shape : (3, 3)
matrix.dtype : int32
matrix.size : 9
matrix.ndim : 2
matrix[0][1] or [0, 1] : 2 2
np.arange()
print(np.arange(10))
print(np.arange(5, 10))
[0 1 2 3 4 5 6 7 8 9]
[5 6 7 8 9]
np.zeros()
print(np.zeros((1, 2)))
[[0. 0.]]
np.ones()
print(np.ones((1, 2)))
[[1. 1.]]
np.full()
print(np.full((1, 2), 5))
[[5 5]]
np.eye()
n x n
형태의 단위 행렬 반환print(np.eye(3))
[[1. 0. 0.]
[0. 1. 0.]
[0. 0. 1.]]
np.random.default_rng()
random_number_generator = np.random.default_rng(seed=10)
rand_int = random_number_generator.integers(low=0, high=2, size=2)
rand_float = random_number_generator.random()
print('rng.integers :', rand_int)
print('rng.random :', rand_float)
rng.integers : [1 1]
rng.random : 0.20768181007914688
arr1 = np.array([
[1, 1],
[2, 2]
])
arr2 = np.array([
[3, 3],
[4, 4]
])
np.vstack()
np.vstack((arr1, arr2))
[[1 1]
[2 2]
[3 3]
[4 4]]
np.hstack()
np.hstack((arr1, arr2))
[[1 1 3 3]
[2 2 4 4]]
arr = np.arange(9)
print(arr)
print('shape :', arr.shape)
[0 1 2 3 4 5 6 7 8]
shape : (9,)
np.newaxis
ex 1 ) 행 벡터 (행 자리에 np.newaxis
)
newaxis = arr[np.newaxis, :]
print(newaxis)
print('shape :', newaxis.shape)
[[0 1 2 3 4 5 6 7 8]]
shape : (1, 9)
ex 2 ) 열 벡터로 만들기 (열 자리에 np.newaxis
)
newaxis = arr[:, np.newaxis]
print(newaxis)
print('shape :', newaxis.shape)
[[0]
[1]
[2]
[3]
[4]
[5]
[6]
[7]
[8]]
shape : (9, 1)
np.expand_dims()
ex 1 ) 행 벡터 (axis = 0
)
expand_row = np.expand_dims(arr, axis=0)
print(expand_row)
print('shape :', expand_row.shape)
[[0 1 2 3 4 5 6 7 8]]
shape : (1, 9)
ex 2 ) 열 벡터 (axis = 1
)
expand_col = np.expand_dims(arr, axis=1)
print(expand_col)
print('shape :', expand_col.shape)
[[0]
[1]
[2]
[3]
[4]
[5]
[6]
[7]
[8]]
shape : (9, 1)
arr = np.arange(9)
print(arr)
print('shape :', arr.shape)
[0 1 2 3 4 5 6 7 8]
shape : (9,)
ndarray.resize()
arr.resize((3, 3))
print(arr)
print('shape :', arr.shape)
[[0 1 2]
[3 4 5]
[6 7 8]]
shape : (3, 3)
np.transpose()
또는 ndarray.T
# 위 3x3 행렬의 전치행렬
transpose = np.transpose(arr)
transpose = arr.T
print(transpose)
print('shape :', transpose.shape)
[[0 3 6]
[1 4 7]
[2 5 8]]
shape : (3, 3)
ndarray.ravel()
또는 ndarray.flatten()
flatten = arr.ravel()
flatten = arr.flatten()
print(flatten)
print('shape :', flatten.shape)
[0 1 2 3 4 5 6 7 8]
shape : (9,)
ndarray.dtype
arr = np.arange(20)
print('arr 배열의 타입 :', type(arr))
print('arr 배열 안의 원소의 타입 :', arr.dtype)
arr 배열의 타입 : <class 'numpy.ndarray'>
arr 배열 안의 원소의 타입 : int32
ndarray.astype()
arr_float32 = arr.astype(np.float32)
print('arr_float32 배열 안의 원소의 타입 :', arr_float32.dtype)
arr_float32 배열 안의 원소의 타입 : float32
np.iinfo()
np.iinfo('int32')
# iinfo(min=-2147483648, max=2147483647, dtype=int32)
np.finfo()
np.finfo('float32')
# finfo(resolution=1e-06, min=-3.4028235e+38, max=3.4028235e+38, dtype=float32)
arr = np.array([[1 , 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
condition = (arr < 5) # 조건
print(arr)
[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]]
np.any()
True
반환np.any(condition) # True
np.all()
True
반환np.all(condition) # False
np.where()
np.where(condition)
(array([0, 0, 0, 0], dtype=int64), array([0, 1, 2, 3], dtype=int64))
np.where(condition, ' 참 ', '거짓')
[[' 참 ' ' 참 ' ' 참 ' ' 참 ']
['거짓' '거짓' '거짓' '거짓']
['거짓' '거짓' '거짓' '거짓']]
np.clip()
np.clip(arr, 5, 8)
[[5 5 5 5]
[5 6 7 8]
[8 8 8 8]]
np.isinf()
: 무한한 값인지 확인np.isnan()
: 결측치인지 확인np.isfinite()
: 유한한 값인지 확인arr = np.array([
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
])
# 열의 개수가 같아야 함
mask = [True, False, True]
print(arr[:, mask])
[[1 3]
[4 6]
[7 9]]
ex 1 ) 행렬 & 스칼라
big = np.array([
[1, 2, 3],
[10, 20, 30]
])
small = 2
print(big * small)
[[ 2 4 6]
[20 40 60]]
ex 2 ) 행렬 & 벡터
big = np.array([
[1, 2, 3],
[10, 20, 30]
])
small = np.array([1, 2, 3])
print(big * small)
[[ 1 4 9]
[10 40 90]]