의사난수 생성

Python으로 살펴보는 표본추출(Sampling)

James Chapman

Curriculum Manager, DataCamp

무작위의 의미는?

형용사 체계나 의도적 선택 없이 만들어지거나 일어나는 것.

1 Oxford Languages
Python으로 살펴보는 표본추출(Sampling)

진정한 난수

  • 동전 던지기 같은 물리 현상에서 생성
  • Hotbits는 방사성 붕괴 사용
  • RANDOM.ORG는 대기 잡음 사용
  • 진정한 무작위성은 비용이 큼
1 https://www.fourmilab.ch/hotbits 2 https://www.random.org
Python으로 살펴보는 표본추출(Sampling)

의사난수 생성

  • 의사난수 생성은 비용이 낮고 빠름
  • 다음 "무작위" 수는 직전 "무작위" 수에서 계산
  • 첫 "무작위" 수는 시드에서 계산
  • 같은 시드는 같은 난수를 생성
Python으로 살펴보는 표본추출(Sampling)

의사난수 생성 예시

seed = 1
calc_next_random(seed)
3
calc_next_random(3)
2
calc_next_random(2)
6
Python으로 살펴보는 표본추출(Sampling)

난수 생성 함수

  • numpy.random을 접두로 사용, 예: numpy.random.beta()
function distribution function distribution
.beta 베타 .hypergeometric 초기하
.binomial 이항 .lognormal 로그정규
.chisquare 카이제곱 .negative_binomial 음이항
.exponential 지수 .normal 정규
.f F .poisson 푸아송
.gamma 감마 .standard_t t
.geometric 기하 .uniform 균등
Python으로 살펴보는 표본추출(Sampling)

난수 시각화

randoms = np.random.beta(a=2, b=2, size=5000)
randoms
array([0.6208281 , 0.73216171, 0.44298403, ..., 
       0.13411873, 0.52198411, 0.72355098])
plt.hist(randoms, bins=np.arange(0, 1, 0.05))
plt.show()

베타 분포 히스토그램

Python으로 살펴보는 표본추출(Sampling)

난수 시드

np.random.seed(20000229)
np.random.normal(loc=2, scale=1.5, size=2)
array([-0.59030264, 1.87821258])
np.random.normal(loc=2, scale=1.5, size=2)
array([2.52619561, 4.9684949 ])
np.random.seed(20000229)
np.random.normal(loc=2, scale=1.5, size=2)
array([-0.59030264, 1.87821258])
np.random.normal(loc=2, scale=1.5, size=2)
array([2.52619561, 4.9684949 ])
Python으로 살펴보는 표본추출(Sampling)

다른 시드 사용

np.random.seed(20000229)
np.random.normal(loc=2, scale=1.5, size=2)
array([-0.59030264, 1.87821258])
np.random.normal(loc=2, scale=1.5, size=2)
array([2.52619561, 4.9684949 ])
np.random.seed(20041004)
np.random.normal(loc=2, scale=1.5, size=2)
array([1.09364337, 4.55285159])
np.random.normal(loc=2, scale=1.5, size=2)
array([2.67038916, 2.36677492])
Python으로 살펴보는 표본추출(Sampling)

Ayo berlatih!

Python으로 살펴보는 표본추출(Sampling)

Preparing Video For Download...