250x250
Link
λ‚˜μ˜ GitHub Contribution κ·Έλž˜ν”„
Loading data ...
Notice
Recent Posts
Recent Comments
관리 메뉴

Data Science LAB

[Python] Numpy λ‚œμˆ˜ 생성 (Random) λ³Έλ¬Έ

🐍 Python/NumPy

[Python] Numpy λ‚œμˆ˜ 생성 (Random)

γ…… γ…œ γ…” γ…‡ 2022. 8. 25. 00:42
728x90

1. np.random.rand(m,n)

0~1의 균일뢄포 ν‘œμ€€ μ •κ·œλΆ„ν¬ λ‚œμˆ˜λ₯Ό  matrix array(m,n) ν˜•νƒœλ‘œ 생성
import numpy as np

np.random.rand(6)
# array([0.43370799, 0.86829053, 0.43087038, 0.20789529, 0.41183189, 0.40093899])

np.random.rand(2,3)
# array([[0.69712992, 0.32758099, 0.20567256],
#       [0.62130179, 0.64666152, 0.71468473]])

숫자λ₯Ό ν•˜λ‚˜λ§Œ μž…λ ₯ν•˜λ©΄ 1차원 λ°°μ—΄ ν˜•νƒœλ‘œ 생성됨

 

2. np.random.randn(m,n)

평균이 0, ν‘œμ€€νŽΈμ°¨ 1인 κ°€μš°μ‹œμ•ˆ ν‘œμ€€ μ •κ·œλΆ„ν¬ λ‚œμˆ˜λ₯Ό matrix(m,n)ν˜•νƒœλ‘œ 생성
np.random.randn(4)
# array([-0.41143005,  0.52455404,  0.2193374 ,  1.58083578])

np.random.randn(2,4)
# array([[ 0.54727886,  1.02628151, -0.93502847, -0.65923374],
#        [ 2.9750309 ,  0.33535613, -0.2068257 ,  1.51217218]])

 

 

3. np.random.randint(m,n,k)

μ‹œμž‘ ~ n-1 μ‚¬μ΄μ˜ 랜덀 숫자λ₯Ό k개 λ½‘μŒ
np.random.randint(3,11,2)
# array([4, 7])

np.random.randint(3,11)
# 3

λ§ˆμ°¬κ°€μ§€λ‘œ k인수λ₯Ό μž…λ ₯ν•˜μ§€ μ•ŠμœΌλ©΄ 숫자 1개만 생성함

 

 

4. np.random.standard_normal()

ν‘œμ€€μ •κ·œλΆ„ν¬λ‘œλΆ€ν„° μƒ˜ν”Œλ§ 된 λ‚œμˆ˜λ₯Ό λ°˜ν™˜ν•¨

randn()κ³Ό λΉ„μŠ·ν•˜μ§€λ§Œ, νŠœν”Œμ„ 인자둜 λ°›μŒ
np.random.standard_normal(1,3)

# TypeError: standard_normal() takes at most 1 positional argument (2 given)

np.random.standard_normal(2)
# array([ 1.53386505, -0.59942005])

np.random.standard_normal((2,3))
# array([[-1.53569412, -0.83581106, -1.09411571],
#        [-0.38692089, -0.66502681,  2.48339435]])

νŠœν”Œ ν˜•μ‹μ΄ μ•„λ‹Œ 인자λ₯Ό λ°›μœΌλ©΄ μ—λŸ¬

 

 

5. np.random.normal(n,m,k)

μ •κ·œλΆ„ν¬λ‘œλΆ€ν„° μƒ˜ν”Œλ§λœ λ‚œμˆ˜ λ°˜ν™˜

N(m,m**2)μœΌλ‘œλΆ€ν„° 얻은 λ‚œμˆ˜ k개 λ°˜ν™˜
np.random.normal(1.5, 1.5, 4)
# array([0.29619501, 2.52406325, 2.95366161, 0.02574515])

np.random.normal(3.0, 4.0, (2, 3))
# array([[ 3.61677682,  1.5624253 , -0.89876034],
#       [ 5.42376827, -4.44813517,  5.60946705]])

 

 

 

6. np.random.random.sample()

0~1 λ²”μœ„μ—μ„œ μƒ˜ν”Œλ§ 된 μž„μ˜μ˜ μ‹€μˆ˜λ₯Ό λ°˜ν™˜
np.random.random_sample((2,3))
# array([[0.51829587, 0.15548231, 0.21444834],
#       [0.41911137, 0.40374473, 0.72714865]])

 

 

 

7. np.random.choice()

주어진 1차원 arrayλ‘œλΆ€ν„° μž„μ˜μ˜ μƒ˜ν”Œμ„ 생성
np.random.choice(5,4)
# array([4, 1, 3, 3])
# np.arange(5)μ—μ„œ 뽑은 4개의 μƒ˜ν”Œμ„ 1차원 array둜 λ°˜ν™˜

np.random.choice(5,(2,3))
# array([[2, 1, 3],
#       [3, 0, 2]])
# np.arange(5)μ—μ„œ 뽑은 (2,3)ν˜•μ‹μ˜ array둜 λ°˜ν™˜

 

 

728x90

'🐍 Python > NumPy' μΉ΄ν…Œκ³ λ¦¬μ˜ λ‹€λ₯Έ κΈ€

[Python] np.linspace()ν•¨μˆ˜  (0) 2022.05.23
[Python] NumPyλ₯Ό μ΄μš©ν•œ ν–‰λ ¬μ˜ μ •λ ¬  (0) 2022.02.06
[Python] NumPy 인덱싱(Indexing)  (0) 2022.02.04
NumPyλž€?  (0) 2022.02.03
Comments