Search code examples
pythonscikit-learngoogle-colaboratorysgdclassifier

warnings.filterwarnings() doesn't work to suppress ConvergenceWarning of SGDClassifier


I was testing the Scikit-learn package's SGDClassifier's accuracy according to the change of the max_iter property. I also knew that the testing max_iter values are small so there would be a bunch of ConvergenceWarning, so I added a code to ignore those warnings. (Testing on Google colab interface, using a local runtime(Jupyter notebook, WSL2 on Windows 11))

import warnings
warnings.filterwarnings(action='ignore')        # <----

from sklearn.model_selection import cross_validate
from sklearn.linear_model import SGDClassifier

for _seq in range(5, 20 + 1, 5):
    sc = SGDClassifier(loss = "log_loss", max_iter = _seq, random_state = 42)
    scores = cross_validate(sc, train_scaled, train_target, n_jobs = -1)
    print(f"""max_iter: {_seq}, scores = {np.mean(scores["test_score"])}""")

Unfortunately, the code didn't work and the unnecessary warnings filled all over the console, and bothered me looking at the change in the model performances.

/home/knightchaser/.local/lib/python3.10/site-packages/sklearn/linear_model/_stochastic_gradient.py:702: ConvergenceWarning: Maximum number of iteration reached before convergence. Consider increasing max_iter to improve the fit.
  warnings.warn(
/home/knightchaser/.local/lib/python3.10/site-packages/sklearn/linear_model/_stochastic_gradient.py:702: ConvergenceWarning: Maximum number of iteration reached before convergence. Consider increasing max_iter to improve the fit.
  warnings.warn(
/home/knightchaser/.local/lib/python3.10/site-packages/sklearn/linear_model/_stochastic_gradient.py:702: ConvergenceWarning: Maximum number of iteration reached before convergence. Consider increasing max_iter to improve the fit.
  warnings.warn(
/home/knightchaser/.local/lib/python3.10/site-packages/sklearn/linear_model/_stochastic_gradient.py:702: ConvergenceWarning: Maximum number of iteration reached before convergence. Consider increasing max_iter to improve the fit.
  warnings.warn(
/home/knightchaser/.local/lib/python3.10/site-packages/sklearn/linear_model/_stochastic_gradient.py:702: ConvergenceWarning: Maximum number of iteration reached before convergence. Consider increasing max_iter to improve the fit.
  warnings.warn(
max_iter: 5, scores = 0.8196000000000001
...(abbreviated)...

Is there a way to suppress those annoying and unnecessary warning messages? I really appreciate any help you can provide.


Solution

  • Try:

    import logging
    logger = logging.getLogger()
    logger.setLevel(logging.CRITICAL)
    

    OR:

    import logging, sys
    logging.disable(sys.maxsize)