Search code examples
pythonmachine-learningscikit-learndata-sciencelogistic-regression

How to implement L1 logistic regression?


As part of pursuing a course, I was trying to implement L1 logistic regression using scikit-learn in Python. Unfortunately for the code

clf, pred = fit_and_plot_classifier(LogisticRegression(penalty = 'l1', C=1000000))

I get the error message

ValueError: Solver lbfgs supports only 'l2' or 'none' penalties, got l1 penalty.

I tried setting l1_ratio

clf, pred = fit_and_plot_classifier(LogisticRegression(l1_ratio = 1))

but got the error message

C:\Users\HP\Anaconda3\lib\site-packages\sklearn\linear_model\_logistic.py:1499: UserWarning: l1_ratio parameter is only used when penalty is 'elasticnet'. Got (penalty=l2)"(penalty={})".format(self.penalty))

So, how to implement L1 Logistic regression?


Solution

  • You can do it like you are doing in the first code snippet, but you have to define another solver. Use either ‘liblinear’ or ‘saga’, check more in the documentation.