10000 No warning when LogisticRegression does not converge · Issue #10866 · scikit-learn/scikit-learn · GitHub
[go: up one dir, main page]

Skip to content

No warning when LogisticRegression does not converge #10866

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
oliverangelil opened this issue Mar 24, 2018 · 12 comments · Fixed by #10881
Closed

No warning when LogisticRegression does not converge #10866

oliverangelil opened this issue Mar 24, 2018 · 12 comments · Fixed by #10881
Labels
good first issue Easy with clear instructions to resolve help wanted

Comments

@oliverangelil
Copy link
oliverangelil commented Mar 24, 2018

Description

I've run LogisticRegressionCV on the Wisconsin Breast Cancer data, and the output of clf.n_iter_ was 100 for all but 1 of the variables. The default of 100 iterations was probably not sufficient in this case. Should there not be some kind of warning? I have done some tests and ~3000 iterations was probably a better choice for max_iter...

Steps/Code to Reproduce

from sklearn.datasets import load_breast_cancer
from sklearn.linear_model import LogisticRegressionCV

data = load_breast_cancer()
y = data.target
X = data.data

clf = LogisticRegressionCV()
clf.fit(X, y)
print(clf.n_iter_)

Expected Results

Some kind of error to be shown. E.g: "result did not converge, try increasing the maximum number of iterations (max_iter)"

Versions

import platform; print(platform.platform())
Darwin-16.7.0-x86_64-i386-64bit
import sys; print("Python", sys.version)
('Python', '2.7.14 |Anaconda, Inc.| (default, Oct 5 2017, 02:28:52) \n[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)]')
import numpy; print("NumPy", numpy.version)
('NumPy', '1.13.3')
import scipy; print("SciPy", scipy.version)
('SciPy', '1.0.0')
import sklearn; print("Scikit-Learn", sklearn.version)
('Scikit-Learn', '0.19.1')

@oliverangelil oliverangelil changed the title No warning when LogisticRegression does not converge. No warning when LogisticRegression is far from converging. Mar 24, 2018
@lesteve
Copy link
Member
lesteve commented Mar 26, 2018

If you use verbose=1 in your snippet, you'll get plenty of ConvergenceWarning.

from sklearn.datasets import load_breast_cancer
from sklearn.linear_model import LogisticRegressionCV

data = load_breast_cancer()
y = data.target
X = data.data

clf = LogisticRegressionCV(verbose=1)
clf.fit(X, y)
print(clf.n_iter_)

I am going to close this one, please reopen if you strongly disagree.

@lesteve lesteve closed this as completed Mar 26, 2018
@rth
Copy link
Member
rth commented Mar 26, 2018

I also think that LogisticRegression (and LogisticRegressionCV) should print a ConvergenceWarning when it fails to converge with default parameters.

It does not make sense to expect the users to set the verbosity in order to get the warning. Also other methods don't do this: e.g. MLPClassifier may raise ConvergenceWarnings so does MultiTaskElasticNet or LassoLars.

The culprit is this line that only affects the lbfgs solver. This is more critical if we can to make lbfgs the default solver #9997 in LogisticRegression

@rth
Copy link
Member
rth commented Mar 26, 2018

After more thoughts, the case of LogisticRegressionCV is more debatable, as there are always CV parameters that may fail to converge and handling it the same way as GridSearchCV would be probably more reasonable.

However,

from sklearn.datasets import load_breast_cancer
from sklearn.linear_model import LogisticRegression

data = load_breast_cancer()
y = data.target
X = data.data

clf = LogisticRegression(solver='lbfgs')
clf.fit(X, y)
print(clf.n_iter_)

silently fails to converge, and this is a bug IMO. This was also recently relevant in #10813

@lesteve
Copy link
Member
lesteve commented Mar 26, 2018

A quick git grep seems to confirm that ConvergenceWarning are generally issued when verbose=0, reopening.

The same thing happens for solver='liblinear' (the default solver at the time of writing) by the way (you need to decrease max_iter e.g. set it to 1). There should be a test that checks the ConvergenceWarning for all solvers.

@lesteve lesteve reopened this Mar 26, 2018
@lesteve lesteve added good first issue Easy with clear instructions to resolve help wanted labels Mar 26, 2018
@lesteve lesteve changed the title No warning when LogisticRegression is far from converging. No warning when LogisticRegression does not converge Mar 26, 2018
@AlexandreSev
Copy link
Contributor

I see that nobody is working on it. Can I do it ?

@rth
Copy link
Member
rth commented Mar 26, 2018

Sure, thank you @AlexandreSev

