머신러닝에서 특징 스케일링

Beginner

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

소개

피처 스케일링은 많은 머신 러닝 알고리즘에 중요한 전처리 단계입니다. 각 피처를 표준 편차가 1 이고 평균이 0 이 되도록 재조정하는 것을 포함합니다. 이 실습에서는 scikit-learn 라이브러리를 사용하여 파이썬에서 피처 스케일링의 중요성과 머신 러닝 모델에 미치는 영향을 살펴볼 것입니다.

VM 팁

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

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

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

데이터 로드 및 준비

scikit-learn 에서 와인 데이터셋을 로드하고 학습 및 테스트 데이터셋으로 분할합니다. 또한 scikit-learn 전처리 모듈의 StandardScaler 를 사용하여 학습 데이터셋의 특징을 스케일링합니다.

from sklearn.datasets import load_wine
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler

X, y = load_wine(return_X_y=True, as_frame=True)
scaler = StandardScaler().set_output(transform="pandas")

X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.30, random_state=42
)
scaled_X_train = scaler.fit_transform(X_train)

k-최근접 이웃 모델에 대한 스케일링 효과

와인 데이터셋에서 두 개의 특징을 추출하여 K-최근접 이웃 분류기를 학습합니다. 스케일링된 데이터와 스케일링되지 않은 데이터를 사용하여 분류기의 결정 경계를 시각화합니다.

import matplotlib.pyplot as plt
from sklearn.inspection import DecisionBoundaryDisplay
from sklearn.neighbors import KNeighborsClassifier

X_plot = X[["proline", "hue"]]
X_plot_scaled = scaler.fit_transform(X_plot)
clf = KNeighborsClassifier(n_neighbors=20)

def fit_and_plot_model(X_plot, y, clf, ax):
    clf.fit(X_plot, y)
    disp = DecisionBoundaryDisplay.from_estimator(
        clf,
        X_plot,
        response_method="predict",
        alpha=0.5,
        ax=ax,
    )
    disp.ax_.scatter(X_plot["proline"], X_plot["hue"], c=y, s=20, edgecolor="k")
    disp.ax_.set_xlim((X_plot["proline"].min(), X_plot["proline"].max()))
    disp.ax_.set_ylim((X_plot["hue"].min(), X_plot["hue"].max()))
    return disp.ax_

fig, (ax1, ax2) = plt.subplots(ncols=2, figsize=(12, 6))

fit_and_plot_model(X_plot, y, clf, ax1)
ax1.set_title("스케일링 없이 KNN")

fit_and_plot_model(X_plot_scaled, y, clf, ax2)
ax2.set_xlabel("스케일링된 proline")
ax2.set_ylabel("스케일링된 hue")
_ = ax2.set_title("스케일링된 KNN")

주성분 분석 (PCA) 차원 축소에 대한 스케일링 효과

주성분 분석 (PCA) 을 사용하여 와인 데이터셋의 차원을 축소합니다. 스케일링되지 않은 데이터에 PCA 를 적용한 주성분과 먼저 StandardScaler 를 사용하여 데이터를 스케일링한 후 PCA 를 적용한 주성분을 비교합니다.

import pandas as pd
from sklearn.decomposition import PCA

pca = PCA(n_components=2).fit(X_train)
scaled_pca = PCA(n_components=2).fit(scaled_X_train)
X_train_transformed = pca.transform(X_train)
X_train_std_transformed = scaled_pca.transform(scaled_X_train)

first_pca_component = pd.DataFrame(
    pca.components_[0], index=X.columns, columns=["without scaling"]
)
first_pca_component["with scaling"] = scaled_pca.components_[0]
first_pca_component.plot.bar(
    title="첫 번째 주성분의 가중치", figsize=(6, 8)
)

_ = plt.tight_layout()

fig, (ax1, ax2) = plt.subplots(nrows=1, ncols=2, figsize=(10, 5))

target_classes = range(0, 3)
colors = ("blue", "red", "green")
markers = ("^", "s", "o")

for target_class, color, marker in zip(target_classes, colors, markers):
    ax1.scatter(
        x=X_train_transformed[y_train == target_class, 0],
        y=X_train_transformed[y_train == target_class, 1],
        color=color,
        label=f"class {target_class}",
        alpha=0.5,
        marker=marker,
    )

    ax2.scatter(
        x=X_train_std_transformed[y_train == target_class, 0],
        y=X_train_std_transformed[y_train == target_class, 1],
        color=color,
        label=f"class {target_class}",
        alpha=0.5,
        marker=marker,
    )

ax1.set_title("스케일링되지 않은 학습 데이터셋의 PCA 후")
ax2.set_title("표준화된 학습 데이터셋의 PCA 후")

for ax in (ax1, ax2):
    ax.set_xlabel("첫 번째 주성분")
    ax.set_ylabel("두 번째 주성분")
    ax.legend(loc="upper right")
    ax.grid()

_ = plt.tight_layout()

모델 성능에 미치는 스케일링의 효과

PCA 로 차원 축소된 데이터를 사용하여 로지스틱 회귀 모델을 학습하고, 특징 스케일링이 모델 성능에 미치는 영향을 평가합니다. 스케일링된 특징과 스케일링되지 않은 특징을 사용한 모델의 성능을 비교합니다.

import numpy as np
from sklearn.pipeline import make_pipeline
from sklearn.linear_model import LogisticRegressionCV
from sklearn.metrics import accuracy_score
from sklearn.metrics import log_loss

Cs = np.logspace(-5, 5, 20)

unscaled_clf = make_pipeline(pca, LogisticRegressionCV(Cs=Cs))
unscaled_clf.fit(X_train, y_train)

scaled_clf = make_pipeline(scaler, pca, LogisticRegressionCV(Cs=Cs))
scaled_clf.fit(X_train, y_train)

y_pred = unscaled_clf.predict(X_test)
y_pred_scaled = scaled_clf.predict(X_test)
y_proba = unscaled_clf.predict_proba(X_test)
y_proba_scaled = scaled_clf.predict_proba(X_test)

print("스케일링되지 않은 PCA 에 대한 테스트 정확도")
print(f"{accuracy_score(y_test, y_pred):.2%}\n")
print("PCA 와 함께 표준화된 데이터에 대한 테스트 정확도")
print(f"{accuracy_score(y_test, y_pred_scaled):.2%}\n")
print("스케일링되지 않은 PCA 에 대한 로그 손실")
print(f"{log_loss(y_test, y_proba):.3}\n")
print("PCA 와 함께 표준화된 데이터에 대한 로그 손실")
print(f"{log_loss(y_test, y_proba_scaled):.3}")

요약

이 실험에서 우리는 머신 러닝에서 특징 스케일링의 중요성과 모델 성능에 미치는 영향을 배웠습니다. K-최근접 이웃 모델과 PCA 차원 축소에 특징 스케일링이 미치는 영향을 탐구했습니다. 또한 PCA 로 차원 축소된 데이터로 로지스틱 회귀 모델을 학습하여 특징 스케일링이 모델 성능에 미치는 영향을 평가했습니다. 차원 축소 전에 특징을 스케일링하면 성분의 크기가 동일한 순서가 되고 클래스의 분리성이 향상되어 모델 성능이 향상된다는 것을 발견했습니다.