numpy.random 3총사 정리(rand, randint, randn)

Gi Woon Lee·2024년 8월 9일
0

TIL

목록 보기
35/78

np.random.rand

random.rand(d0, d1, d2, ...,dn)
0~1 사이 균일분포를 따르는 array 생성

  • d0, d1 ~ dn: 행렬의 차원이다.
np.random.rand(3,2) # 3행 2열
array([[ 0.14022471,  0.96360618],  #random
       [ 0.37601032,  0.25528411],  #random
       [ 0.49313049,  0.94909878]]) #random

np.random.randint

random.randint(low, high=None, size=None, dtype=int)
low이상 high미만 값의 무작위 정수를 리턴한다.
high값이 없으면(디폴트는 없음) 0이상 low미만 값의 무작위 정수를 리턴.

  • low: int or array-like ints
  • high: int or array-like of ints, optional
  • size:
    size(정수) -> 생성할 정수의 개수
    size(튜플) -> 행렬의 형태 ex) size(2,3)은 2행 3렬 형태임, optional
  • dtype: dtype, optional 기본값은 int임

단일 정수 생성:

import numpy as np
print(np.random.randint(10) #0이상10미만의 정수 생성

두 개의 값 지정:

print(np.random.randint(5,15)) #5이상 100미만의 정수 생성

매트릭스 생성:

print(np.random.randint(1, 100, size=5) # 1이상 100미만의 정수 5개 생성
print(np.random.randint(1, 100, size=(2,3))) # 1 이상 100미만의 정수로 2행 3렬 매트릭스 생성 

데이터 타입 지정:

print(np.random.randint(1,10,size=3,dtype=np.int64)) #1이상10미만의 64비트 정수 3개 생성

np.random.randn

random.randn(d0, d1, ..., dn)
표준 정규분포(평균이 0, 표준편차 1)에서 무작위 샘플을 생성

d0 인자는 하나가 입력되어 있으면 원소 개수, 복수면 차원을 의미한다.

import numpy as np
print(np.random.randn(5))  # 5개의 원소를 가지는 1차원 배열 생성
import numpy as np
print(np.random.randn(2, 3, 4))  # 2x3x4 형태의 3차원 배열 생성

0개의 댓글