scikit-learn으로 합성 데이터셋 만들기

Python으로 배우는 데이터 프라이버시와 익명화

Rebeca Gonzalez

Data engineer

Scikit-learn으로 데이터셋 생성

  • 확률분포에서 샘플링한 데이터셋을 만들 수 있습니다

  • 예: 정규분포

정규분포 히스토그램 예시

Python으로 배우는 데이터 프라이버시와 익명화

정규분포

자연에서 자주 관찰됩니다

  • 혈압
  • IQ 점수

정규분포를 따르는 대규모 키 데이터셋의 히스토그램

Python으로 배우는 데이터 프라이버시와 익명화

정규분포에서 샘플 생성

import numpy as np


# Create new pandas DataFrame new_measures = pd.DataFrame()
# Selecting the mean/center values and the standard deviation of the sample mean = 65 standard_deviation = 2
# Generating the sample new_measures['Height'] = np.random.normal(mean, standard_deviation, 10000)
Python으로 배우는 데이터 프라이버시와 익명화

정규분포에서 샘플 생성

# Draw histogram to see the resulting heights distribution
new_measures['Height'].hist(bins=50)

결과 데이터의 히스토그램

Python으로 배우는 데이터 프라이버시와 익명화

scikit-learn으로 데이터셋 생성

Scikit-learn은 다음 작업용 데이터셋을 쉽게 생성합니다:

  • 분류
  • 클러스터링
  • 회귀
Python으로 배우는 데이터 프라이버시와 익명화

분류·클러스터링용 합성 데이터

make_classification()

  • 정규분포 클러스터를 생성합니다
  • 상관된/비정보(features)를 만들 수 있습니다

make_blobs()

  • 클러스터 중심과 표준편차를 더 세밀하게 제어
Python으로 배우는 데이터 프라이버시와 익명화

분류용 합성 데이터

# Import make_classification from sklearn datasets module
from sklearn.datasets import make_classification


# Generate the samples and their labels x, y = make_classification(n_samples=1000,
n_classes=2,
n_informative=2,
n_features=4,
n_clusters_per_class=2,
class_sep=1)
Python으로 배우는 데이터 프라이버시와 익명화

분류용 합성 데이터

# See the generated data and labels
print(x.shape)
print(y.shape)
print(x)
(1000, 4)
(1000,)
[[ 1.22914870e+00 -2.62386795e+00  2.25878743e+00  2.55377055e+00]
 [-1.10279812e+00 -1.15816087e+00  1.55571279e+00  7.80565898e-02]
 [ 2.65581977e-03 -2.33278818e+00  2.37837858e+00  1.57533194e+00]
 ...
 [ 4.51006972e-01  7.53435745e-01 -9.21597108e-01 -2.20659747e-01]
 [ 5.31925876e-01  7.42210504e-01 -9.37625248e-01 -1.61488855e-01]
 [ 1.62862108e+00 -2.72435345e+00  2.22562940e+00  2.87628246e+00]]
Python으로 배우는 데이터 프라이버시와 익명화

분류용 합성 데이터

생성된 2클래스 데이터셋의 데이터 포인트 플롯

Python으로 배우는 데이터 프라이버시와 익명화

분류용 합성 데이터

class_sep 값에 따른 생성 데이터셋과 데이터 포인트 3개 플롯. 왼쪽은 매우 가까움, 오른쪽은 많이 분리됨

Python으로 배우는 데이터 프라이버시와 익명화

클러스터링용 합성 데이터

# Import the datasets module for generating clustering datasets
from sklearn.datasets import make_blobs


# Specify a value for standard deviation standard_deviation = 1.5
# Generate the data and labels of the dataset x, labels = make_blobs(n_features=3, centers=4, cluster_std=standard_deviation)
# See the shape of the generated data print(x.shape)
(100, 3)
Python으로 배우는 데이터 프라이버시와 익명화

클러스터링용 합성 데이터

결과 클러스터링 데이터 포인트 플롯: 클러스터별 색상, 4개 중심-4개 클러스터

Python으로 배우는 데이터 프라이버시와 익명화

클러스터링용 합성 데이터

표준편차가 생성 데이터 포인트에 미치는 영향 3개 플롯. 왼쪽은 중심에 밀집, 오른쪽은 크게 분산

Python으로 배우는 데이터 프라이버시와 익명화

연습해 봅시다!

Python으로 배우는 데이터 프라이버시와 익명화

Preparing Video For Download...