Lesson 5.9 · 8 min read · about 50 min to master at a very slow pace
Selecting λ Using Cross-validation
λ is a hyperparameter: choose it by cross-validation on standardised training data, then test once. The full procedure, the one-standard-error rule, a case study, and the mistakes to avoid.
The procedure
- Split off a test set and leave it alone.
- Choose a grid of λ values, log-spaced (e.g. to ).
- For each λ, run K-fold CV on the training data. In each fold, standardise using that fold's training part only, fit, and record the validation error.
- Pick the λ with the lowest mean CV error (or use the 1-SE rule below).
- Refit on all training data with that λ.
- Evaluate once on the test set.
Why standardise first?
The penalty treats every coefficient the same, but a coefficient's size depends on its predictor's units. A variable measured in small units needs a big coefficient and gets penalised unfairly. Standardising (mean 0, SD 1) puts every predictor on the same footing. And to avoid leakage, the mean and SD must come from training folds only. The intercept is never penalised.
The one-standard-error rule
CV error is itself noisy. The 1-SE rule chooses the largest λ whose CV error is within one standard error of the minimum. You get a simpler (more regularised, sparser for LASSO) model that is statistically indistinguishable in accuracy.
Case study: an air-quality sensor model
Predicting CO from 11 standardised sensors ( training rows), λ by 5-fold CV:
| Test R² | Test RMSE | NMHC sensor coef. | |
|---|---|---|---|
| OLS | 0.885 | 0.478 | −0.051 |
| Ridge | 0.885 | 0.477 | −0.006 |
| LASSO | 0.885 | 0.477 | 0.000 |
Prediction: essentially identical. With 6,000 rows and only 11 predictors, OLS already has low variance; regularisation buys little accuracy.
Coefficients: the near-duplicate NMHC sensor (correlation 0.98 with benzene, VIF 61) is shrunk almost to zero by ridge and dropped by LASSO; the inflated benzene coefficient falls from 0.586 to 0.553. Coefficients become more stable and the model cheaper: one fewer sensor to buy.
Takeaway: regularisation's benefit isn't always accuracy. Stability, interpretability and cost can matter as much.
Common mistakes
- Selecting λ on the test set (then the test score is optimistic).
- Standardising on the whole dataset before CV (leakage).
- Forgetting to standardise at all.
- Calling LASSO-selected variables "the causes", or reporting p-values from an OLS refit on them as if no selection happened.
Cheatsheet
Choosing λ
Regularisation & leakage
Check your understanding
Score 75% or more to master this lesson and earn XP. Answer first, then check.