Foundations to Frontier
Tests
D. Classical Machine Learning › D1 Linear Models for Regression

Lasso Regression (L1 Regularisation and Coordinate Descent)

The lasso adds a penalty λΣ|βⱼ| to least squares. Unlike ridge, it drives many coefficients exactly to zero, so it fits and selects features at the same time. There is no closed form, but cyclic coordinate descent, which updates one coefficient at a time by soft-thresholding, solves it efficiently along a whole path of λ values with warm starts. This page covers the geometry of why L1 produces zeros, soft-thresholding and coordinate descent, λ_max and the regularisation path, the KKT optimality conditions, the Laplace-prior interpretation, and when the lasso does and does not recover the true features. In the exercises you implement coordinate descent, the path with warm starts, KKT checks and the relaxed lasso; then you test how well the lasso finds 5 relevant features among 100, with independent and with correlated distractors.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Keeping only the clues that matter

Ridge tells every weight to be a little smaller. The lasso charges a flat fee for every unit of weight, so a clue that helps only a tiny bit is not worth its fee: its weight drops to exactly zero, and the clue is switched off. You end up with a short list of the clues that really matter, which is easier to understand and cheaper to measure.

Diamond versus disc: where zeros come from
■ lasso (L1): (1.20, 0) · ■ ridge (L2): (1.04, 0.59)
Shrink the budget: the expanding error ellipse usually first touches the diamond at a corner, where one coefficient is exactly 0, while it touches the round disc at a point with both coefficients non-zero. That geometric fact is why the lasso selects features and ridge only shrinks them.
Why engineers care

A model that needs 3 sensors instead of 40 is cheaper to fly, easier to certify and easier to explain. The lasso is a standard way to find a sparse set of predictors, and its sparsity idea underlies compressed sensing and sparse system identification (SINDy).

📝 Quick check (Level 0)
0/3 answered
  1. 1. The lasso can make weights…
  2. 2. In the demo, zeros happen when the ellipse touches…
  3. 3. A sparse model is…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

Soft-thresholding by hand

With orthonormal features (XᵀX/n = I), the lasso solution for each coefficient is the least-squares value z, moved toward 0 by λ and set to 0 if it would cross:

β = sign(z)·max(|z| − λ, 0).

OLS value z λ = 1 λ = 2
3.0 2.0 1.0
−0.5 0 0
−2.0 −1.0 0

Ridge in the same situation would give z/(1 + λ), smaller but never zero.

Coordinate descent in plain English:

  1. Standardise the features and centre y.
  2. Start with all weights at 0.
  3. Repeatedly visit each feature. Compute how well it explains what the other features leave unexplained, then soft-threshold that number.
  4. Stop when no weight changes.

Run this for a decreasing list of λ values, starting each from the previous answer (warm start).

📝 Quick check (Level 1)
0/3 answered
  1. 1. Soft-threshold of 2.5 with λ = 1?
  2. 2. Soft-threshold of −0.3 with λ = 1?
  3. 3. Warm starts mean…

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 35 minUndergraduate students and practising engineers.

Objective, algorithm and tuning

Objective: minimise (1/2n)‖y − Xβ‖² + λ‖β‖₁. It is convex but not differentiable at βⱼ = 0.

Coordinate update. Let r be the residual and ρⱼ = (1/n)xⱼᵀr + (‖xⱼ‖²/n)βⱼ. Then

βⱼ ← S(ρⱼ, λ)/(‖xⱼ‖²/n), where S is soft-thresholding.

Each update costs O(n) using a running residual.

λ_max = max_j |xⱼᵀy|/n. At and above it, every coefficient is 0. The path runs over a log grid from λ_max down to about 10⁻³λ_max.

KKT conditions:

  • for active features, xⱼᵀr/n = λ·sign(βⱼ);
  • for inactive features, |xⱼᵀr/n| ≤ λ.

Relaxed lasso: refit least squares on the selected features to undo the shrinkage bias, or blend the two fits.

from sklearn.linear_model import LassoCV, lasso_path
from sklearn.pipeline import make_pipeline; from sklearn.preprocessing import StandardScaler
model = make_pipeline(StandardScaler(), LassoCV(cv=5)).fit(X, y)
print(model[-1].alpha_, (model[-1].coef_ != 0).sum(), "features kept")

Hyperparameter: λ (sklearn’s alpha). Choose it by CV for prediction; a larger λ (the one-standard-error rule) gives sparser, more stable selections. Use it when you expect few relevant features or need a compact model. Be careful when features are strongly correlated: the lasso picks one of a group somewhat arbitrarily (use the elastic net, D1.7).

🐍 Exercise 1: lasso by coordinate descent
Implement soft-thresholding, λ_max, cyclic coordinate descent with a running residual, the regularisation path with warm starts, a KKT optimality check and the relaxed lasso; verify that the solution is zero at λ_max, equals OLS as λ → 0, satisfies the KKT conditions, and that refitting reduces the shrinkage of a strong coefficient.
📝 Quick check (Level 2)
0/3 answered
  1. 1. Above λ_max the lasso solution is…
  2. 2. For an inactive feature, KKT requires…
  3. 3. The relaxed lasso…

Level 3: Advanced

