【问题标题】:Standardizing X different in Python Lasso and R glmnet?在 Python Lasso 和 R glmnet 中标准化 X 不同?
【发布时间】:2017-09-23 09:30:07
【问题描述】:

我试图使用 Python 的 scikit-learn 和 R 的 glmnet 获得相同的结果拟合套索。 A helpful link

如果我在 Python 中指定“normalize =True”,在 R 中指定“standardize = T”,它们给了我相同的结果。

Python:

from sklearn.linear_model import Lasso
X = np.array([[1, 1, 2], [3, 4, 2], [6, 5, 2], [5, 5, 3]])
y = np.array([1, 0, 0, 1])
reg = Lasso(alpha =0.01, fit_intercept = True, normalize =True)
reg.fit(X, y)
np.hstack((reg.intercept_, reg.coef_))

Out[95]: array([-0.89607695,  0.        , -0.24743375,  1.03286824])

R:

reg_glmnet = glmnet(X, y, alpha = 1, lambda = 0.02,standardize = T)
coef(reg_glmnet)

4 x 1 sparse Matrix of class "dgCMatrix"
                    s0
(Intercept) -0.8960770
V1           .        
V2          -0.2474338
V3           1.0328682

但是,如果我不想标准化变量并设置 normalize =False 和 standardize = F,他们给了我完全不同的结果。

Python:

from sklearn.linear_model import Lasso
Z = np.array([[1, 1, 2], [3, 4, 2], [6, 5, 2], [5, 5, 3]])
y = np.array([1, 0, 0, 1])
reg = Lasso(alpha =0.01, fit_intercept = True, normalize =False)
reg.fit(Z, y)
np.hstack((reg.intercept_, reg.coef_))

Out[96]: array([-0.88      ,  0.09384212, -0.36159299,  1.05958478])

R:

reg_glmnet = glmnet(X, y, alpha = 1, lambda = 0.02,standardize = F)
coef(reg_glmnet)

4 x 1 sparse Matrix of class "dgCMatrix"
                     s0
(Intercept) -0.76000000
V1           0.04441697
V2          -0.29415542
V3           0.97623074

Python Lasso 中的“normalize”和 R 的 glmnet 中的“standardize”有什么区别?

【问题讨论】:

    标签: python r lasso-regression standardized


    【解决方案1】:

    目前,对于normalize 参数,docs 状态“如果您希望标准化,请在使用normalize=False 调用拟合估计器之前使用StandardScaler。”

    所以很明显 normalize 和standardize 和sklearn.linear_model.Lasso 是不一样的。阅读了StandardScaler 文档后,我无法理解其中的区别,但提供的normalize 参数描述暗示了这一事实。

    【讨论】:

      猜你喜欢
      • 2016-11-17
      • 2021-03-13
      • 2013-06-19
      • 2020-05-07
      • 2020-06-05
      • 2018-11-09
      • 2016-04-30
      • 2020-12-24
      • 1970-01-01
      相关资源
      最近更新 更多