Solved – Can scikit-learn’s ElasticNetCV be used for classification problems

classificationelastic netglmnetscikit learn

I have been using R's glmnet for training elastic net models for both regression and classification problems. In glmnet, you have to set the parameter family as family="binomial" for binary classification tasks and family="gaussian" for regression tasks. This is very much in line with the theory as the cost function for regression and that for classification differs a lot (the cost function of regression is primarily dependent on the difference between predicted and actual quantitative value whereas the cost function for classification from the context of logistic regression is based on sigmoid function and log values).

In scikit-learn, the corresponding function for building Elastic Net model is ElasticNetCV and there is no mention of selecting a loss function or something which is intuitively similar to the usage of glmnet for classification problems. It looks like scikit-learn's ElasticNetCV can only be used for regression tasks in the strict sense. But I have seen many people using the same for classification tasks as well. Is this a valid practice?

Best Answer

You can use the elasticnet penalty in sklearn's Logistic Regression classifier:

from sklearn.linear_model import LogisticRegression

lr = LogisticRegression(penalty = 'elasticnet', solver = 'saga', l1_ratio = 0.5)

LogisticRegressionCV will take these parameters as well

Related Question