15
$\begingroup$

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?

$\endgroup$
1

1 Answer 1

10
$\begingroup$

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)
$\endgroup$
2
  • 4
    $\begingroup$ There is a relatively new python wrapper for the Fortran code used in the R package glmnet. This should also get you the same results as in R. github.com/civisanalytics/python-glmnet $\endgroup$
    – Jordi
    Sep 1, 2016 at 8:03
  • $\begingroup$ It is still strange that the results differ for non-standardized data. There's not reason that they should as far as I know. $\endgroup$ Apr 29, 2022 at 8:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.