Why should features be standardized before applying an L1 or L2 penalty?
Last updated
The penalty acts on raw coefficient magnitudes, and those depend on each feature's units. Without standardization, features are penalized unequally for arbitrary reasons, so both the amount of shrinkage and which features get selected depend on the measurement scale.
Scale changes coefficient size:
Measure a feature in meters instead of kilometers and its coefficient becomes 1000× smaller for the same effect.
Unpenalized OLS doesn't care, because predictions are unchanged. A penalty does care.
Unequal treatment:
Features with small numeric ranges need large coefficients, so they are penalized heavily and shrunk or dropped.
Features with large ranges need tiny coefficients, so they are barely penalized.
With L1, this can decide which features survive feature selection.
Standardizing (zero mean, unit variance) puts every coefficient on a common scale, so λ means the same thing for each feature.
It also improves optimization conditioning, for example for coordinate or gradient descent.
Practical notes:
Fit the scaler on training data only, for example inside a
Pipeline, to avoid leakage.Some libraries (like
glmnet) standardize internally. scikit-learn'sRidgeandLassodo not.
[lang:python]
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import Lasso
model = make_pipeline(StandardScaler(), Lasso(alpha=0.1))
model.fit(X_train, y_train)