scikit-learn-contrib / sklearn-pandas

Pandas integration with sklearn
Other
2.81k stars 413 forks source link

ValueError: Logistic Regression supports only penalties in ['l1', 'l2'], got none. #197

Closed sunrisehang closed 5 years ago

sunrisehang commented 5 years ago
from sklearn.linear_model import LogisticRegression
lr = LogisticRegression(penalty='none',solver='saga')
lr.fit(X_train1, y_train1)

It will report the error: ValueError: Logistic Regression supports only penalties in ['l1', 'l2'], got none.

I dont know why i cant input parameter:penalty='none'

redwards-amfam commented 5 years ago

You need to update to the latest development version of sklearn: conda install scikit-learn=0.21.2

sunrisehang commented 5 years ago

thanks

agn-7 commented 4 years ago

I had had the same problem so I updated the latest version of scikit-learn (until now is 0.22) by pip and the problem fixed:

pip install -U scikit-learn

-U is upgrade option

Result:

pip show scikit-learn

Name: scikit-learn
Version: 0.22
Summary: A set of python modules for machine learning and data mining
Home-page: http://scikit-learn.org
Author: None
Author-email: None
License: new BSD
Location: /usr/local/lib/python3.6/dist-packages
Requires: joblib, scipy, numpy
Required-by: mlxtend
vickykhan89 commented 4 years ago

I upgrade but still the same error

shaygeller commented 4 years ago

Upgrading didn't work for me. I found out that the problem is with the solver. If you'll change to 'lbfgs', you'll be able to use penalty='none'.

jayanti-prasad commented 4 years ago

The error message is the same no matter what non-default solver you use: "ValueError: Solver lbfgs supports only 'l2' or 'none' penalties, got l1 penalty."

I tried the followings:

1) LogisticRegression (solver='newton-cg',penalty='l2') 2) LogisticRegression (solver='newton-cg') 3) LogisticRegression (solver='lbfgs') 4) LogisticRegression (solver='lbfgs',penalty='l2') . .

This is pretty bad situation since you get same error no matter what solver you use.

Full error :

================================= Traceback (most recent call last): File "classifiers.py", line 224, in model.fit(X_train, y_train) File "/usr/local/lib/python3.7/site-packages/sklearn/linear_model/_logistic.py", line 1488, in fit solver = _check_solver(self.solver, self.penalty, self.dual) File "/usr/local/lib/python3.7/site-packages/sklearn/linear_model/_logistic.py", line 445, in _check_solver "got %s penalty." % (solver, penalty)) ValueError: Solver lbfgs supports only 'l2' or 'none' penalties, got l1 penalty.

KshetriDeepen commented 4 years ago

Logistic Regression

from sklearn.linear_model import LogisticRegression lr_classifier = LogisticRegression(random_state = 51, penalty = 'l1') lr_classifier.fit(X_train, y_train) y_pred_lr = lr_classifier.predict(X_test) accuracy_score(y_test, y_pred_lr)

And Encountered this issue: ValueError Traceback (most recent call last)

in 2 from sklearn.linear_model import LogisticRegression 3 lr_classifier = LogisticRegression(random_state = 51, penalty = 'l1') ----> 4 lr_classifier.fit(X_train, y_train) 5 y_pred_lr = lr_classifier.predict(X_test) 6 accuracy_score(y_test, y_pred_lr) ~/opt/anaconda3/lib/python3.8/site-packages/sklearn/linear_model/_logistic.py in fit(self, X, y, sample_weight) 1302 The SAGA solver supports both float64 and float32 bit arrays. 1303 """ -> 1304 solver = _check_solver(self.solver, self.penalty, self.dual) 1305 1306 if not isinstance(self.C, numbers.Number) or self.C < 0: ~/opt/anaconda3/lib/python3.8/site-packages/sklearn/linear_model/_logistic.py in _check_solver(solver, penalty, dual) 440 441 if solver not in ['liblinear', 'saga'] and penalty not in ('l2', 'none'): --> 442 raise ValueError("Solver %s supports only 'l2' or 'none' penalties, " 443 "got %s penalty." % (solver, penalty)) 444 if solver != 'liblinear' and dual: ValueError: Solver lbfgs supports only 'l2' or 'none' penalties, got l1 penalty.
ayush237 commented 3 years ago

The solver liblinear supports those panalties, so make sure to create your classifier object like this : clf = LogisticRegression(C=0.01, penalty='l1',solver='liblinear');

Anonymous-Humanoid commented 5 months ago

I found changing penalty=None worked with LogisticRegression

sunrisehang commented 5 months ago

张建行:您的邮件我已收到。

hu-minghao commented 5 months ago

你好,已收到,谢谢。