Lasso和Elastic Net(弹性网络)

翻译者:@Loopy
校验者:@barrycg

使用坐标下降实现Lasso和Elastic Net(弹性网络)(L1和L2罚项)。

回归系数可以被强制设定为正。

  1. from itertools import cycle
  2. import numpy as np
  3. import matplotlib.pyplot as plt
  4. from sklearn.linear_model import lasso_path, enet_path
  5. from sklearn import datasets
  1. diabetes = datasets.load_diabetes()
  2. X = diabetes.data
  3. y = diabetes.target
  4. X /= X.std(axis=0) # 标准化数据(使得l1_ratio参数更容易被设置)
  1. # 计算路径
  2. eps = 5e-3 # 它越小,路径就越长
  3. print("使用lasso计算正则化路径...")
  4. alphas_lasso, coefs_lasso, _ = lasso_path(X, y, eps, fit_intercept=False)
  5. print("使用系数强制为正的lasso计算正则化路径...")
  6. alphas_positive_lasso, coefs_positive_lasso, _ = lasso_path(
  7. X, y, eps, positive=True, fit_intercept=False)
  8. print("使用Elastic Net计算正则化路径...")
  9. alphas_enet, coefs_enet, _ = enet_path(
  10. X, y, eps=eps, l1_ratio=0.8, fit_intercept=False)
  11. print("使用系数强制为正的Elastic Net计算正则化路径...")
  12. alphas_positive_enet, coefs_positive_enet, _ = enet_path(
  13. X, y, eps=eps, l1_ratio=0.8, positive=True, fit_intercept=False)
  1. 使用lasso计算正则化路径...
  2. 使用系数强制为正的lasso计算正则化路径...
  3. 使用Elastic Net计算正则化路径...
  4. 使用系数强制为正的Elastic Net计算正则化路径...
  1. # 输出结果
  2. plt.figure(1)
  3. colors = cycle(['b', 'r', 'g', 'c', 'k'])
  4. neg_log_alphas_lasso = -np.log10(alphas_lasso)
  5. neg_log_alphas_enet = -np.log10(alphas_enet)
  6. for coef_l, coef_e, c in zip(coefs_lasso, coefs_enet, colors):
  7. l1 = plt.plot(neg_log_alphas_lasso, coef_l, c=c)
  8. l2 = plt.plot(neg_log_alphas_enet, coef_e, linestyle='--', c=c)
  9. plt.xlabel('-Log(alpha)')
  10. plt.ylabel('系数')
  11. plt.title('Lasso和Elastic-Net的路径')
  12. plt.legend((l1[-1], l2[-1]), ('Lasso', 'Elastic-Net'), loc='lower left')
  13. plt.axis('tight')
  1. (-1.8715612353951188,
  2. 0.7661727741441225,
  3. -14.885251328819376,
  4. 28.52127146246762)

png

  1. plt.figure(2)
  2. neg_log_alphas_positive_lasso = -np.log10(alphas_positive_lasso)
  3. for coef_l, coef_pl, c in zip(coefs_lasso, coefs_positive_lasso, colors):
  4. l1 = plt.plot(neg_log_alphas_lasso, coef_l, c=c)
  5. l2 = plt.plot(neg_log_alphas_positive_lasso, coef_pl, linestyle='--', c=c)
  6. plt.xlabel('-Log(alpha)')
  7. plt.ylabel('系数')
  8. plt.title('Lasso和系数强制为正的lasso的路径')
  9. plt.legend((l1[-1], l2[-1]), ('Lasso', '系数强制为正的Lasso'), loc='lower left')
  10. plt.axis('tight')
  1. (-1.7698057217366594,
  2. 0.7613272734937198,
  3. -14.945497919817987,
  4. 29.786449873438457)

png

  1. plt.figure(3)
  2. neg_log_alphas_positive_enet = -np.log10(alphas_positive_enet)
  3. for (coef_e, coef_pe, c) in zip(coefs_enet, coefs_positive_enet, colors):
  4. l1 = plt.plot(neg_log_alphas_enet, coef_e, c=c)
  5. l2 = plt.plot(neg_log_alphas_positive_enet, coef_pe, linestyle='--', c=c)
  6. plt.xlabel('-Log(alpha)')
  7. plt.ylabel('系数')
  8. plt.title('Elastic-Net和系数强制为正的Elastic-Net的路径')
  9. plt.legend((l1[-1], l2[-1]), ('Elastic-Net', '系数强制为正的Elastic-Net'),
  10. loc='lower left')
  11. plt.axis('tight')
  12. plt.show()

png