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.

Not startedJump to quiz

The procedure

  1. Split off a test set and leave it alone.
  2. Choose a grid of λ values, log-spaced (e.g. to ).
  3. 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.
  4. Pick the λ with the lowest mean CV error (or use the 1-SE rule below).
  5. Refit on all training data with that λ.
  6. Evaluate once on the test set.
0.811.21.41.6-3-2-10123log₁₀ λCV errormin CV1-SE rule
Mean CV error
Small λ (left) overfits; large λ (right) underfits. Bars show ± 1 standard error across folds. The 1-SE rule picks the largest λ within one SE of the minimum: a simpler model with nearly the same accuracy.

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 RMSENMHC sensor coef.
OLS0.8850.478−0.051
Ridge0.8850.477−0.006
LASSO0.8850.4770.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 λ

Split off test → log-spaced λ grid → K-fold CV (standardise inside folds) → pick min CV or 1-SE → refit on all training → test once.

Regularisation & leakage

Standardise within training folds. Never choose λ on the test set.

See every cheatsheet by category →

Check your understanding

Score 75% or more to master this lesson and earn XP. Answer first, then check.

Q1conceptHow should be chosen?
Q2diagnose"We standardised using the whole dataset, then cross-validated ." What's the issue?
Q3conceptWhat does the one-standard-error rule choose?
Q4interpretOLS, ridge and LASSO reach test RMSE of 0.478, 0.477 and 0.477 on a dataset with 6,000 rows and 11 predictors. Was regularisation pointless?
Q5conceptWhy must predictors be standardised before ridge or LASSO?