【问题标题】:Why do I not get the same coefficients as linear regression when my elastic net regression model has alpha = 0 & l1_ratio =0当我的弹性网络回归模型具有 alpha = 0 和 l1_ratio =0 时,为什么我没有得到与线性回归相同的系数
【发布时间】:2020-09-17 00:49:43
【问题描述】:

我正在使用sklearn.linear_model.ElasticNet 模型,我试图通过设置alph = 0l1_ratio = 0 重新创建线性回归,但我得到的系数非常不同。有谁知道为什么?我认为使用这些参数应该可以得到常规的线性回归系数。

我的代码在下面,附件是输出的图像。

from sklearn.linear_model import ElasticNet
from sklearn.linear_model import LinearRegression

linear_model = LinearRegression().fit(xtrain, ytrain)
linear_coeffs = linear_model.coef_

enet_model = ElasticNet(alpha = 0, l1_ratio=0).fit(xtrain,ytrain)
enet_coef = enet_model.coef_

linear_model = LinearRegression().fit(xtrain, ytrain)
linear_coef = linear_model.coef_

sum_squares_coeff_diff = (np.square(enet_coef))/(np.square(linear_coef))

for i in range(len(enet_coef)):
    print("enet_coef, linear_coef, ratio:           %f           %f           %f" %(enet_coef[i],linear_coef[i],sum_squares_coeff_diff[i]))

print('ratio enet_reg sum squares coef / linear reg sum squares coef:   %f' %(np.sum(np.square(enet_coef))/np.sum(np.square(linear_coef))))

(len(enet_coef),len(linear_coef)))

enter image description here

【问题讨论】:

标签: python scikit-learn regression


【解决方案1】:

当我重现此代码(在 Google Colab 中)时,我收到以下警告。

/usr/local/lib/python3.6/dist-packages/ipykernel_launcher.py:9: UserWarning: With alpha=0, this algorithm does not converge well. You are advised to use the LinearRegression estimator

文档 (https://github.com/scikit-learn/scikit-learn/blob/0fb307bf3/sklearn/linear_model/_coordinate_descent.py#L557) 说

Currently, l1_ratio <= 0.01 is not reliable,
    unless you supply your own sequence of alpha.

UPD:底层优化算法是坐标下降。 L1 惩罚使其对每个条目的符号敏感。我想这就是没有适当收敛的原因。

【讨论】:

  • 感谢 kate-melnykova 的回答。我在 Google Colab 中尝试了同样的事情,也收到了警告信息。你知道一个非常小的 alpha 是否会遇到同样的问题吗?当我应用 alpha = .0001 和 l1_ratio = .9 和类似的值时,我得到了 sum(squared(enet_coef)) > sum(squared(linear_coef))。这与我的直觉相反,因为我认为任何 alpha 和任何 l1_ratio 都应该总是添加一些收缩而不是添加。
  • 嗯...我不知道最小化问题的最小可接受值。 sum(squared(enet_coef)) &gt; sum(squared(linear_coef)) 实际上是正确的。请注意,线性回归会找到方程的最小 2 范数解(假设没有噪声)。 L1 惩罚使 L1 范数(abs 值之和)更小,这反过来又促进了重构向量的稀疏性。
  • 换句话说,在欠采样的无噪声情况下,线性回归通常会返回最小的 2 范数解——然后得到的不等式才有意义。 L1 正则化使 L1 范数变小,用于找到大多数条目为零且 L1 的解。
猜你喜欢
  • 1970-01-01
  • 2019-07-12
  • 1970-01-01
  • 2018-08-18
  • 2020-01-28
  • 2016-11-21
  • 2016-06-07
  • 2012-04-09
  • 2021-07-10
相关资源
最近更新 更多