科学の箱

科学・IT・登山の話題

機械学習

線形回帰とリッジ回帰を比較する

投稿日:

リッジ回帰はL2ノルムの正則項を導入することで、過学習を防ぐ。この場合過学習を防ぐとは、重みを0の方向に動かすという意味である。実際に線形回帰とリッジ回帰を比べてみて、どのようにモデルが変わるかを確認する。

線形回帰

アイスクリームの売り上げを考えて、1~37度に対してランダムの売り上げデータを生成する。

import matplotlib.pyplot as plt
import seaborn as sns;
import numpy as np
%matplotlib inline
import random

rng = np.random.RandomState(1)

x = 37 * rng.rand(50) + 1 # 1度から3度
y = 500 * x - 5000 * rng.randn(50) # 売り上げデータはおよそ気温の500倍想定、ただしノイズを組みこむ

 

次に線形回帰を行う。

# 線形回帰
from sklearn.linear_model import LinearRegression
model_lr = LinearRegression(fit_intercept=True)

model_lr.fit(x[:, np.newaxis], y)

xfit = np.linspace(0, 50, 1000)
yfit_lr = model.predict(xfit[:, np.newaxis])

plt.scatter(x, y)
plt.plot(xfit, yfit_lr,color="red", label="LinearRegression");

 

リッジ回帰

リッジ回帰はRidgeを用いる。

sklearn.linear_model.Ridge — scikit-learn 0.21.3 documentation

Ridge(alpha=1.0, fit_intercept=True, normalize=False, copy_X=True, max_iter=None, tol=0.001, solver=’auto’, random_state=None)

もっとも重要なのがalphaである。alphaは正則項の強さを指定する。

# リッジ回帰
from sklearn.linear_model import Ridge
model_r = Ridge(alpha=1000)
model_r.fit(x[:, np.newaxis], y)

yfit_r = model_r.predict(xfit[:, np.newaxis])

plt.scatter(x, y)
plt.plot(xfit, yfit_lr,color="red", label="LinearRegression");
plt.plot(xfit, yfit_r, color="yellow", label="Ridge alpha=1000")
plt.legend()

 

リッジ回帰のモデルは重みづけが小さくなっていることがわかる。

メタ情報

inarticle



メタ情報

inarticle



-機械学習

執筆者:


comment

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

関連記事

no image

Mahout環境構築

Mahoutのシステム要件を確認する。 Java 1.6.x or greater. Maven 3.x to build the source code. CPU, Disk and Memory …

no image

データサイエンス超入門

シンプソンのパラドックス レコメンドエンジン コンテンツベースフィルタリング 強調 アイテム ユーザー ビジネスにおけるデータ分析の手順 課題→ビジネスケース→仮説→分析→用途 データ分析の手順 デー …

no image

スパムメールの判別に使うベイズ定理についてまとめる

分類問題 スパムメールを判別するような問題は一般的に「分類問題」として機械学習では取り扱う。分類問題とはいまある「物」や「発生した事柄」を確率的に分類する。この「確率的」という言葉がみそであり、固定さ …

no image

回帰と分類の違い

回帰と分類は両方とも”予測”問題である。予測とは従属変数から目的変数を明らかにする。この目的変数の種類により回帰と分類に分けられる。 回帰では連続した数値を予測する。例えば株価 …

no image

dataanalysis-002-week6

prediction study motivation 手順 データの選択 エラーの測定 デザイン データの分割 トレーニング テスト 検証 true false positives true pos …

2019年10月
月 火 水 木 金 土 日
« 9月   11月 »
 123456
78910111213
14151617181920
21222324252627
28293031  

side bar top



アーカイブ

カテゴリー