Sub topic 8
How outliers distort each scaler
This chapter assumes you know the StandardScaler and MinMaxScaler formulas and the fit / transform API from earlier chapters. It also assumes you can read basic NumPy output.
Why outliers matter here
An outlier is a value far away from the rest of a column. Both scalers learn their parameters from the column they are fitted on, so one extreme value changes the parameters for every row.
| Scaler | Statistics it learns | Sensitive to outliers through |
|---|---|---|
| `MinMaxScaler` | minimum and maximum | The outlier *is* the minimum or maximum |
| `StandardScaler` | mean and standard deviation | The outlier shifts the mean and inflates the standard deviation |
A small experiment
The column below has three ordinary values and one outlier. The code scales it twice, once without the outlier and once with it.
import numpy as np
from sklearn.preprocessing import StandardScaler, MinMaxScaler
clean = np.array([[1.0], [2.0], [3.0]])
dirty = np.array([[1.0], [2.0], [3.0], [100.0]]) # 100 is an outlier
for name, data in [("clean", clean), ("with outlier", dirty)]:
mm = MinMaxScaler().fit_transform(data).ravel()
z = StandardScaler().fit_transform(data).ravel()
print(name)
print(" min-max :", np.round(mm, 3))
print(" z-score :", np.round(z, 3))Rounded to three decimals, the results are:
| Input | Min-max, ordinary values | Z-score, ordinary values | Outlier |
|---|---|---|---|
| Without outlier | 0, 0.5, 1 | -1.225, 0, 1.225 | none |
| With outlier | 0, 0.010, 0.020 | -0.601, -0.577, -0.554 | min-max 1, z 1.732 |
What happens to MinMaxScaler
The range is now 99 instead of 2. The values 1, 2 and 3 land between 0 and 0.02, and the outlier alone takes up the rest of the interval. A model sees almost no difference between the ordinary rows, and the feature carries little useful information.
What happens to StandardScaler
The mean moves from 2 to 26.5. The population standard deviation grows from about 0.82 to about 42.4. The ordinary values now sit near -0.58, all close together and all below the mean. The outlier is only 1.73 standard deviations from the mean, so it does not look extreme in z-score terms.
This effect is called masking. An extreme point inflates the standard deviation, which makes the point itself look less extreme. Rules like "flag anything with |z| > 3" can miss outliers in small samples for this reason.
flowchart TDThe diagram below summarizes how the effect spreads.
Outliers that appear after fitting
Outliers can also show up in the test data. The scaler keeps the training statistics, so the two scalers behave differently:
MinMaxScalercan return values below 0 or above 1. A test value of 150 with a training range of 1 to 100 becomes about 1.51.StandardScalerhas no fixed bounds, so the value simply gets a large z-score.
import numpy as np
from sklearn.preprocessing import MinMaxScaler
train = np.array([[1.0], [2.0], [3.0], [100.0]])
test = np.array([[150.0], [-20.0]])
scaler = MinMaxScaler().fit(train)
print(scaler.transform(test).ravel()) # values outside [0, 1]Some models assume inputs lie in [0, 1], and this output breaks that assumption. MinMaxScaler(clip=True) forces transformed values into the feature range. Clipping hides the information that a value was extreme, so use it deliberately.
What to do about outliers
First decide whether the outlier is an error or real data. A typo such as a height of 1800 cm should be fixed or removed before scaling. A genuine extreme value, such as a very large purchase, is real information, and deleting it can hurt the model.
Then pick one of these options:
- Remove or correct the bad values before fitting the scaler.
- Use
RobustScaler, which centers on the median and divides by the interquartile range. Both statistics ignore the extremes. - Transform the column first (for example, a log transform for positive, right-skewed data) and then scale it.
- Keep the data and accept the distortion if the model is not scale-sensitive, such as a tree-based model.
Summary
MinMaxScalerdepends entirely on the two extreme values, so a single outlier compresses everything else.StandardScaleris also pulled by outliers, and masking can make an outlier look milder than it is.- Test-time outliers produce min-max values outside [0, 1] unless you clip.
- Inspect the data first. Then clean it, use a robust method, or transform the column.
Exercise
A training column holds the values 10, 12, 11, 13 and 500. You fit a MinMaxScaler on it. Roughly what scaled value does 12 receive, and what does this tell you about the feature?
Show answer
The minimum is 10 and the maximum is 500, so 12 becomes (12 - 10) / (500 - 10) = 2 / 490, about 0.004. The ordinary values 10 to 13 all fall between 0 and about 0.006, so the model can barely tell them apart. The outlier at 500 has stretched the range. Fix or remove it if it is an error, or use RobustScaler or a log transform if it is real data.
Exercise
Why can a StandardScaler leave an extreme value looking only 1.73 standard deviations from the mean in the column [1, 2, 3, 100]?
Show answer
The value 100 is included when the mean (26.5) and standard deviation (about 42.4) are computed. It pulls the mean toward itself and inflates the standard deviation, so its own z-score is (100 - 26.5) / 42.4, about 1.73. This effect is called masking.