np.ones, np.zeros, np.eye, np.full
np.ones(6)
#array([1., 1., 1., 1., 1., 1.])
np.ones((2, 3), dtype=np.int16)
#array([[1, 1, 1],[1, 1, 1]], dtype=int16)
# np.zeros(shape) 0 으로 채워진 array 생성, dtype=float64(기본값) 으로 생성됨
np.zeros((3, 2, 5))
"""
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.eye(N) 단위행렬, 주대각선만 1이고 나머지는 0 인 (N,N) array 생성
np.eye(5, dtype=np.int16) # -> (5, 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]], dtype=int16)
"""
# np.full(shape, fill_value) 특정값으로 채워진 array 생성
np.full((2, 3), 10)
array([[10, 10, 10],[10, 10, 10]])
np.random
# rand: 0부터 1사이의 균일 분포 [0, 1) uniform distribution
# randn: 가우시안 표준 정규 분포 (평균0, 표준편차1 인 종모양 분포) normal distribution
# randint: 균일 분포의 정수 난수
# seed: 램덤 시드값. 고정 난수값 생성
np.random.rand(10)
# array([0.58856886, 0.26093418, 0.64921407, 0.66552132, 0.81017877,
# 0.33000508, 0.91659605, 0.19783543, 0.54494915, 0.57904898])
np.random.rand(2, 3)
# array([[0.14309353, 0.30486328, 0.85275412],
# [0.24001492, 0.04883082, 0.84448773]])
np.random.randn(5)
# array([ 0.29904924, -1.68149036, 0.77227703, -0.43302774, 0.19593748])
np.random.randint(5)
# 3
np.random.randint(1, 10)
# 7
np.random.randint(1, 100, (3, 5, 2))
"""
array([[[96, 88],
[59, 19],
[88, 60],
[97, 17],
[68, 75]],
[[47, 80],
[18, 11],
[92, 67],
[51, 72],
[99, 24]],
[[86, 15],
[16, 62],
[19, 27],
[86, 32],
[76, 32]]])
"""
# seed() : 난수를 예측가능하도록 만든다. 실행할때마다 동일한 난수가 발생되도록 함.
np.random.seed(0)
np.random.rand(4)
'AI > 파이썬' 카테고리의 다른 글
| pandas - Series (0) | 2026.04.09 |
|---|---|
| numpy - Boolean Indexing (0) | 2026.04.08 |
| numpy - axis (차원축) (0) | 2026.04.07 |
| numpy - shape (차원) 변경하기 (0) | 2026.04.06 |
| numpy(1) (0) | 2026.04.03 |