Concepts / Feature Scaling and Model Performance

Feature Scaling and Model Performance

StandardScaler gives each feature a mean of 0 and a variance of 1, but it does not fix a minimum or maximum.

  • Programming

Why the Scale of a Feature Matters

Imagine a dataset where each row describes one example and each column is a feature measured on its own scale. One feature might sit between 10 and 15, while another sits roughly between 1 and 9. Nothing is wrong with either feature on its own. The problem appears when the values of different features live in different ranges, because the features are then not on equal footing. Many machine learning methods treat a larger numeric spread as a stronger signal, so the scale of a feature can influence the result for reasons unrelated to what the feature actually means.

Preprocessing is the set of transformations applied to data before it reaches a learning method. This article looks at four transformations that appear together in scikit-learn: StandardScaler, RobustScaler, MinMaxScaler, and Normalizer. All four change the numbers in a dataset, but they do so for different goals. The useful question is not which one is best in general, but which property you need the transformed data to have.

What do you think happens?

After MinMaxScaler runs on a feature, what are the smallest and largest values that feature can contain, using the default feature range?

  • The original minimum and maximum stay the same

  • 0 and 1

  • -1 and 1

  • The mean is 0 and the variance is 1

Reveal answer
Answer:

0 and 1

MinMaxScaler shifts each feature so that all of its values fall exactly between 0 and 1, which gives a guaranteed minimum of 0 and maximum of 1 for each feature.

Tracing a Two-Feature Dataset

Keep one small two-feature dataset in mind as the running picture. The first feature stays between 10 and 15, and the second between 1 and 9. The question to hold onto is this: when you transform the data, what exactly is guaranteed afterward, and what is left unguaranteed? Each scaler gives a different answer, and reading the four answers side by side is the fastest way to build an intuition for them.

Data pointFeature A (range about 10 to 15)Feature B (range about 1 to 9)
Point 1102
Point 2125
Point 3159

A small running dataset with two features on different scales

Read the table by column to see each feature's own range, and by row to see one complete data point. Both views matter, because the four scalers differ in which direction they operate. Three of them work on each feature (column) separately, while the Normalizer works on each data point (row) as a whole.

StandardScaler: Centering and Spreading Each Feature

StandardScaler transforms each feature so that its mean is 0 and its variance is 1. It does this one feature at a time, so every column ends up with the same center and the same spread. The practical result is that all features come to the same magnitude. No single feature dominates merely because its raw numbers were larger.

In the running dataset, Feature A and Feature B both end up with mean 0 and variance 1 after StandardScaler. Their minimum and maximum values, however, are not forced to 0 and 1. If you check the transformed table, you will find values on both sides of zero, and the largest value for a feature may be well above 1. This is the expected behavior, not an error.

RobustScaler and the Problem of Outliers

RobustScaler aims for the same goal as StandardScaler, which is putting features on a comparable scale, but it builds that scale from different statistics. Instead of the mean and variance, it uses the median and the quartiles. The median is the middle value of a feature, and the quartiles mark the spread of the central portion of the data. Because these statistics describe the middle of the data, they are much less pulled around by a few extreme values.

This matters because of outliers, which are data points that are very different from the rest, such as measurement errors. A single outlier can shift a mean considerably and inflate a variance, which then distorts how every ordinary point is scaled. RobustScaler ignores such points to a much greater degree, which is why it is the preferred choice when the data contains them. Recognizing outliers is therefore part of choosing a scaler.

The key contrast is this. StandardScaler's two statistics, the mean and the variance, are both sensitive to every point, including the extreme ones. RobustScaler's statistics are anchored to the middle of the data, so an unusual point has far less leverage over the transformation.

MinMaxScaler and the Feature Range

MinMaxScaler has a different purpose. It shifts the data so that all values of each feature fall inside a specific range, namely between 0 and 1 by default. For a two-dimensional dataset, this means all of the points end up inside a square whose sides run from 0 to 1 along each axis. Unlike StandardScaler, this technique gives you a guaranteed minimum and maximum for each feature.

The mapping is easiest to see by following one value. The original minimum of a feature moves to the bottom of the target range, the original maximum moves to the top, and every value in between lands at a proportional position. The standard calculation for a target range from a to b is shown below, where X_min and X_max are the smallest and largest original values of the feature.

Formula, step by step
  1. 1
    Find how far the original value X sits above the feature minimum: $X - X_{\min}$
  2. 2
    Divide by the original spread of the feature: $\frac{X - X_{\min}}{X_{\max} - X_{\min}}$, which gives a number between 0 and 1
  3. 3
    Stretch that position to the width of the target range: multiply by $(b - a)$
  4. 4
    Shift to the start of the target range by adding $a$, giving the final value $X'$
Worked example

Scaling one value from Feature A into the range 0 to 1

Feature A runs from a minimum of $10$ to a maximum of $15$. Using the default target range from $a = 0$ to $b = 1$, what does the value $12$ become?

  1. 1
    Identify the inputs

    The value is $X = 12$, the feature minimum is $X_{\min} = 10$, the feature maximum is $X_{\max} = 15$, and the target range is $a = 0$ to $b = 1$.

  2. 2
    Measure the position inside the original range

    Compute $X - X_{\min} = 12 - 10 = 2$, and the original spread is $X_{\max} - X_{\min} = 15 - 10 = 5$.

  3. 3
    Convert to a fraction of the range

    The value sits at $\frac{2}{5} = 0.4$ of the way from the minimum to the maximum.

  4. 4
    Stretch and shift into the target range

    Multiply by $(b - a) = 1 - 0 = 1$ and add $a = 0$, which gives $0.4 \times 1 + 0 = 0.4$.

