ndarray를 생성할 때 random을 이용하면 난수로 값을 채우며 생성할 수 있다.
- np.random.normal
- np.random.rand
- np.random.randn
- np.random.radint
- np.random.random
np.random.normal(mean, std, shape)
정규분포에서 실수 표본을 추출하여 생성
mean = 0
std = 1 # 표준정규분포
arr = np.random.normal(mean, std, (1000000,))
print(arr)
# 정규분포에 진짜로 따르는지 확인
plt.hist(arr, bins=100)
plt.show()
0.03062562]

np.random.randn()
표준정규분포에서 실수 표본을 추출
arr = np.random.randn(100000)
print(arr)
plt.hist(arr, bins=100)
plt.show()
-0.90040773]

np.random.rand(shape)
[0,1)의 범위 균등 분포에서 실수 표본을 추출
arr = np.random.rand(1000000) # 인자가 1개면 1차원, 2개면 2차원 ...
print(arr)
plt.hist(arr, bins=100)
plt.show()

np.random.random()
[0,1) 범위의 균등 분포에서 실수 표본을 추출
arr = np.random.random((100000,))
print(arr)
plt.hist(arr, bins=100)
plt.show()
[0.51801271 0.72641858 0.39811911 ... 0.76886222 0.89438647 0.4184151 ]

np.random.randint(low, high, shape)
균등 분포에서 정수 표본을 추출
arr = np.random.randint(1, 100, (100000,))
print(arr)
plt.hist(arr, bins=99)
plt.show()
[19 67 36 ... 35 44 28]

random 관련 함수
seed()
랜덤값을 도출하는 초기값을 고정하여 항상 같은 값을 얻을 수 있게 함.
난수의 재현성을 확보.
# 난수이기 때문에 실행할때마다 값이 다름.
for i in range(3) :
arr = np.random.randint(1, 10, (5,))
print(arr)
[3 2 8 8 1]
[1 3 9 6 7]
[6 5 3 6 1]
세 번의 결과가 모두 다른데, seed를 사용하면
for i in range(3) :
np.random.seed(1)
arr = np.random.randint(1, 10, (5,))
print(arr)
[6 9 6 1 1]
[6 9 6 1 1]
[6 9 6 1 1]
모두 같은 값이 나온다.
shuffle()
데이터의 순서를 바꾼다.
복사본을 만드는 것이 아니라 원본을 섞는다.
arr = np.arange(1, 10)
new_arr = np.random.shuffle(arr) # 이렇게 쓰지 않음!
print(new_arr)
None
복사본을 만들지 않기 때문에 위의 방법으로 쓰지 않는다.
arr = np.arange(1, 10)
print(arr)
np.random.shuffle(arr) # => 이렇게 사용!!
print(arr)
[1 2 3 4 5 6 7 8 9]
[5 6 3 9 4 8 2 7 1]
choice()
sampling(표본 추출)에 사용
np.random.choice(a, size, replace, p)
- a : 모집단, 배열(ndarray)
- size : 추출할 표본의 크기
- replace : 복원 추출 여부. True-복원추출(default), False-비복원추출
- p : 각 데이터가 선택될 수 있는 확률
arr = np.random.choice(np.arange(1,10), 3)
# arr = np.random.choice(np.arange(1,10), 3, replace=True) 와 같다.
arr
array([5, 8, 8, 2, 8])
arr = np.random.choice(np.arange(1,10), 5, False)
arr
array([3, 9, 5, 4, 8])
arr = np.random.choice(np.array([1,2,3,4,5]), 3, p=[0.1, 0.2, 0.5, 0.2, 0])
# p의 합은 1
arr
array([3, 3, 2])
'Programming > Numpy' 카테고리의 다른 글
[numpy][pandas] np.histogram()으로 도수분포표 만들기 (0) | 2022.03.23 |
---|---|
[numpy] ndarray의 indexing과 slicing (0) | 2022.03.15 |
[numpy] ndarray의 shape과 관련된 함수들 (0) | 2022.03.15 |
[numpy] ndarray를 생성하는 여러가지 방법 1 (0) | 2022.03.15 |
[numpy] 데이터 핸들링을 위한 넘파이 (0) | 2022.03.15 |
ndarray를 생성할 때 random을 이용하면 난수로 값을 채우며 생성할 수 있다.
- np.random.normal
- np.random.rand
- np.random.randn
- np.random.radint
- np.random.random
np.random.normal(mean, std, shape)
정규분포에서 실수 표본을 추출하여 생성
mean = 0
std = 1 # 표준정규분포
arr = np.random.normal(mean, std, (1000000,))
print(arr)
# 정규분포에 진짜로 따르는지 확인
plt.hist(arr, bins=100)
plt.show()
0.03062562]

np.random.randn()
표준정규분포에서 실수 표본을 추출
arr = np.random.randn(100000)
print(arr)
plt.hist(arr, bins=100)
plt.show()
-0.90040773]

np.random.rand(shape)
[0,1)의 범위 균등 분포에서 실수 표본을 추출
arr = np.random.rand(1000000) # 인자가 1개면 1차원, 2개면 2차원 ...
print(arr)
plt.hist(arr, bins=100)
plt.show()

np.random.random()
[0,1) 범위의 균등 분포에서 실수 표본을 추출
arr = np.random.random((100000,))
print(arr)
plt.hist(arr, bins=100)
plt.show()
[0.51801271 0.72641858 0.39811911 ... 0.76886222 0.89438647 0.4184151 ]

np.random.randint(low, high, shape)
균등 분포에서 정수 표본을 추출
arr = np.random.randint(1, 100, (100000,))
print(arr)
plt.hist(arr, bins=99)
plt.show()
[19 67 36 ... 35 44 28]

random 관련 함수
seed()
랜덤값을 도출하는 초기값을 고정하여 항상 같은 값을 얻을 수 있게 함.
난수의 재현성을 확보.
# 난수이기 때문에 실행할때마다 값이 다름.
for i in range(3) :
arr = np.random.randint(1, 10, (5,))
print(arr)
[3 2 8 8 1]
[1 3 9 6 7]
[6 5 3 6 1]
세 번의 결과가 모두 다른데, seed를 사용하면
for i in range(3) :
np.random.seed(1)
arr = np.random.randint(1, 10, (5,))
print(arr)
[6 9 6 1 1]
[6 9 6 1 1]
[6 9 6 1 1]
모두 같은 값이 나온다.
shuffle()
데이터의 순서를 바꾼다.
복사본을 만드는 것이 아니라 원본을 섞는다.
arr = np.arange(1, 10)
new_arr = np.random.shuffle(arr) # 이렇게 쓰지 않음!
print(new_arr)
None
복사본을 만들지 않기 때문에 위의 방법으로 쓰지 않는다.
arr = np.arange(1, 10)
print(arr)
np.random.shuffle(arr) # => 이렇게 사용!!
print(arr)
[1 2 3 4 5 6 7 8 9]
[5 6 3 9 4 8 2 7 1]
choice()
sampling(표본 추출)에 사용
np.random.choice(a, size, replace, p)
- a : 모집단, 배열(ndarray)
- size : 추출할 표본의 크기
- replace : 복원 추출 여부. True-복원추출(default), False-비복원추출
- p : 각 데이터가 선택될 수 있는 확률
arr = np.random.choice(np.arange(1,10), 3)
# arr = np.random.choice(np.arange(1,10), 3, replace=True) 와 같다.
arr
array([5, 8, 8, 2, 8])
arr = np.random.choice(np.arange(1,10), 5, False)
arr
array([3, 9, 5, 4, 8])
arr = np.random.choice(np.array([1,2,3,4,5]), 3, p=[0.1, 0.2, 0.5, 0.2, 0])
# p의 합은 1
arr
array([3, 3, 2])
'Programming > Numpy' 카테고리의 다른 글
[numpy][pandas] np.histogram()으로 도수분포표 만들기 (0) | 2022.03.23 |
---|---|
[numpy] ndarray의 indexing과 slicing (0) | 2022.03.15 |
[numpy] ndarray의 shape과 관련된 함수들 (0) | 2022.03.15 |
[numpy] ndarray를 생성하는 여러가지 방법 1 (0) | 2022.03.15 |
[numpy] 데이터 핸들링을 위한 넘파이 (0) | 2022.03.15 |