Feature Scaling

Two columns measured in different units, and a model that can only see the larger one. Switch the scaler and watch the accuracy move.

Feature Scaling

This module needs JavaScript: the numbers are computed in the page rather than recorded.

Worth knowing

Any model that measures distance is sensitive to units. k-NN, SVM with an RBF kernel, k-means and PCA all are.
StandardScaler subtracts the mean and divides by the standard deviation. Outliers survive it.
MinMaxScaler maps to [0, 1]. One extreme value squashes everything else into a corner.
Trees do not care. A split asks whether a value exceeds a threshold, and that answer is unchanged by rescaling.

Overview

Suppose a dataset describes people using age (about 25–55 years) and income (about £20,000–£70,000). In a raw Euclidean distance, income differences can outweigh age differences simply because the numbers are larger. A distance-based model may then behave as if income were the only feature.

In this guide
  1. What common scalers do
  2. Which models benefit
  3. Fit preprocessing on training data only
  4. Common mistakes with Feature Scaling
  5. Further reading

What common scalers do

  • Standard scaling subtracts a feature's training mean and divides by its standard deviation. The transformed feature has mean 0 and standard deviation 1 on that training data.
  • Min–max scaling maps the training minimum and maximum to chosen endpoints, commonly 0 and 1. It is useful when a bounded range is required, but a new extreme value can compress most observations into a narrow interval.
  • Robust scaling uses the median and interquartile range. It is less sensitive to extreme values than mean-and-standard-deviation scaling, though it does not make outliers harmless.

For example, if age has mean 40 and standard deviation 10, age 30 becomes −1. If income has mean £45,000 and standard deviation £15,000, £30,000 also becomes −1. The two values now represent the same distance from their respective training means.

Which models benefit

Scaling usually matters for methods that compare distances or use coefficient magnitudes, including KNN, K-means, PCA, RBF-kernel SVMs, and regularised linear models. Neural-network optimisation can also be easier when inputs have comparable ranges.

Decision trees and tree ensembles usually do not need feature scaling: a split such as income ≤ 45000 can be expressed as an equivalent threshold after a monotonic rescaling. Scaling may still be useful for a shared preprocessing pipeline, but it generally does not change those tree splits.

Fit preprocessing on training data only

Learn the scaler's statistics from the training split, then use that fitted scaler to transform validation and test rows. If you calculate the mean, standard deviation, minimum, or maximum from the whole dataset first, information from the held-out rows has entered training. That is data leakage. A pipeline keeps the scaler inside each training fold during cross-validation.

Common mistakes with Feature Scaling

  • Assuming standard scaling removes outlier influence. Mean and standard deviation can both be pulled by extreme values; consider robust scaling when that is appropriate.
  • Fitting a new scaler on validation or test data. Reuse the scaler fitted on training data so evaluation matches deployment.
  • Scaling without a reason. Tree-based models generally do not need it. Binary indicator columns are often kept as 0/1 so their meaning remains clear.
  • Forgetting to invert target transformations. If the target was scaled, convert predictions back to the original units before interpreting errors or reporting results.

Further reading

The scikit-learn preprocessing guide describes these transformations and their API. Continue with K-nearest neighbors, where feature scale directly affects which examples are considered nearest.

Check yourself

0 of 3

Answer without scrolling back up.

  1. Why is k-NN affected by feature scaling but a decision tree is not?

  2. What is MinMaxScaler's main weakness compared with StandardScaler?

  3. Where must a scaler's mean and standard deviation come from?

Cheat sheet

Feature Scaling

Apply min-max and standard scaling to raw features. See which models need scaled inputs, which ignore them, and what happens when one column dominates.

MACHINE LEARNING · vizlearn.in/machine_learning/feature_scaling.html

About the author

Ashish Jangra builds and maintains VizLearn. Every module here is written and the visualisation behind it hand-built, so the numbers in a readout come from the same code that draws the picture. Corrections are genuinely welcome and get priority over everything else — if a page states something wrong, or an animation misrepresents what the algorithm does, get in touch.

Teaching a course with this? Individual teachers can use it freely; for a whole department or programme, see institutional licences.