概要
「パターン認識と機械学習 上 (PRML)」 の「1.1 例: 多項式曲線フィッティング」に記載されている内容を Python で再現したコードになります。 書籍に記載されている説明は省略しているので、PRML と合わせて読むことが前提の記事です。
問題設定
の多項式曲線 (Polynomial Curve) による近似 (fitting) を考えます。
訓練集合を作成する
- N: サンプル数
- 入力データ集合 (input data set):
- 目標データ集合 (target data set):
今回、訓練データ集合、テストデータ集合を以下のように作成します。
- 訓練データ集合
- 入力データ集合は、 から等間隔に10個選ぶ。
- 目標データ集合は、入力データ集合の各点の の値に正規分布に従うノイズを加えた値とする。
- テストデータ集合
- 入力データ集合は、 から等間隔に100個選ぶ。
- 目標データ集合は、入力データ集合の各点の の値に正規分布に従うノイズを加えた値とする。
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
np.random.seed(0)
def make_dataset(n, noise=False):
x = np.linspace(0, 1, n)
y = np.sin(2 * np.pi * x)
if noise:
y += np.random.normal(0, 0.2, n)
return x, y
# 訓練集合、テスト集合を作成する。
x_train, y_train = make_dataset(10, noise=True)
x_test, y_test = make_dataset(100, noise=True)
x, y = make_dataset(100)
# 描画する。
fig, ax = plt.subplots()
ax.grid()
ax.scatter(x_train, y_train, s=50, fc="none", ec="b", label="train")
ax.plot(x, y, "g", label="$\sin(2 \pi x)$")
ax.legend()
plt.show()

多項式曲線フィッティング
次の多項式曲線で近似します。
- は係数が である の関数であることを意味します。
- : 多項式曲線の次数 (degree)
- : 多項式曲線の係数
損失関数は二乗誤差とします。 は微分した際に式を簡潔にするためについています。
この損失関数を最小化する を求めて、 を係数とした多項式曲線 で近似します。
のときの のグラフを描画してみます。
from sklearn.linear_model import LinearRegression
def to_polynominal_feature(degree, x):
return np.hstack([x ** i for i in range(degree + 1)])
fig = plt.figure(figsize=(10, 10))
fig.subplots_adjust(hspace=0.4)
for i, degree in enumerate(range(10)):
# reshape(-1, 1) で (サンプル数, 特徴量の次元数) という2次元配列にする。
X_train = x_train.reshape(-1, 1)
X = x.reshape(-1, 1)
# 特徴量を生成する。[x] -> [x^0, x^1, ..., x^m]
X_train = to_polynominal_feature(degree, X_train)
X = to_polynominal_feature(degree, X)
# 学習する。
model = LinearRegression(fit_intercept=False).fit(X_train, y_train)
# 推論する。
y_pred = model.predict(X)
# 描画する。
ax = fig.add_subplot(4, 3, i + 1)
ax.grid()
ax.scatter(x_train, y_train, s=50, fc="none", ec="b", label="train")
ax.plot(x, y, "g", label="$\sin(2 \pi x)$")
ax.plot(x, y_pred, c="r", label="prediction")
ax.set_title(f"$M$={degree}")
ax.legend(loc=(1.05, 0))
plt.show()

- の場合は、訓練データへの当てはまりが悪く、 の近似には表現力が足りていません
- の場合は、訓練データ、テストデータともに当てはまりがよく、 を上手く近似できています
- の場合は、訓練データへの当てはまりは良いが、テストデータへの当てはまりは悪く、過学習しています
平均平方二乗誤差 (RMSE) を確認する
どのくらい近似できているかを数値的にも評価しましょう。 評価には、平均平方二乗誤差 (RMSE) を使用します。
先程の二乗誤差と比べると、以下の利点があります。
- 平均をとっているため、平均平方二乗誤差の値がサンプル数 に左右されない
- 平方根をとっているため、目標変数 と単位が同じ
def rmse(y_true, y_pred):
return np.sqrt(np.mean((y_true - y_pred) ** 2))
errors = []
for degree in range(10):
# reshape(-1, 1) で (サンプル数, 特徴量の次元数) という2次元配列にする。
X_train = x_train.reshape(-1, 1)
X_test = x_test.reshape(-1, 1)
# 特徴量を生成する。[x] -> [x^0, x^1, ..., x^m]
X_train = to_polynominal_feature(degree, X_train)
X_test = to_polynominal_feature(degree, X_test)
# 学習する。
model = LinearRegression(fit_intercept=False).fit(X_train, y_train)
# 推論する。
y_train_pred = model.predict(X_train)
y_test_pred = model.predict(X_test)
# RMSE を計算する。
train_rmse = rmse(y_train, y_train_pred)
test_rmse = rmse(y_test, y_test_pred)
errors.append(
{"M": degree, "train": train_rmse, "test": test_rmse, "coefficient": model.coef_,}
)
errors = pd.DataFrame(errors)
# 描画する。
fig, ax = plt.subplots()
ax.grid()
ax.plot(errors["M"], errors["train"], "bo-", ms=10, mfc="none", label="train")
ax.plot(errors["M"], errors["test"], "ro-", ms=10, mfc="none", label="test")
ax.set_xlabel("M")
ax.set_ylabel("RMSE")
ax.legend()
plt.show()

