scikit-learn Python中的ElasticNet与R中的Glmnet之间的区别


11

是否有人尝试验证在同一数据集上的ElasticNetPython和glmnetR 中的scikit-learn中的Elastic Net模型拟合是否产生相同的算术结果?我一直在尝试使用参数的许多组合(因为这两个函数在传递给参数的默认值方面有所不同),并且还对数据进行了缩放,但是在这两种语言之间似乎并没有产生相同的模型。有人遇到过同样的问题吗?


Answers:


6

最后,我得到了与以下代码相同的值:

蟒蛇

# normalize function that gives the same with R
def mystandardize(D):
   S = np.std(D, axis=0, ddof=1)
   M = np.mean(D, axis = 0)
   D_norm = (D-M)/S
return [D_norm, M, S]

Y_norm_train = pd.DataFrame(mystandardize(Y_train)[0])
glmnet_regr = linear_model.ElasticNet(alpha=1, l1_ratio = 0.01,
                                  fit_intercept = True, normalize =    False, tol=0.0000001, max_iter = 100000)
glmnet_regr.fit(X_train, Y_norm_train)

[R

y_norm_train <- scale(y[train_idx])
glmnet_obj_norm <- glmnet(x_train, y_norm_train, alpha=0.01, lambda = 1,  
                   thresh = 1e-07, standardize = FALSE, intercept=TRUE, standardize.response = FALSE)
print_coef(glmnet_obj_norm)

3
R包中使用的Fortran代码有一个相对较新的python包装器glmnet。这也应该获得与R中相同的结果。 github.com/civisanalytics/python-glmnet
尔迪
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.