How to Fix Convergence Warning in sklearn in Python
ConvergenceWarning in sklearn means the model did not finish learning properly within the set limits. To fix it, increase max_iter or scale your data with StandardScaler before training. These steps help the model converge and stop the warning.Why This Happens
A convergence warning occurs when a model's training process stops before it fully learns the best solution. This usually happens because the maximum number of iterations (max_iter) is too low or the data is not scaled properly. Models like logistic regression or linear models need enough steps and well-prepared data to find the best fit.
from sklearn.linear_model import LogisticRegression from sklearn.datasets import load_iris X, y = load_iris(return_X_y=True) model = LogisticRegression(max_iter=10) # Too few iterations model.fit(X, y)
The Fix
To fix the warning, increase the max_iter parameter to allow more training steps. Also, scale your data using StandardScaler to help the model learn faster and more reliably. These changes help the model converge without warnings.
from sklearn.linear_model import LogisticRegression from sklearn.datasets import load_iris from sklearn.preprocessing import StandardScaler from sklearn.pipeline import make_pipeline X, y = load_iris(return_X_y=True) # Create a pipeline that scales data then fits the model model = make_pipeline(StandardScaler(), LogisticRegression(max_iter=200)) model.fit(X, y) print("Training completed without convergence warning.")
Prevention
Always scale your input features before training models that rely on gradient-based optimization. Set a sufficiently high max_iter value based on your dataset size and complexity. Monitor training logs for warnings and adjust parameters early. Using pipelines helps keep scaling and modeling steps together, reducing errors.
Related Errors
Other common sklearn warnings include:
- DataConversionWarning: Happens when input data types are inconsistent; fix by converting data properly.
- UndefinedMetricWarning: Occurs when metrics like precision or recall are ill-defined due to no positive samples; fix by checking labels.
- FutureWarning: Indicates deprecated features; fix by updating code to current API.