- は訓練誤差が大きく、 の近似には表現力が足りていません
- は訓練誤差、テスト誤差ともに低く、 を上手く近似できています
- は訓練誤差は低いが、テスト誤差は大きくなり、過学習しています
係数を確認する
各次数における学習によって得られた係数 の値を確認します。
coefs = []
index = []
for x in errors.itertuples():
coefs.append({f"$w_{k}$": v for k, v in enumerate(x.coefficient)})
index.append(f"$M={x.M}$")
pd.options.display.float_format = "{:.2f}".format
pd.DataFrame(coefs, index=index).fillna("")
0.15 | ||||||||||
0.98 | -1.66 | |||||||||
0.97 | -1.64 | -0.02 | ||||||||
0.18 | 11.39 | -34.35 | 22.89 | |||||||
0.24 | 9.31 | -23.83 | 6.01 | 8.44 | ||||||
0.34 | -0.38 | 56.92 | -222.22 | 270.05 | -104.64 | |||||
0.36 | -6.09 | 127.41 | -525.66 | 856.42 | -626.23 | 173.86 | ||||
0.36 | 3.13 | -24.81 | 377.03 | -1695.99 | 3095.93 | -2527.33 | 771.77 | |||
0.35 | 33.28 | -632.79 | 4962.76 | -18995.39 | 39111.60 | -44545.62 | 26497.25 | -6431.37 | ||
0.35 | -90.27 | 2220.86 | -20741.11 | 101757.97 | -289865.39 | 494134.29 | -495988.80 | 270001.76 | -61429.58 |
次数が大きいほど、係数が大きな値をとる傾向があることがわかります。
サンプル数を変えたときの挙動を確認する
次数を に固定して、サンプル数を変化させたときの挙動を確認します。
fig = plt.figure(figsize=(7, 7))
fig.subplots_adjust(hspace=0.4)
for i, n in enumerate([10, 15, 25, 50]):
# 訓練集合、テスト集合を作成する。
x_train, y_train = make_dataset(n, True)
x, y = make_dataset(100)
# reshape(-1, 1) で (サンプル数, 特徴量の次元数) という2次元配列にする。
X_train = x_train.reshape(-1, 1)
X = x.reshape(-1, 1)
# 特徴量を生成する。[x] -> [x^0, x^1, ..., x^m]
X_train = to_polynominal_feature(9, X_train)
X = to_polynominal_feature(9, X)
# 学習する。
model = LinearRegression(fit_intercept=False).fit(X_train, y_train)
# 推論する。
y_pred = model.predict(X)
# 描画する。
ax = fig.add_subplot(2, 2, i + 1)
ax.grid()
ax.scatter(x_train, y_train, s=30, fc="none", ec="b", label="train")
ax.plot(x, y, "g", label="$\sin(2 \pi x)$")
ax.plot(x, y_pred, c="r", label="prediction")
ax.set_title(f"$N$={n}")
ax.legend(loc=(1.05, 0))
plt.show()

次数が多くてもサンプル数が十分大きい場合は過学習しないことがわかります。
正規化項の導入
最小化対象の二乗誤差に正規化項 を追加します。 は係数ベクトル のノルムで、各係数が大きいほど、ノルムも大きくなります。 を最小化することは、二乗誤差を小さくしつつ、係数も小さくなるような を探すことになります。
は正規化項が課す制約の強さを表すパラメータです。大きいほど制約が強くなり、小さいほど制約が弱くなります。 とすると、正則化項がない二乗誤差になります。
を の範囲で変えたときの挙動を確認します。
from sklearn.linear_model import Ridge
np.random.seed(0)
degree = 9
# 訓練集合、テスト集合を作成する。
x_train, y_train = make_dataset(10, noise=True)
x_test, y_test = make_dataset(100, noise=True)
x, y = make_dataset(100)
log_ls = np.linspace(-50, 0, 6)
errors = []
fig = plt.figure(figsize=(10, 6))
fig.subplots_adjust(hspace=0.4)
for i, log_l in enumerate(log_ls):
# reshape(-1, 1) で (サンプル数, 特徴量の次元数) という2次元配列にする。
X_train = x_train.reshape(-1, 1)
X_test = x_test.reshape(-1, 1)
X = x.reshape(-1, 1)
# 特徴量を生成する。[x] -> [x^0, x^1, ..., x^m]
X_train = to_polynominal_feature(degree, X_train)
X_test = to_polynominal_feature(degree, X_test)
X = to_polynominal_feature(degree, X)
# 学習する。
model = Ridge(alpha=np.exp(log_l), fit_intercept=False).fit(X_train, y_train)
# 推論する。
y_train_pred = model.predict(X_train)
y_test_pred = model.predict(X_test)
y_pred = model.predict(X)
# RMSE を計算する。
train_rmse = rmse(y_train, y_train_pred)
test_rmse = rmse(y_test, y_test_pred)
# 描画する。
ax = fig.add_subplot(2, 3, i + 1)
ax.grid()
ax.scatter(x_train, y_train, s=50, fc="none", ec="b", label="train")
ax.plot(x, y, "g", label="$\sin(2 \pi x)$")
ax.plot(x, y_pred, c="r", label="prediction")
ax.set_title(f"$\log \lambda$={log_l}")
errors.append(
{
"log_lambda": log_l,
"train": train_rmse,
"test": test_rmse,
"coefficient": model.coef_,
}
)
errors = pd.DataFrame(errors)
ax.legend(loc=(1.05, 0))
plt.show()

- では、制約が弱すぎるため、過学習の傾向が見られます。
- ではそれなりに近似できています。
- では、制約が強すぎるため、 を表現できなくなっています。
coefs = []
index = []
for x in errors.itertuples():
coefs.append({f"$w_{k}$": v for k, v in enumerate(x.coefficient)})
index.append(fr"$\log \lambda={x.log_lambda}$")
pd.options.display.float_format = "{:.2f}".format
pd.DataFrame(coefs, index=index).fillna("")
0.35 | -90.49 | 2226.13 | -20788.42 | 101979.48 | -290466.89 | 495116.24 | -496938.63 | 270503.06 | -61540.74 | |
0.35 | -90.49 | 2226.13 | -20788.42 | 101979.48 | -290466.89 | 495116.24 | -496938.63 | 270503.06 | -61540.74 | |
0.35 | -30.28 | 839.32 | -8336.90 | 43671.44 | -132098.74 | 236528.98 | -246766.87 | 138448.10 | -32255.31 | |
0.36 | -0.75 | 41.34 | -40.01 | -385.48 | 802.71 | -197.68 | -606.72 | 460.80 | -74.48 | |
0.27 | 6.93 | -8.90 | -16.09 | 2.98 | 12.66 | 10.40 | 2.98 | -3.87 | -7.29 | |
0.52 | -0.41 | -0.47 | -0.33 | -0.18 | -0.05 | 0.05 | 0.12 | 0.18 | 0.23 |
制約を強くするほど、係数が小さくなっていることが確認できました。 を変化させたときの平均平方二乗誤差の値も確認します。
# 描画する。
fig, ax = plt.subplots()
ax.grid()
ax.plot(errors["log_lambda"], errors["train"], "bo-", ms=10, mfc="none", label="train")
ax.plot(errors["log_lambda"], errors["test"], "ro-", ms=10, mfc="none", label="test")
ax.set_xlabel("$\log \lambda$")
ax.set_ylabel("RMSE")
ax.legend()
plt.show()

- では、訓練誤差は低いが、テスト誤差は大きくなり、過学習しています
- ではそれなりに近似できています
- では、訓練誤差が大きく、 の近似には表現力が足りていません
コメント