にほんごのれんしゅう

日本語として伝えるための訓練を兼ねたテクログ

Elastic-Netをロジスティック回帰の代わりに使いたい

  • 次元圧縮という視点においてElasticNetは有用である。
  • それは、min[1/2NΣ(y_i - β_0 - x_iβ)^2 + λPα(β) ]というElasticNetの方程式において、Pαのパラメータを調整することで次元圧縮が可能なことに起因している
  • Pα(β) = (1 - α)1/2||β||^2  + α||β||という式があり、α||β||に対してある種の閾値を設けることでラッソー回帰と同様の次元削減効果を得ることが可能になっている。ただし、次元の縮小までいかなくとも(1 - α)1/2||β||^2部分において、より安定した係数になるように縮小されている。[1]
  • 期待できる効果としては、予想因子が多すぎる場合に減らすことやサンプリングが不十分な場合に、積極的に次元削減を行い、モデルとしての精度を高めることなどが考えられる。
  • scikit-learnに糖尿病の予測判定にElastic-NetをPythonで用いた例があったので、実際に試してみた。

import numpy as np

import matplotlib.pyplot as plt

from sklearn.linear_model import lasso_path, enet_path

from sklearn import datasets

diabetes = datasets.load_diabetes()

X = diabetes.data

y = diabetes.target

X /= X.std(axis=0)  # Standardize data (easier to set the l1_ratio parameter)

eps = 5e-3  # the smaller it is the longer is the path

eps2 = 0.5  # ラッソー回帰の閾値のようなものと理解している

print("Computing regularization path using the lasso...")

alphas_lasso, coefs_lasso, _ = lasso_path(X, y, eps, fit_intercept=False)

print("Computing regularization path using the positive lasso...")

alphas_positive_lasso, coefs_positive_lasso, _ = lasso_path(X, y, eps, positive=True, fit_intercept=False)

print("Computing regularization path using the elastic net...")

# この例ではラッソーの比を0.8、リッジ回帰の比を0.2に設定している

alphas_enet, coefs_enet, _ = enet_path(X, y, eps=eps, l1_ratio=0.8, fit_intercept=False)

print("Computing regularization path using the positve elastic net...")

alphas_positive_enet, coefs_positive_enet, _ = enet_path(X, y, eps=eps, l1_ratio=0.8, positive=True, fit_intercept=False)

# Display results

plt.figure(1)

ax = plt.gca()

ax.set_color_cycle(2 * ['b', 'r', 'g', 'c', 'k'])

l1 = plt.plot(-np.log10(alphas_lasso), coefs_lasso.T)

l2 = plt.plot(-np.log10(alphas_enet), coefs_enet.T, linestyle='--')

plt.xlabel('-Log(alpha)')

plt.ylabel('coefficients')

plt.title('Lasso and Elastic-Net Paths')

plt.legend((l1[-1], l2[-1]), ('Lasso', 'Elastic-Net'), loc='lower left')

plt.axis('tight')

plt.show()

f:id:catindog:20150921145248p:plain

  • 画像はmac osxでX11をポーフトフォワードしたものである。
  • 参考文献
    • [1]みんなのR p299