python - 为什么 sklearn 的套索系数不等于线性回归系数?

标签 python machine-learning scikit-learn lasso-regression

我正在尝试在我的代码中实现 sklearn 的套索。为了对其进行测试,我决定使用 alpha = 0 进行测试。根据定义,这应该产生与 LinearRegression 相同的结果,但事实并非如此。
这是代码:

import pandas as pd
from sklearn.linear_model import Lasso
from sklearn.linear_model import LinearRegression

# Don't worry about this. It is made so that we can work with the same dataset.
df = pd.read_csv('http://web.stanford.edu/~oleg2/hse/Credit.csv').dropna()
df['Asian'] = df.Ethnicity=='Asian'
df['Caucasian'] = df.Ethnicity=='Caucasian'
df['African American'] = df.Ethnicity=='African American'
df = df.drop(['Ethnicity'],axis=1).replace(['Yes','No','Male','Female',True,False],[1,0,1,0,1,0])
# End of unimportant part

ft = Lasso(alpha=0).fit(x, df.Balance)
print(ft.intercept_)
ft = LinearRegression().fit(x, df.Balance)
print(ft.intercept_)

输出:

-485.3744897927978
-480.89071679937786

coef_ 也各不相同。

我做错了什么?

最佳答案

的确,这似乎会产生不同的结果。但是,运行您的代码时,还会产生以下警告:

ft = Lasso(alpha=0).fit(X, y)
print(ft.intercept_)
ft = LinearRegression().fit(X, y)
print(ft.intercept_)

-485.3744897927984
-480.89071679937854 

UserWarning: With alpha=0, this algorithm does not converge well. You are advised to use the LinearRegression estimator

这让您知道,由于 alpha=0,这意味着我们只剩下一个普通的线性回归,算法不会很好地收敛。这就是为什么您会看到截距有所不同,并且可能是指标恶化的原因。

关于python - 为什么 sklearn 的套索系数不等于线性回归系数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64702644/

相关文章:

Python递归通过对象和子对象,打印子深度数

python - 如何在 google appengine 上使用 python 实现分页?

machine-learning - gamma和epsilon在K-L散度计算中的作用是什么?

android - 在 Android 中使用经过训练的 Scikit-learn svm 分类器

python - 带有枚举和 for 循环的代码在第一个字母上给我重复的结果

Python3.4 : Opening file with mode 'w' still gives me FileNotFound error

tensorflow - GPU 中的 Bfloat16 训练

image-processing - 深度网络框架中卷积稀疏编码的实现

python - 如何在Scikit-Learn文本CountVectorizer或TfidfVectorizer中保留标点符号?

python - 为给定文档选择前 n 个 TFIDF 特征