Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
标准化Python Lasso和R glmnet中的X不同?_Python_R_Lasso Regression_Standardized - Fatal编程技术网

标准化Python Lasso和R glmnet中的X不同?

标准化Python Lasso和R glmnet中的X不同?,python,r,lasso-regression,standardized,Python,R,Lasso Regression,Standardized,我试图用Python的scikit learn和R的glmnet来拟合lasso得到同样的结果 如果我在Python中指定“normalize=True”,在R中指定“standarize=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 = La

我试图用Python的scikit learn和R的glmnet来拟合lasso得到同样的结果

如果我在Python中指定“normalize=True”,在R中指定“standarize=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])
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
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])
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
R:

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])
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
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])
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
然而,如果我不想标准化变量并设置normalize=False和normalize=F,它们会给出完全不同的结果

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])
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
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])
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
R:

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])
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
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])
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中的“standarize”有什么区别?

目前,关于
normalize
参数,状态“如果您希望标准化,请在调用fit对
normalize=False
的估计器之前使用
StandardScaler


因此,显然,规范化和标准化与
sklearn.linear_model.Lasso
不同。阅读了
StandardScaler
文档后,我无法理解其中的区别,但提供的
normalize
参数说明暗示了这一点。

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

显然,规范化和标准化与
sklearn.linear\u model.Lasso
不同。阅读了
StandardScaler
文档后,我无法理解其中的差异,但是
normalize
参数的描述暗示了存在差异的事实