★★★★☆ Advanced⏱ 35 minGraduate level, with full derivations and proofs.

Theory

Why zeros. The subdifferential of |βⱼ| at 0 is [−1, 1]. So βⱼ = 0 is optimal whenever the gradient of the loss lies in [−λ, λ], which is a whole interval of data configurations. The L1 ball’s corners make sparse solutions generic (see the demo).

Bayesian view. The lasso is the MAP estimate under independent Laplace priors p(βⱼ) ∝ exp(−|βⱼ|/b). (The posterior mean is not sparse.)

Algorithms:

  • coordinate descent (glmnet: Friedman, Hastie and Tibshirani, 2010);
  • proximal gradient methods (ISTA, and FISTA with O(1/k²));
  • LARS, which traces the exact piecewise-linear path (D1.8);
  • ADMM.

Screening rules (strong rules) discard features that are certainly inactive.

Recovery theory. With n ≳ s·log p samples for s true features, a suitable λ ∝ σ√(log p / n) gives prediction error O(σ²s·log p/n) under restricted-eigenvalue conditions. Exact support recovery also needs the irrepresentable condition: irrelevant features must not be too correlated with the relevant ones. And the prediction-optimal λ typically selects extra noise features.

Limitations:

  • at most n non-zero coefficients when p > n;
  • it picks one of a group of correlated features;
  • the shrinkage biases large coefficients. Remedies: relaxed lasso, adaptive lasso, SCAD and MCP penalties, elastic net.

Interview questions.

  • Why standardise before the lasso? The penalty treats all coefficients equally, so unscaled features get unequal penalties.
  • Why is the lasso unstable with correlated features? Several near-equivalent subsets fit equally well, and small data changes flip which one is chosen. Stability selection addresses this.
📝 Quick check (Level 3)
0/3 answered
  1. 1. The lasso is MAP estimation under…
  2. 2. Exact support recovery needs roughly…
  3. 3. With p > n, the lasso selects at most…

Level 4: Expert

★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.

Does the lasso find the right features?

History. Tibshirani introduced the lasso in 1996, following Breiman’s non-negative garrote (1995) and Chen, Donoho and Saunders’ basis pursuit (1998). Efron, Hastie, Johnstone and Tibshirani’s LARS (2004) and Friedman, Hastie and Tibshirani’s glmnet (2010) made it fast. Zhao and Yu (2006) characterised when it selects correctly. Compressed sensing (Candès, Romberg and Tao; Donoho, 2006) built on the same L1 idea.

Exercise 1.

  • λ_max = 2.75, where every coefficient is 0, and λ → 0 reproduces OLS.
  • At λ = 0.1 the solution satisfies the KKT conditions and keeps 10 of 20 features, including all 3 true ones.
  • The relaxed refit moves a shrunk coefficient from 2.55 back to 2.92 (truth 3).

Exercise 2: 5 relevant features among 100. Setup: n = 120, noise σ = 1; λ chosen on a validation set; FISTA solver; means over 15 datasets.

Features Lasso test MSE Ridge test MSE Selected True found (of 5) False selected Sparser λ: selected / true / false
independent 1.23 2.59 21.7 5.0 16.7 9.7 / 5.0 / 4.7
correlated distractors 1.18 1.75 20.9 4.0 16.9 13.5 / 4.1 / 9.4

The sparser λ is the largest whose validation error is within 10% of the best.

  • With a sparse truth, the lasso predicts twice as well as ridge.
  • The λ that is best for prediction keeps about 17 noise features alongside the 5 real ones. Prediction and selection want different λ values.
  • A slightly larger λ halves the false selections at a 6% cost in error.
  • When irrelevant features are correlated with the relevant ones (violating the irrepresentable condition), the lasso misses true features and keeps twice as many false ones, whatever λ.
  • Use the selected set as a candidate list, not as a discovery. Check stability across resamples.
🐍 Exercise 2 (research): feature selection with the lasso
Generate data with 5 relevant features out of 100, with independent features or with distractors correlated with the relevant ones; tune λ by validation (FISTA solver); report test error against ridge and relaxed lasso, and how many true and false features are selected at the prediction-optimal λ and at a sparser λ.

The lasso in AI, robotics and aerospace

  • Sparse system identification (SINDy): discovers governing equations by selecting a few terms from a library of candidate functions.
  • Sensor selection: choose a minimal subset of sensors that predicts a quantity of interest.
  • Compressed sensing: MRI and radar reconstruction from few measurements.
  • Interpretable baselines: compact models for certification reviews.

References

  1. R. Tibshirani (1996). “Regression shrinkage and selection via the lasso.” JRSS B 58(1).
  2. J. Friedman, T. Hastie, R. Tibshirani (2010). “Regularization paths for generalized linear models via coordinate descent.” Journal of Statistical Software 33(1).
  3. P. Zhao, B. Yu (2006). “On model selection consistency of lasso.” JMLR 7.
  4. A. Beck, M. Teboulle (2009). “A fast iterative shrinkage-thresholding algorithm for linear inverse problems.” SIAM J. Imaging Sciences 2(1).
  5. S. L. Brunton, J. L. Proctor, J. N. Kutz (2016). “Discovering governing equations from data by sparse identification of nonlinear dynamical systems.” PNAS 113(15).