@rth
Copy link
Member
rth commented Mar 26, 2018

Unless @oliverangelil was planning to submit a PR..

@AlexandreSev
Copy link
Contributor

I think I need to correct also this line. Do you agree ?
Moreover, I just saw that the name of this function is not perfectly correct, since it does not test the convergence warning.
Maybe we could write a test a bit like this one to test all the warnings. What do you think ?

@lesteve
Copy link
Member
lesteve commented Mar 26, 2018

You can reuse test_max_iter indeed to check for all solvers that there has been a warning and add an assert_warns_message in it. If you do that, test_logistic_regression_convergence_warnings is not really needed any more. There is no need to touch test_lr_liblinear_warning.

The best is to open a PR and see how that goes! Not that you have mentioned changing the tests but you will also need to change sklearn/linear_model/logistic.py to make sure a ConvergenceWarning is issued for all the solvers.

@jnothman
Copy link
Member
jnothman commented Mar 26, 2018 via email

AlexandreSev added a commit to AlexandreSev/scikit-learn that referenced this issue Mar 28, 2018
…ion.

For now, convergence warnings are silent when verbose is set to 0
for lbfgs and liblinear solver, whereas they are not for others.
Fix scikit-learn#10866.
AlexandreSev added a commit to AlexandreSev/scikit-learn that referenced this issue Mar 28, 2018
Modify slightly test_max_iter to check that a warning is sent.
See scikit-learn#10866.
AlexandreSev added a commit to AlexandreSev/scikit-learn that referenced this issue Mar 28, 2018
…ion.

For lbfgs and liblinears solvers, the convergence warnings appeared
only when verbose was greater than 0, whereas they appeared with
verbose = 0 with other solvers.
See scikit-learn#10866
AlexandreSev added a commit to AlexandreSev/scikit-learn that referenced this issue Mar 28, 2018
Modify test_max_iter to also check for the warning, and remove
a test which becomes redundant with this modification.
See scikit-learn#10866.
AlexandreSev added a commit to AlexandreSev/scikit-learn that referenced this issue Mar 28, 2018
…re_warn

With the add of convergence warning in logistic regression, this
test failed.
See scikit-learn#10866
AlexandreSev added a commit to AlexandreSev/scikit-learn that referenced this issue Mar 28, 2018
…ion.

For lbfgs and liblinears solvers, the convergence warnings appeared
only when verbose was greater than 0, whereas they appeared with
verbose = 0 with other solvers.
See scikit-learn#10866
AlexandreSev added a commit to AlexandreSev/scikit-learn that referenced this issue Mar 28, 2018
Modify test_max_iter to also check for the warning, and remove
a test which becomes redundant with this modification.
See scikit-learn#10866.
AlexandreSev added a commit to AlexandreSev/scikit-learn that referenced this issue Mar 28, 2018
…re_warn

With the add of convergence warning in logistic regression, this
test failed.
See scikit-learn#10866
AlexandreSev added a commit to AlexandreSev/scikit-learn that referenced this issue Mar 28, 2018
AlexandreSev added a commit to AlexandreSev/scikit-learn that referenced this issue Mar 29, 2018
For lbfgs and liblinears solvers, the convergence warnings appeared
only when verbose was greater than 0, whereas they appeared with
verbose = 0 with other solvers.
Create test to check the convergence warning in logistic regression
and in linear svm.
Update `test_search` to ignore this convergence warning.
Fixes scikit-learn#10866
AlexandreSev added a commit to AlexandreSev/scikit-learn that referenced this issue Mar 29, 2018
For lbfgs and liblinears solvers, the convergence warnings appeared
only when verbose was greater than 0, whereas they appeared with
verbose = 0 with other solvers.
Create test to check the convergence warning in logistic regression
and in linear svm.
Update `test_search` to ignore this convergence warning.
Fixes scikit-learn#10866
@Bharat8219-git
Copy link

C:\ProgramData\Anaconda3\lib\site-packages\sklearn\linear_model_logistic.py:762: ConvergenceWarning: lbfgs failed to converge (status=1):
STOP: TOTAL NO. of ITERATIONS REACHED LIMIT.

Increase the number of iterations (max_iter) or scale the data as shown in:
https://scikit-learn.org/stable/modules/preprocessing.html
Please also refer to the documentation for alternative solver options:
https://scikit-learn.org/stable/modules/linear_model.html#logistic-regression
n_iter_i = _check_optimize_result(

@Bharat8219-git
Copy link

please solve this issue with right algorothms

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Easy with clear instructions to resolve help wanted
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants
0