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?


Finally I got the same values with the following code :


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


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)
  • 3
    $\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 '16 at 8:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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