Result

The value 12 becomes 0.4. The minimum 10 maps to 0, the maximum 15 maps to 1, and 12 lands at 0.4 along the way.

Minimum 10maps to 0Value 12maps to 0.4Maximum 15maps to 1
Where does the value 12 sit in the original Feature A range, and where does it land after MinMaxScaler maps that range onto 0 to 1?

Because the mapping is linear, the ordering of values is preserved. Any value that was larger than another before scaling is still larger after scaling, and the whole feature is compressed or stretched into the chosen interval. This is what gives MinMaxScaler its guaranteed bounds.

Normalizer Works Per Data Point

Normalizer is different from the first three techniques in one fundamental way: it does not work column by column. It looks at each data point as a whole feature vector and rescales that vector so its Euclidean length is 1. Geometrically, every data point is projected onto the circle of radius 1 in two dimensions, or the sphere of radius 1 in higher dimensions.

Feature A columnscaled as one set of valuesPoint (3, 4)scaled as one vectorFeature B columnscaled as its own set ofvaluesScaled point (0.6,0.8)length 1
Does the scaling run down each column (feature) or across each row (data point), and what changes when that direction flips?

The reason the Normalizer must work per point is that its goal is a property of each point, namely its length. A length cannot be fixed one column at a time. Each point is divided by its own length, so every point is scaled by a different number. The divisor is the inverse of that point's length. Points that were far from the origin shrink more, and points already close to the origin are stretched, but in every case the final point has length 1.

Worked example

Normalizing a single data point

A data point has the feature vector (3, 4). Apply the Normalizer so that its Euclidean length becomes 1.

  1. 1
    Compute the length of the point

    The Euclidean length is $\sqrt{3^{2} + 4^{2}} = \sqrt{9 + 16} = \sqrt{25} = 5$.

  2. 2
    Divide each feature by that length

    Dividing both values by 5 gives $\frac{3}{5} = 0.6$ and $\frac{4}{5} = 0.8$.

  3. 3
    Check the new length

    The new vector (0.6, 0.8) has length $\sqrt{0.6^{2} + 0.8^{2}} = \sqrt{0.36 + 0.64} = 1$.

Result

The point (3, 4) becomes (0.6, 0.8). Its direction, meaning its angle, is preserved, while its original length of 5 is discarded.

This normalization is most useful when only the direction of the data matters and the length of the feature vector does not. Once every point has length 1, comparisons between points depend on their angles rather than on how large their raw values were.

Mistakes Beginners Make With Scalers

  • Common mistake

    Assuming StandardScaler output always falls between 0 and 1

    What it looks like

    After StandardScaler, a feature is checked and a value well above 1 appears, so the learner concludes the scaling failed.

    Why it fails

    StandardScaler fixes the mean at 0 and the variance at 1 only. It does not promise any minimum or maximum.

    Do this instead

    Use MinMaxScaler when you need a guaranteed interval such as 0 to 1.

  • Common mistake

    Using StandardScaler on data dominated by outliers without checking

    What it looks like

    A measurement error creates one extreme value, and the mean and variance shift so that ordinary points are squeezed together.

    Why it fails

    Both the mean and the variance are sensitive to every point, including extreme ones.

    Do this instead

    Consider RobustScaler, which uses the median and quartiles and is much less affected by such points.

  • Common mistake

    Thinking the Normalizer scales each feature column

    What it looks like

    A learner expects the Normalizer to make Feature A and Feature B each have length 1 across all rows.

    Why it fails

    The Normalizer works on each data point as a whole feature vector, not on each feature.

    Do this instead

    Each row is rescaled so its own Euclidean length is 1.

  • Common mistake

    Confusing the purpose of a scaler with a claim that one scaler is best in general

Practice: Choosing and Calculating

MEDIUM

A feature runs from a minimum of 10 to a maximum of 15. Using MinMaxScaler with the target range from -1 to 1, what value does the original value 12 become? Show the position of 12 in the original range, then stretch and shift it into the new range.

Hints
  • First find the fraction $\frac{12 - 10}{15 - 10}$, which is the position of 12 inside the original range.
  • The target width is $b - a = 1 - (-1) = 2$, and the shift is $a = -1$.
  • Compute $0.4 \times 2 + (-1)$.
EASY

Your dataset has one feature with a few extreme measurement errors, and another feature with well-behaved values. Which scaler would you consider for the noisy feature, and which property does it preserve? Explain your choice using the median and quartiles.

Hints
  • Think about which statistics are pulled by extreme values.
  • RobustScaler is built on the median and quartiles, which describe the middle of the data.

Summary of the Four Scalers

  • StandardScaler centers each feature on mean 0 with variance 1, one feature at a time, but does not fix a minimum or maximum.
  • RobustScaler uses the median and quartiles, so it is much less affected by outliers such as measurement errors.
  • MinMaxScaler maps each feature so that all of its values fall within a chosen range, by default between 0 and 1, and gives guaranteed bounds.
  • Normalizer works on each data point, rescaling its feature vector to Euclidean length 1 while keeping its direction and discarding its length.

At a glance

  • Features on different ranges are not on equal footing, because many learning methods treat a larger spread as a stronger signal.
  • StandardScaler sets each feature to mean 0 and variance 1 but guarantees no minimum or maximum, while RobustScaler uses the median and quartiles to resist outliers.
  • MinMaxScaler guarantees each feature falls within a target range such as 0 to 1, and the Normalizer rescales each data point to Euclidean length 1.
  • The right scaler depends on outliers and on the property you need the transformed data to have, not on one scaler being best in general.

Related concepts

More concepts starting with F