الانتقال إلى المحتوى
Machine LearningFeature Engineering· 6 دقائق للقراءة

When Feature Scaling Changes Your Results, and When It Doesn't

Distances, penalties and gradient steps all depend on units; tree splits don't. Know which one your model uses.

ReadIn

مقال كل يوم يفرق معاك

21 نوفمبر 2026
  • كاتب موثّق
  • scikit-learn 1.9
محتويات المقال

A team prototypes "customers like you" recommendations with k-nearest neighbors on three columns: yearly spend in dollars, number of orders and average rating. The neighbors it returns are simply customers who spent about the same, whatever they ordered or rated. Someone suggests adding StandardScaler. Someone else objects: "Scaling doesn't change the information. We tried it on the churn random forest and nothing moved." Both observations are true, and they're about different kinds of models.

The common misconception

Many developers treat feature scaling as optional, cosmetic preprocessing that never changes a model's results. There are good reasons it looks that way:

  • Scaling loses no information. Each column keeps its order and the relative spacing of its values, so it feels like changing units from meters to kilometers.
  • Tree-based models, a common first choice for tabular data, really are unaffected. Anyone who tried scaling with a random forest saw identical numbers.
  • Tutorials often add StandardScaler as a ritual step without saying why, so it looks like hygiene, like formatting code.

The half-truth: scaling keeps the information within each column. But many models combine columns, adding them into distances, penalizing their weights together or stepping along all of them with one learning rate. Those comparisons depend on units.

What actually happens

puts features on comparable ranges. StandardScaler, the usual choice, gives each feature a mean of 0 and a standard deviation of 1. Whether that matters depends on how the model uses the numbers.

Same data, five models

scikit-learn's built-in wine dataset has 13 measurements per wine on very different scales. The script measures five models with 5-fold cross-validation, with and without scaling.

Python
from sklearn.datasets import load_winefrom sklearn.model_selection import StratifiedKFold, cross_val_scorefrom sklearn.neighbors import KNeighborsClassifierX, y = load_wine(return_X_y=True)print(X[:, 12].max(), X[:, 10].max())  # proline 1680.0, hue 1.71# "Scaling is cosmetic": KNN on the raw columnscv = StratifiedKFold(n_splits=5, shuffle=True, random_state=0)score = cross_val_score(KNeighborsClassifier(), X, y, cv=cv).mean()print(f"KNN on raw features: {score:.3f}")  # 0.663

Three models change, two don't move at all. The reason is in how each one uses the numbers.

Distances: the biggest column wins

KNN finds the closest training rows, and an SVM with its default RBF kernel also works from distances between rows. A distance adds up differences across all columns in their raw units. In the wine data, proline goes up to 1680 while hue tops out at 1.71, so a typical proline difference outweighs the full range of hue. scikit-learn's scaling example shows exactly this: without scaling, the neighbors are decided mostly by proline; after standardization, both features fall roughly between -3 and 3 and count about equally. That's KNN going from 0.663 to 0.961 and the SVM from 0.657 to 0.983. PCA suffers for a related reason: it looks for the directions of largest variance, and the same example shows proline dominating the first principal component when the data isn't scaled.

Penalties: regularization depends on units

LogisticRegression applies an L2 penalty by default, which shrinks all weights by the same rule. A feature measured in large units needs only a tiny weight, so it barely gets penalized, while small-unit features get squeezed. The scikit-learn preprocessing guide warns that such penalties may assume features have variance of the same order, and that a feature with a much larger variance can dominate the objective. Here, logistic regression moves from 0.955 to 0.983. With the default max_iter=100, the unscaled version doesn't even finish: the lbfgs solver reports that it failed to converge after 100 iterations.

Gradient steps: one learning rate for every feature

Models trained with , from SGDClassifier to neural networks, use a single learning rate across all weights. Google's Machine Learning Crash Course lists what normalization buys you there: faster convergence, avoiding the "NaN trap" when values are very large, and letting the model learn appropriate weights for each feature.

Trees: only the order matters

A decision tree fitted on the wine data opens with the question "is proline at most 755?". Standardizing changes the threshold number, not which rows go left or right, so the same tree comes out. scikit-learn's example says tree-based models are (almost) unaffected by scaling, and the script agrees: the decision tree and the random forest print identical scores. Gradient-boosted trees such as HistGradientBoostingClassifier behave the same way, because they bin each feature by the order of its values.

Model familyDoes scaling change results?Why
k-nearest neighbors, SVM with RBF kernelYes, often a lotDistances add up raw units
Regularized linear and logistic regressionYesOne penalty for all weights
SGD models, neural networksYes, in trainingOne learning rate for all weights
PCAYesLarge-unit features dominate the components
Decision trees, random forests, gradient-boosted treesNoSplits depend only on value order

How to apply it in your project

  1. Check your model familyUse the table above. If your model compares features through distances, penalties or gradient steps, scaling is part of the model, not cosmetics.
  2. Put the scaler in a pipelineUse make_pipeline(StandardScaler(), model) so cross-validation fits the scaler on each training fold only.
  3. Choose the scaler by distributionStart with StandardScaler, switch to RobustScaler for heavy outliers, and log-transform power-law features first.
  4. Measure both versionsRun the same cross-validation with and without the scaler, as in the script above. It's a one-line change.
  5. Save the whole pipelinePersist the fitted pipeline, scaler included, so production inputs get exactly the training-time transformation.

Key takeaways

  • Scaling keeps the information within each column but changes how models compare columns.
  • Distance-based models, regularized linear models and gradient-trained models need it; on the wine data, KNN went from 0.663 to 0.961.
  • Tree-based models only use the order of values, so scaling leaves them unchanged.
  • Fit the scaler on training data inside a pipeline, and ship it with the model.

📚 المصادر

  1. Importance of Feature Scaling — scikit-learn examples
  2. Preprocessing data: Standardization, or mean removal and variance scaling — scikit-learn User Guide
  3. Numerical data: Normalization — Google Machine Learning Crash Course
  4. Common pitfalls and recommended practices — scikit-learn User Guide

التعليقات (0)