Diagnosing scikit-learn convergence failures from unscaled features
Compare raw and scaled pipelines to fix convergence warnings and poor accuracy in distance-based scikit-learn models.
24 Apr 2026, 23:22 UTC

The problem: convergence warnings and weak accuracy despite predictive features
You train a scikit-learn classifier such as LogisticRegression, SGDClassifier, SVC, or KNeighborsClassifier. The fit completes, but the model emits ConvergenceWarning, or test accuracy stays far below a naive baseline. One feature's coefficient dwarfs the others even though domain knowledge suggests several inputs matter. Before searching hyperparameters, check whether your numeric features are measured on very different scales.
Useful takeaway: if a Pipeline that wraps StandardScaler around the same estimator materially improves cross-validation accuracy and removes convergence warnings, unscaled features are the likely cause. The fix is to scale inside the pipeline so the scaler is fit only on training folds, never on the full dataset before splitting.
Recognizable condition and likely cause
| Symptom | Likely cause | First check |
|---|---|---|
| ConvergenceWarning from LogisticRegression, SGDClassifier, or similar | Gradient-based updates are dominated by features with large numeric ranges | Compare np.std(X, axis=0) across features |
| KNeighborsClassifier, SVC, or other distance-based model scores far below baseline | Distance metrics weight high-magnitude features disproportionately | Review X.describe() for spreads greater than roughly 100x |
| One linear model coefficient is orders of magnitude larger than the rest | Unscaled inputs distort the regularization penalty and coefficient scale | Inspect clf.coef_ before and after scaling |
Tree-based estimators such as RandomForestClassifier are generally insensitive to feature scaling, so this guide is not the first step for those models.
Ordered diagnostic checks
- Confirm the estimator is scale-sensitive. This guide applies to gradient-based linear models, SVMs, k-nearest neighbors, and similar estimators. It does not apply as a first step to decision trees or random forests.
- Measure feature spread. Run this in a Python session or Jupyter notebook. Replace X with your feature matrix.
import numpy as np
print(np.std(X, axis=0))
print(X.describe())
If the ratio between the largest and smallest standard deviation exceeds roughly 100, scaling is a probable culprit.
- Reproduce the warning or low score. Fit the estimator on training data and note any ConvergenceWarning or low cross-validation accuracy.
- Establish a raw baseline. Run cross-validation on the estimator without scaling.
- Run the same estimator inside a scaling pipeline. Compare the scores.
- Decide. If the scaled pipeline lifts accuracy meaningfully, adopt it. If not, escalate to the checks below.
Fixes tied to each finding
- Scaled pipeline improves results. Replace the standalone estimator with:
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
model = Pipeline([
('scaler', StandardScaler()),
('clf', LogisticRegression(max_iter=1000)),
])
Fit model on training data only. The Pipeline fits StandardScaler within each cross-validation fold and applies the learned mean and variance to validation or test data, which prevents data leakage.
- No improvement after scaling. Investigate class imbalance by using stratify=y in train_test_split, check for target leakage where information from the test period appears in training features, and confirm you have enough samples before returning to hyperparameter search.
- You need to scale a regression target. Do not apply StandardScaler to y unless you inverse-transform predictions and adjust evaluation metrics accordingly.
Concrete example: raw estimator versus scaled pipeline
Run this in a Python session or Jupyter notebook. No special file-system permissions are required beyond reading your data. Replace X and y with your feature matrix and target vector.
from sklearn.model_selection import train_test_split, cross_val_score
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import Pipeline
from sklearn.linear_model import LogisticRegression
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42, stratify=y
)
raw_clf = LogisticRegression(max_iter=1000)
raw_scores = cross_val_score(raw_clf, X_train, y_train, cv=5)
scaled_pipe = Pipeline([
('scaler', StandardScaler()),
('clf', LogisticRegression(max_iter=1000)),
])
scaled_scores = cross_val_score(scaled_pipe, X_train, y_train, cv=5)
print('Raw mean accuracy:', raw_scores.mean())
print('Scaled pipeline mean accuracy:', scaled_scores.mean())
Expected check: if scaled_scores.mean() is materially higher than raw_scores.mean(), and convergence warnings disappear, feature scaling was the fault. Risk: calling scaler.fit_transform(X) before train_test_split leaks test-set statistics into training and inflates metrics. The Pipeline pattern above avoids that because fitting happens inside cross-validation.
Limitations
- This guidance assumes the scikit-learn 1.x API.
- StandardScaler does not require Gaussian distributions, but extreme outliers can still distort mean and variance; consider RobustScaler if outliers dominate.
- Do not scale the target y with StandardScaler unless you inverse-transform predictions and adjust metrics.
- Tree ensembles are generally insensitive to scaling; start here only for linear and distance-based estimators.
Practical verification and rollback
Verify by comparing cross_val_score on the raw estimator against the same estimator inside the Pipeline. A meaningful lift confirms the diagnosis. Confirm leakage avoidance by ensuring the scaler is fit only within cross-validation folds or the Pipeline, never on the entire dataset before splitting. Inspect fitted linear model coefficients after scaling; they should be more comparable across features than on raw data.
Introducing a Pipeline changes the in-memory model object only. To revert, instantiate the raw estimator again; no persisted state changes unless you explicitly save the model to disk.