데이터를 정규 분포로 매핑하기

Beginner

This tutorial is from open-source community. Access the source code

소개

이 실험은 PowerTransformer 를 통해 Box-Cox 및 Yeo-Johnson 변환을 사용하여 다양한 분포의 데이터를 정규 분포로 매핑하는 방법을 보여줍니다.

VM 팁

VM 시작이 완료되면 왼쪽 상단 모서리를 클릭하여 Notebook 탭으로 전환하여 연습을 위한 Jupyter Notebook에 접근합니다.

때때로 Jupyter Notebook 이 완전히 로드되기까지 몇 초 정도 기다려야 할 수 있습니다. Jupyter Notebook 의 제한으로 인해 작업의 유효성 검사를 자동화할 수 없습니다.

학습 중 문제가 발생하면 Labby 에 문의하십시오. 세션 후 피드백을 제공하면 문제를 신속하게 해결해 드리겠습니다.

라이브러리 가져오기

먼저 필요한 라이브러리를 가져와야 합니다: numpy, matplotlib, PowerTransformer, QuantileTransformer, 그리고 train_test_split 입니다.

import numpy as np
import matplotlib.pyplot as plt
from sklearn.preprocessing import PowerTransformer
from sklearn.preprocessing import QuantileTransformer
from sklearn.model_selection import train_test_split

상수 설정

샘플 수, 글꼴 크기, 그리고 구간 (bins) 의 상수를 설정합니다.

N_SAMPLES = 1000
FONT_SIZE = 6
BINS = 30

랜덤 분포 생성

로그정규, 카이제곱, 와이블, 가우시안, 균일, 이중봉 분포를 포함한 여섯 가지 서로 다른 확률 분포를 생성합니다.

rng = np.random.RandomState(304)
bc = PowerTransformer(method="box-cox")
yj = PowerTransformer(method="yeo-johnson")
qt = QuantileTransformer(n_quantiles=500, output_distribution="normal", random_state=rng)
size = (N_SAMPLES, 1)

## 로그정규 분포
X_lognormal = rng.lognormal(size=size)

## 카이제곱 분포
df = 3
X_chisq = rng.chisquare(df=df, size=size)

## 와이블 분포
a = 50
X_weibull = rng.weibull(a=a, size=size)

## 가우시안 분포
loc = 100
X_gaussian = rng.normal(loc=loc, size=size)

## 균일 분포
X_uniform = rng.uniform(low=0, high=1, size=size)

## 이중봉 분포
loc_a, loc_b = 100, 105
X_a, X_b = rng.normal(loc=loc_a, size=size), rng.normal(loc=loc_b, size=size)
X_bimodal = np.concatenate([X_a, X_b], axis=0)

플롯 생성

이제 여섯 가지 분포 각각에 대해 원본 분포와 Box-Cox, Yeo-Johnson 및 Quantile 변환을 사용한 변환된 분포를 보여주는 플롯을 생성합니다.

distributions = [
    ("Lognormal", X_lognormal),
    ("Chi-squared", X_chisq),
    ("Weibull", X_weibull),
    ("Gaussian", X_gaussian),
    ("Uniform", X_uniform),
    ("Bimodal", X_bimodal),
]

colors = ["#D81B60", "#0188FF", "#FFC107", "#B7A2FF", "#000000", "#2EC5AC"]

fig, axes = plt.subplots(nrows=8, ncols=3, figsize=plt.figaspect(2))
axes = axes.flatten()
axes_idxs = [
    (0, 3, 6, 9),
    (1, 4, 7, 10),
    (2, 5, 8, 11),
    (12, 15, 18, 21),
    (13, 16, 19, 22),
    (14, 17, 20, 23),
]
axes_list = [(axes[i], axes[j], axes[k], axes[l]) for (i, j, k, l) in axes_idxs]

for distribution, color, axes in zip(distributions, colors, axes_list):
    name, X = distribution
    X_train, X_test = train_test_split(X, test_size=0.5)

    ## 파워 변환 및 퀀타일 변환 수행
    X_trans_bc = bc.fit(X_train).transform(X_test)
    lmbda_bc = round(bc.lambdas_[0], 2)
    X_trans_yj = yj.fit(X_train).transform(X_test)
    lmbda_yj = round(yj.lambdas_[0], 2)
    X_trans_qt = qt.fit(X_train).transform(X_test)

    ax_original, ax_bc, ax_yj, ax_qt = axes

    ax_original.hist(X_train, color=color, bins=BINS)
    ax_original.set_title(name, fontsize=FONT_SIZE)
    ax_original.tick_params(axis="both", which="major", labelsize=FONT_SIZE)

    for ax, X_trans, meth_name, lmbda in zip(
        (ax_bc, ax_yj, ax_qt),
        (X_trans_bc, X_trans_yj, X_trans_qt),
        ("Box-Cox", "Yeo-Johnson", "Quantile transform"),
        (lmbda_bc, lmbda_yj, None),
    ):
        ax.hist(X_trans, color=color, bins=BINS)
        title = "After {}".format(meth_name)
        if lmbda is not None:
            title += "\n$\\lambda$ = {}".format(lmbda)
        ax.set_title(title, fontsize=FONT_SIZE)
        ax.tick_params(axis="both", which="major", labelsize=FONT_SIZE)
        ax.set_xlim([-3.5, 3.5])

plt.tight_layout()
plt.show()

요약

이 실험에서 우리는 Box-Cox 및 Yeo-Johnson 변환을 사용하여 다양한 분포에서 정규 분포로 데이터를 매핑하는 방법을 PowerTransformer 를 사용하여 배웠습니다. 또한 임의의 분포를 가우시안 분포로 강제하는 방법을 QuantileTransformer 를 사용하여 배웠습니다. 일부 변환은 특정 데이터 세트에 효과적이지 않을 수 있으므로 변환 전후에 데이터를 시각화하는 것이 중요합니다.