Sub topic 11
Why penalties need scaled features
This chapter assumes you know how ridge and lasso add a penalty to the loss, and that you have used scikit-learn pipelines. The penalty adds up coefficient sizes, so it only treats features fairly when the features share a common scale.
The problem: coefficient size depends on units
Suppose a feature is measured in millimetres instead of metres. The same real effect now needs a coefficient 1000 times smaller. The model fits the data equally well either way, but the penalty sees two very different coefficients.
An L2 or L1 penalty adds up coefficient sizes. A feature with large numeric values needs only a tiny coefficient, so the penalty barely touches it. A feature with small values needs a large coefficient, so the penalty shrinks it hard. The result is that the units you happened to choose decide which features get regularised.
Ordinary least squares has no such problem, because rescaling a feature just rescales its coefficient. Penalised models are not invariant to scale.
Seeing it in code
Both features below have the same true effect on y. The second column is multiplied by 1000, as if it were recorded in different units.
import numpy as np
from sklearn.linear_model import Ridge
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
rng = np.random.default_rng(0)
a = rng.normal(size=100)
b = rng.normal(size=100)
y = 3 * a + 3 * b + rng.normal(0, 0.5, size=100)
X = np.column_stack([a, 1000 * b])
raw = Ridge(alpha=100).fit(X, y)
scaled = make_pipeline(StandardScaler(), Ridge(alpha=100)).fit(X, y)
print("raw, per original unit of b:", np.round(raw.coef_ * [1, 1000], 2))
print("scaled:", np.round(scaled[-1].coef_, 2))In the raw model, the first coefficient should be shrunk to roughly half its true value of 3. The second, converted back to the original units of b, should stay close to 3. After standardising, both features are shrunk by a similar amount. That is the behaviour you want from a penalty.
The standard fix
Standardise each feature to mean 0 and standard deviation 1 before fitting a penalised model. Then a coefficient measures the effect of a one-standard-deviation change, and the penalty compares features on equal terms.
Details to remember
- The intercept is not penalised, so you do not need to scale the target for the penalty to work.
- Scaling matters for L1, L2, and elastic net alike. For lasso, unscaled features can be dropped or kept based on units rather than usefulness.
- Binary and one-hot columns are usually scaled too, though some practitioners leave them. Choose one approach and apply it consistently.
- The same idea applies to weight decay in neural networks. Unscaled inputs make the penalty uneven across weights.
Exercise
You fit a lasso model on features that include house area in square metres (values around 100) and distance to the city in metres (values around 20000). Without scaling, which feature is more likely to be shrunk to zero, and why? What should you change?
Show answer
The distance feature is less likely to be zeroed. Its large values mean a tiny coefficient is enough, so the penalty barely affects it. Area needs a larger coefficient for the same effect, so the L1 penalty pushes it toward zero more easily. Put a StandardScaler before Lasso in a pipeline, so each feature has mean 0 and standard deviation 1 and the penalty treats them equally.