Skip to content

No warning when LogisticRegression does not converge #10866

Closed

Description

@oliverangelil

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')

Activity

  1. changed the title [-]No warning when LogisticRegression does not converge.[/-] [+]No warning when LogisticRegression is far from converging.[/+] on Mar 24, 2018
  2. lesteve commented on Mar 26, 2018

    @lesteve
    Member

    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.

  3. rth commented on Mar 26, 2018

    @rth
    Member

    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

  4. rth commented on Mar 26, 2018

    @rth
    Member

    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

  5. lesteve commented on Mar 26, 2018

    @lesteve
    Member

    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.

  6. added
    good first issue
    on Mar 26, 2018
  7. changed the title [-]No warning when LogisticRegression is far from converging.[/-] [+]No warning when LogisticRegression does not converge[/+] on Mar 26, 2018
  8. AlexandreSev commented on Mar 26, 2018

    @AlexandreSev
    Contributor

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

  9. rth commented on Mar 26, 2018

    @rth
    Member

    Sure, thank you @AlexandreSev

  10. rth commented on Mar 26, 2018

    @rth
    Member

    Unless @oliverangelil was planning to submit a PR..

  11. AlexandreSev commented on Mar 26, 2018

    @AlexandreSev
    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 ?

  12. lesteve commented on Mar 26, 2018

    @lesteve
    Member

    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.

  13. jnothman commented on Mar 26, 2018

    @jnothman
    Member
  14. added 11 commits that reference this issue on Mar 28, 2018
    7f5f516
    5e072ae
    af76e36
    2d73ad2
    4c8e4fe
    6d3ed75
    c5091d5
    cfee9c1
    3eb22a0
    5d10ba5
    dcec4ba
  15. Bharat8219-git commented on Sep 13, 2020

    @Bharat8219-git

    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(

  16. Bharat8219-git commented on Sep 13, 2020

    @Bharat8219-git

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    good first issuehelp wanted

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions