import numpy as np
np.arange(30) # arange : list의 range와 같은 효과, integer로 0부터 29까지 배열 추출
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])
np.arange(0, 5, 0.5) # (시작, 끝, step) & floating point 표시 가능
array([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]])
np.zeros(shape=(10,), dtype=np.int8) # 10 - zero vector 생성
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0], dtype=int8)
np.zeros((2, 5)) # 2 by 5 - zero matrix 생성
array([[0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0.]])
np.ones(shape=(10,), dtype=np.int8)
array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1], dtype=int8)
np.ones((2,5))
array([[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.]])
np.empty(shape=(10,), dtype=np.int8)
array([110, 0, 32, 0, 70, 0, 105, 0, 108, 0], dtype=int8)
np.empty((3,5))
array([[0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0.]])
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]])
np.zeros_like(test_matrix)
array([[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0]])
np.identity(n=3, dtype=np.int8) # n : number of rows
array([[1, 0, 0],
[0, 1, 0],
[0, 0, 1]], dtype=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.]])
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)
np.eye(3)
array([[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]])
np.eye(3, 5, k=2) # k : start index
array([[0., 0., 1., 0., 0.],
[0., 0., 0., 1., 0.],
[0., 0., 0., 0., 1.]])
matrix = np.arange(9).reshape(3,3)
np.diag(matrix)
array([0, 4, 8])
np.diag(matrix, k=1) # k : start index
array([1, 5])
np.random.uniform(0,1,10).reshape(2,5) # 균등분포
array([[0.39687944, 0.14559092, 0.62898727, 0.26847138, 0.60052723],
[0.69149895, 0.40151342, 0.02206676, 0.7171968 , 0.81369266]])
np.random.normal(0,1,10).reshape(2,5) # 정규분포
array([[-0.04325594, -0.50706879, -0.37899264, -0.35053484, 0.18610277],
[-1.0736414 , 0.34261525, 1.18722716, -1.84160466, 0.75003724]])