Skip to content
Advertisement

SKLearn & ElasticNet: Cross validation fails when using Accuracy as a metric

I have a binary classification problem. I’ve been using cross validation to optimize the ElasticNet parameters. However ElasticNet only seems to work when I supply roc_auc as the scoring method to be used during CV, However I also want to test out a wide range of scoring methods, in particular accuracy. Specifically, when using accuracy, ElasticNet returns this error:

JavaScript

However my y targets are indeed binary. Below is a replication of my problem using the dataset from here:

JavaScript

I figured that ElasticNet might be trying to solve a linear regression problem so I tried lr = LogisticRegression(penalty='elasticnet', l1_ratios=[0.1, 0.5, 0.9], solver='saga') as the classifier but the same problem persists.

If I use as the scoring metric scorer = 'roc_auc' then the model is built as expected.

Also, as a sanity to check to see if there is something wrong with the data I tried the same but with a random forest classifier and here the problem disappears:

JavaScript

Has anyone got any ideas on what’s happening here?

Advertisement

Answer

ElasticNet is a regression model.

If you want an ElasticNet penalty in classification, use LogisticRegression:

JavaScript

Minimal Reproducible Example:

JavaScript
Advertisement