기록하는 습관

[Numpy] 010. 랜덤 표본 추출 (Random Sampling) 본문

Python/Numpy

[Numpy] 010. 랜덤 표본 추출 (Random Sampling)

Avalla 2023. 8. 10. 18:00

샘플링 함수들

import numpy as np

 

np.random.uniform(하한, 상한, 형태)

Docstring:

uniform(low=0.0, high=1.0, size=None)

Draw samples from a uniform distribution.

Samples are uniformly distributed over the half-open interval
[low, high) (includes low, but excludes high). In other words,
any value within the given interval is equally likely to be drawn
by uniform.

print( np.random.uniform(0, 1, 5) )
print( np.random.uniform(0, 1, (2,3)) )
[0.71029172 0.00673713 0.42074585 0.53413197 0.62083628]
[[0.46548593 0.62558901 0.2386203 ]
 [0.49704519 0.06470508 0.0865601 ]]

 

np.random.normal(평균, 표준편차, 형태)

print( np.random.normal(0, 1, 4) )
print( np.random.normal(50, 10, (2,2)) )
print( np.random.normal(0, 1, 1000000).mean() )
[ 0.77727106  0.91040219 -0.70066303  1.31100763]
[[42.03442125 54.90997165]
 [57.27299351 60.1309165 ]]
0.0004011744654032617
import pandas as pd
pd.DataFrame({"sample":np.random.normal(0, 1, 1000)}).hist()

np.random.randint(상한, 하한, 형태)

Docstring:

randint(low, high=None, size=None, dtype=int)

Return random integers from low (inclusive) to high (exclusive).

Return random integers from the "discrete uniform" distribution of
the specified dtype in the "half-open" interval [low, high). If
high is None (the default), then results are from [0, low).

print( np.random.randint(0, 10, (3,4)) )
print( np.random.randint(0, 10, (3,1,1)) )
[[4 5 5 5]
 [9 4 5 8]
 [0 3 0 3]]
[[[8]]

 [[3]]

 [[6]]]

 

np.random.choice(1차원배열, 형태)

Docstring:

choice(a, size=None, replace=True, p=None)

Generates a random sample from a given 1-D array

print( np.random.choice(['가위', '바위', '보']) )
print( np.random.choice(['가위', '바위', '보'], size=0) )
print( np.random.choice(['가위', '바위', '보'], size=1) )
print( np.random.choice(['가위', '바위', '보'], size=(2,2)) )
보
[]
['바위']
[['바위' '바위']
 ['보' '가위']]