Many methods compare variables by the size of their numbers. A variable measured in thousands can then outweigh one measured in tens, even if it matters no more. Standardising puts the variables on a common scale.
What standardising does
We subtract the mean and divide by the standard deviation:
\[ z = \frac{x - \bar{x}}{s} \]
The result, the z-score, has mean 0 and standard deviation 1. It has no units, and it tells us how many standard deviations a value lies from the mean. The shape of the distribution stays the same, so skewed data remains skewed.
x <- c(40, 50, 60, 70, 80)
z <- (x - mean(x)) / sd(x) # same as scale(x)
round(z, 3)[1] -1.265 -0.632 0.000 0.632 1.265from statistics import mean, stdev
x = [40, 50, 60, 70, 80]
m, s = mean(x), stdev(x) # sample SD, as in R
z = [round((v - m) / s, 3) for v in x]
print(z)[-1.265, -0.632, 0.0, 0.632, 1.265]Example 1: which customer is most similar?
A shop wants to know whether customer B or C is more like customer A:
| Customer | Age (years) | Monthly income |
|---|---|---|
| A | 25 | 50,000 |
| B | 60 | 50,500 |
| C | 26 | 52,000 |
C is almost the same age as A, while B is 35 years older. Now we compute the Euclidean distance in raw units:
\[ \begin{aligned} d(A, B) &= \sqrt{35^2 + 500^2} \approx 501 \\ d(A, C) &= \sqrt{1^2 + 2000^2} \approx 2000 \end{aligned} \]
B appears four times closer. The age gap hardly counts, because income has much larger numbers.
Now we standardise. Suppose that among all customers the standard deviation is 10 years for age and 5,000 for income. We divide each difference by its standard deviation. The means cancel when we subtract, so we do not need them.
\[ \begin{aligned} d(A, B) &= \sqrt{3.5^2 + 0.1^2} \approx 3.50 \\ d(A, C) &= \sqrt{0.1^2 + 0.4^2} \approx 0.41 \end{aligned} \]
C is now much closer, which matches what we see in the table.
Example 2: the units should not decide
Without standardising, merely recording income in thousands changes the answer. This means we divide each income by 1,000:
| Customer | Age | Income | Income in thousands |
|---|---|---|---|
| A | 25 | 50,000 | 50 |
| B | 60 | 50,500 | 50.5 |
| C | 26 | 52,000 | 52 |
The income gaps shrink from 500 and 2,000 to 0.5 and 2, while the age gaps stay at 35 and 1:
\[ \begin{aligned} d(A, B) &= \sqrt{35^2 + 0.5^2} \approx 35.0 \\ d(A, C) &= \sqrt{1^2 + 2^2} \approx 2.24 \end{aligned} \]
Comparing the versions:
| Version | \(d(A, B)\) | \(d(A, C)\) | Closer |
|---|---|---|---|
| Raw income | 501 | 2000 | B |
| Income in thousands | 35.0 | 2.24 | C |
| Standardised | 3.50 | 0.41 | C |
The customers have not changed, only the unit. A standardised value is the same in any unit, because the standard deviation is rescaled along with the data. Methods based on distances, such as k-nearest neighbours and k-means clustering, are sensitive to this.
Example 3: principal component analysis
Principal component analysis (PCA) looks for the directions of greatest variance. R’s built-in USArrests data holds, for the 50 US states, three arrest rates and the percentage of urban population. Their variances differ greatly:
sapply(USArrests, var) Murder Assault UrbanPop Rape
18.97047 6945.16571 209.51878 87.72916 We run PCA without and with standardising (scale. = TRUE):
raw <- prcomp(USArrests)
scaled <- prcomp(USArrests, scale. = TRUE)
# weights of each variable on the first component
round(cbind(raw = raw$rotation[, 1],
scaled = scaled$rotation[, 1]), 3)
# share of variance explained by the first component
round(summary(raw)$importance[2, 1], 3)
round(summary(scaled)$importance[2, 1], 3) raw scaled
Murder 0.042 -0.536
Assault 0.995 -0.583
UrbanPop 0.046 -0.278
Rape 0.075 -0.543
[1] 0.966
[1] 0.62Without scaling, the first component is almost entirely Assault (weight 0.995) and seems to explain 96.6% of the variance. This mostly reflects its large numbers. After scaling, all three arrest rates contribute similarly, and the share is a more modest 62%.
The overall sign of a component is arbitrary. We compare the sizes of the weights and whether they agree in sign.
Penalised regression
Ridge and lasso regression penalise large coefficients. The size of a coefficient depends on the unit of its variable: changing metres to kilometres multiplies it by 1,000. Standardising lets the penalty treat the variables comparably.
When is it unnecessary?
- Same unit and meaningful sizes: for map coordinates, the raw differences are what we want. Scaling each axis separately would distort distances.
- Tree-based methods (decision trees, random forests): they split on the order of values, so scaling has almost no effect.
- Ordinary least squares: rescaling a predictor changes its coefficient, but not the fitted values.
Cautions
- Compute the mean and standard deviation from the training data only, and apply the same two numbers to new data.
- Outliers inflate the standard deviation. A scale based on the median and interquartile range is less affected.
- A variable that never changes has standard deviation 0, so we cannot standardise it.
Key takeaways
- Standardising gives each variable mean 0 and standard deviation 1.
- It stops units from deciding the result of distance-based methods, PCA and penalised regression.
- It is a choice, not a rule: we skip it when the original scale carries the information we need.
References
- Preprocessing data, scikit-learn documentation.
- Importance of feature scaling, scikit-learn example.
- R documentation:
scaleandprcomp.