1 분 소요

원본 사이트: https://scikit-learn.org/stable/auto_examples/linear_model/plot_ols_ridge_variance.html


최소 자승법 및 Ridge 회귀 분산

선형 회귀는 차원 상의 점들을 가능한 한 잘 나타내는 선을 구합니다. 이러한 이유로 관측치의 노이즈는 첫 번째 그림에서 볼 수 있는 것처럼 큰 분산을 일으킬 것입니다. 모든 회귀 직선의 기울기는 관측치에서 유도된 노이즈로 인해 각 예측에 대해 상당히 다를 수 있습니다.

Ridge 회귀는 기본적으로 최소 제곱 함수에 제약을 주어 정규화한 버전입니다. 페널티를 적용하면 회귀 계수 값이 축소됩니다. 각 차원의 데이터 포인트가 적음에도 불구하고 표준 선형 회귀 분석에 비해 예측의 기울기가 훨씬 안정적이고 선 자체의 분산이 크게 감소합니다.

image.png

image.png

# Code source: Gaël Varoquaux
# Modified for documentation by Jaques Grobler
# License: BSD 3 clause

import numpy as np
import matplotlib.pyplot as plt

from sklearn import linear_model

X_train = np.c_[0.5, 1].T   # shape: (2, 1)
y_train = [0.5, 1]          
X_test = np.c_[0, 2].T      # shape: (2, 1)

np.random.seed(0)           

classifiers = dict(
    ols=linear_model.LinearRegression(), ridge=linear_model.Ridge(alpha=0.1)
)

for name, clf in classifiers.items():
    fig, ax = plt.subplots(figsize=(4, 3))

    for _ in range(6):
        this_X = 0.1 * np.random.normal(size=(2, 1)) + X_train  # X_train에 잡음 추가
        clf.fit(this_X, y_train)                                # 분류기 학습

        ax.plot(X_test, clf.predict(X_test), color="gray")
        ax.scatter(this_X, y_train, s=3, c="gray", marker="o", zorder=10)

    clf.fit(X_train, y_train)
    ax.plot(X_test, clf.predict(X_test), linewidth=2, color="blue")
    ax.scatter(X_train, y_train, s=30, c="red", marker="+", zorder=10)

    ax.set_title(name)
    ax.set_xlim(0, 2)
    ax.set_ylim((0, 1.6))
    ax.set_xlabel("X")
    ax.set_ylabel("y")

    fig.tight_layout()

plt.show()
<Figure size 288x216 with 1 Axes>
<Figure size 288x216 with 1 Axes>

ⓒ 2007 - 2021, scikit-learn developers (BSD License). Show this page source

댓글남기기