Solved – Difference between ElasticNet in scikit-learn Python and Glmnet in R

elastic netglmnetmachine learningscikit learn

Has anybody tried to verify whether fitting an Elastic Net model with ElasticNet in scikit-learn in Python and glmnet in R on the same data set produces identical arithmetic results? I have been experimenting with many combinations of the parameters ( since the two functions differ in the default values they pass to the arguments) and also scaling the data, but nothing seems to produce the same model between the two languages. Has anybody faced the same issue?

Best Answer

Finally I got the same values with the following code :

Python

# 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)
Related Question