How to suppress scikit-learn warnings (ConvergenceWarning, UndefinedMetricWarning), tested

To suppress scikit-learn warnings, import the category from sklearn.exceptions (ConvergenceWarning, UndefinedMetricWarning) and ignore it inside warnings.catch_warnings(). For precision/recall warnings, zero_division=0 removes the warning at the source. Tested on scikit-learn 1.9.1.

1. The two most common scikit-learn warnings

import warnings
warnings.simplefilter("always")
import numpy as np
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import precision_score

X = np.arange(20, dtype=float).reshape(-1, 1) * 100
y = np.array([0] * 10 + [1] * 10)
LogisticRegression(max_iter=1).fit(X, y)
print(precision_score([0, 0, 1], [0, 0, 0]))

Output:

/usr/local/lib/python3.15/site-packages/sklearn/linear_model/_logistic.py:599: ConvergenceWarning: lbfgs failed to converge after 1 iteration(s) (status=1):
STOP: TOTAL NO. OF ITERATIONS REACHED LIMIT

Increase the number of iterations to improve the convergence (max_iter=1).
You might also want to 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(
/usr/local/lib/python3.15/site-packages/sklearn/metrics/_classification.py:1879: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 due to no predicted samples. Use `zero_division` parameter to control this behavior.
  _warn_prf(average, modifier, f"{metric.capitalize()} is", result.shape[0])
0.0

ConvergenceWarning comes from iterative solvers that hit max_iter; UndefinedMetricWarning from metrics like precision, recall and F1 when a class is never predicted (division by zero, result set to 0.0). Both live in sklearn.exceptions.

2. Suppress by category, scoped

import warnings
import numpy as np
from sklearn.exceptions import ConvergenceWarning, UndefinedMetricWarning
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import precision_score

X = np.arange(20, dtype=float).reshape(-1, 1) * 100
y = np.array([0] * 10 + [1] * 10)
with warnings.catch_warnings():
    warnings.simplefilter("ignore", category=ConvergenceWarning)
    warnings.simplefilter("ignore", category=UndefinedMetricWarning)
    LogisticRegression(max_iter=1).fit(X, y)
    print(precision_score([0, 0, 1], [0, 0, 0]))
print(precision_score([0, 0, 1], [0, 0, 0], zero_division=0))

Output:

0.0
0.0

Ignore each category inside warnings.catch_warnings() so the filters are undone after the block and unrelated warnings still show. For metrics there is a better option: pass zero_division=0 to state what you want, and the warning is not emitted at all, as the last line shows. For ConvergenceWarning, fixing the cause is usually better: see lbfgs failed to converge, where scaling cut iterations from 100+ to 19.

Related: suppress pandas warnings · suppress matplotlib warnings · NumPy RuntimeWarning.

Frequently asked questions

How do I turn off sklearn warnings?

with warnings.catch_warnings(): warnings.simplefilter('ignore', category=ConvergenceWarning) (import it from sklearn.exceptions). Use the same pattern for other categories.

How do I stop 'Precision is ill-defined and being set to 0.0'?

Pass zero_division=0 (or 1) to precision_score/recall_score/f1_score; the warning is then not emitted.

Should I suppress ConvergenceWarning?

Only if you accept a not-fully-optimized model. Scaling the features or raising max_iter usually fixes it.

Written with AI assistance. Measured on 2026-10-23 in Docker: python:3-slim (Python 3.15.1) with scikit-learn 1.9.1. Each block was run verbatim and the output pasted unedited. PlainTables is our extension; Python and scikit-learn are free.