Numpy. section2 : ndarray 만들기. Lec8. 랜덤한 값으로 ndarray 만들기

timekeeep·2023년 2월 11일
0

Numpy

목록 보기
7/28

[0] Random값으로 ndarray 만들기

  • 테스트 데이터셋 만들시
  • 노이즈 추가시

[1] Making ndarrays from Normal Distributions

  • numpy.random.randn(평균 0, 표준편차 1) , numpy.random.normal 두 가지 함수사용
  • print(random_values.shape) # (300,) 300개의 datapoint를 가지는 vector가 만들어진다

  • 3시그마가 넘어가면 거의 샘플링이 되지 않는다
  • 샘플링은 어떤 확률분포에서 값들을 추출하는 것
  • independent sampling : 첫번째 샘플링된 수는 다음 샘플링에 영향을 미치지 않는다

normal1 = np.random.normal(loc = 2, scale = 1, size = (200,))

normal1 = np.random.normal(loc = 2, scale = 1, size = (200,))
normal2 = np.random.normal(loc = 0, scale = 2, size = (200,))
normal3 = np.random.normal(loc = 3, scale = 5, size = (200,))

normal = np.random.normal(loc = [-2, 0, 3], scale = [1,2,5], size = (200,3))
  • 각각의 컬럼들이 데이터 벡터들을 만들게 됨

  • 노이즈 (100,3)짜리 생성후 더해주면 노이즈가 포함된 데이터셋 생성가능

#Making ndarrays from Normal Distribution

import numpy as np
import matplotlib.pyplot as plt
plt.style.use('seaborn')

fig, ax = plt.subplots(figsize = (10,5))

random_values = np.random.randn(300) #300개 뽑기
ax.hist(random_values, bins = 20) #histogram 그리기
print(random_values.shape) # (300,) 300개의 datapoint를 가지는 vector가 만들어진다

normal1 = np.random.normal(loc = 2, scale = 1, size = (200,))
normal2 = np.random.normal(loc = 0, scale = 2, size = (200,))
normal3 = np.random.normal(loc = 3, scale = 5, size = (200,))a

normal = np.random.normal(loc = [-2, 0, 3], scale = [1,2,5], size = (200,3))

normal = np.random.normal(loc = -2, scale = 1, size = (3,3))

[2] Making ndarrays from Uniform Distributions

  • rand는 [0,1)의 범위로 확률값을 뽑음
  • uniform은 [)의 범위를 설정해줄 수 있는 api이다.

#Making ndarrays from Normal Distribution

import numpy as np
import matplotlib.pyplot as plt
plt.style.use('seaborn')

fig, ax = plt.subplots(figsize = (10,5))

random_values = np.random.randn(300) #300개 뽑기
ax.hist(random_values, bins = 20) #histogram 그리기
print(random_values.shape) # (300,) 300개의 datapoint를 가지는 vector가 만들어진다

normal1 = np.random.normal(loc = 2, scale = 1, size = (200,))
normal2 = np.random.normal(loc = 0, scale = 2, size = (200,))
normal3 = np.random.normal(loc = 3, scale = 5, size = (200,))a

normal = np.random.normal(loc = [-2, 0, 3], scale = [1,2,5], size = (200,3))

normal = np.random.normal(loc = -2, scale = 1, size = (3,3))

#Making ndarrays from Uniform Distribution

import numpy as np
import matplotlib.pyplot as plt

fig, ax = plt.subplots(figsize = (10,5))

uniform = np.random.rand(1000) #[0,1)에서 뽑음

ax.hist(uniform)
print(uniform.shape)

uniform = np.random.rand(2,3,4)
print(uniform.shape)

uniform = np.random.uniform(low = -10, high = 10, size = (10000,))
ax.hist(uniform, bins = 20)

randint = np.random.randint(low = 0, high = 7, size = (20,))
print(randint) # [3 5 2 4 0 0 0 3 1 1 1 4 2 3 1 0 4 3 2 6 ]

profile
Those who are wise will shine like the brightness of the heavens, and those who lead many to righteousness, like the stars for ever and ever

0개의 댓글