Search

Search papers and lessons

Lasso Regression

Level 3 · 13 min read

Lasso regression is linear regression with an L1 penalty on the weights, which shrinks coefficients and sets many of them exactly to zero, so it selects a small set of features.

On this page

  • Colour = topic mix
  • Calculus
  • Linear algebra
  • Losses & regularization
  • Optimization
  • Probability
  • Learning basics
  • Sign in to see your progress

TL;DR

Lasso regression fits a linear model by minimising squared error plus a penalty equal to the sum of the absolute values of the weights. The penalty shrinks every weight towards zero, and because its corners sit on the axes, it often drives some weights exactly to zero. The result is a model that uses only a few of the available features, which makes it easier to read and less prone to overfitting when many features are noise.

Where you'll see it: in genomics, where thousands of gene measurements are used to predict a trait from a few dozen samples; in economics and marketing models with many candidate variables; and as a feature-selection step before a more complicated model.

Intuition

Suppose you have two hundred candidate features and only a few truly matter. Ordinary linear regression uses all two hundred, and each one picks up a little of the noise in the training data. The fitted model looks perfect on the data it was trained on and does poorly on new data. This is overfitting, and the usual cure is to penalise large weights.

Ridge regression, which uses an L2 penalty, shrinks all the weights smoothly. It makes them smaller but almost never zero. A lasso penalty works differently. It charges a fixed price for each unit of weight, whether the weight is large or small. Near zero, that price is relatively expensive compared with the gain in fit from keeping a tiny weight. So many weights are pushed all the way to zero.

The picture is geometric. Imagine the error as a bowl centred on the least-squares solution, and the penalty as a region around the origin. For L2 the region is a disc, with no corners. For L1 it is a diamond, with corners on the axes. The first point where the expanding bowl touches the region is usually a corner of the diamond, and a corner means some weights are zero.

The plot below shows the one-dimensional version, which captures the whole mechanism. The soft-threshold map takes an unpenalised weight yy and returns sign⁡(y)max⁡(∣y∣−λ,0)\operatorname{sign}(y)\max(|y| - \lambda, 0). Small weights are set to zero, and large ones are shrunk by λ\lambda.

−4−3−2−101234−4−2024unpenalised weight yfitted weight
  • lasso, lambda = 1
  • ridge-style shrink, factor 1/2
  • no penalty
The lasso map is flat at zero for small inputs and shifts large inputs towards zero by lambda. A ridge-style shrink never reaches zero.

The math

Definition

Definition: Lasso regression

Given nn examples with feature matrix X∈Rn×pX \in \mathbb{R}^{n \times p}, targets y∈Rn\mathbf{y} \in \mathbb{R}^{n}, and a penalty strength λ≥0\lambda \ge 0, the lasso chooses weights w\mathbf{w} to minimise

12n ∥y−Xw∥22+λ ∥w∥1,∥w∥1=∑j=1p∣wj∣.\frac{1}{2n}\,\|\mathbf{y} - X\mathbf{w}\|_2^2 + \lambda\,\|\mathbf{w}\|_1, \qquad \|\mathbf{w}\|_1 = \sum_{j=1}^{p} |w_j|.

The first term is the average squared error of the linear regression model. The second term is the L1 norm of the weights, scaled by λ\lambda. Setting λ=0\lambda = 0 recovers ordinary least squares. As λ\lambda grows, the penalty dominates and more weights are driven to zero.

The factor 12n\tfrac{1}{2n} is a convention. Some texts omit it or use a sum rather than an average, which changes the meaning of λ\lambda by a constant. Always check which scaling a source uses before comparing values of λ\lambda.

Why the L1 penalty is convex

The squared-error term is convex in w\mathbf{w}, and the absolute value is convex. Their sum is convex, so the lasso has no bad local minima. This matters for the algorithms, which can rely on the optimality conditions of subgradients. The objective is not differentiable where any wj=0w_j = 0, so gradient descent alone is not enough, and the usual solvers use coordinate descent.

Optimality condition

For each coordinate jj, the subgradient condition at the minimiser says that zero lies in the subdifferential of the objective. Write ρj=1nxj⊤r−j\rho_j = \tfrac{1}{n}\mathbf{x}_j^{\top}\mathbf{r}_{-j} for the average product of feature jj (column xj\mathbf{x}_j of XX) and the residual r−j\mathbf{r}_{-j} that remains when all other features are used. Under the standard normalisation, with each column of XX having average squared value 1, the condition gives

wj=sign⁡(ρj) max⁡(∣ρj∣−λ, 0).w_j = \operatorname{sign}(\rho_j)\,\max\big(|\rho_j| - \lambda,\ 0\big).

If ∣ρj∣≤λ|\rho_j| \le \lambda, the weight is exactly zero. Otherwise it is shrunk by λ\lambda from the value that ordinary regression would give. This is the same soft-threshold rule that solves the one-dimensional problem in L1 Minimization.

Coordinate descent

Coordinate descent solves the lasso by cycling through the weights. For each jj, hold the other weights fixed, compute ρj\rho_j from the current residual, and apply the soft-threshold rule to update wjw_j. Repeat until nothing changes much. Each update is exact and cheap, and the method converges for the lasso because the objective is convex and the penalty is separable across coordinates. See Coordinate Descent for the general idea.

Choosing lambda

The penalty λ\lambda is a hyperparameter, so it is chosen by validation, not by fitting to the training data. A common approach fits the lasso over a grid of values from large to small, which starts with an all-zero model and adds features one at a time, then picks the value with the best error on held-out data. Cross-validation over the same grid is the standard tool, as described in Train, Validation and Test Sets.

Worked examples

Example
One weight, by hand

Minimise 12(w−y)2+λ∣w∣\tfrac{1}{2}(w - y)^2 + \lambda |w| with y=3y = 3 and λ=1\lambda = 1. This is the one-dimensional lasso with an unpenalised weight of 33.

  1. Optimality: w−y+λs=0w - y + \lambda s = 0, where s=sign⁡(w)s = \operatorname{sign}(w) if w≠0w \ne 0.
  2. Since ∣y∣=3>λ=1|y| = 3 \gt \lambda = 1, the solution is nonzero. Use s=1s = 1: w=y−λ=3−1=2w = y - \lambda = 3 - 1 = 2.
  3. Check the condition: w−y+λs=2−3+1⋅1=0w - y + \lambda s = 2 - 3 + 1 \cdot 1 = 0. The subgradient is s=1s = 1, which is a valid value, so w=2w = 2 is the minimiser.
  4. Compare with the unpenalised answer w=3w = 3. The penalty shrank the weight by exactly λ=1\lambda = 1.

Interpretation: the penalty did not remove this feature, but it made the weight smaller. If yy had been 0.50.5 instead, the same calculation would give w=0w = 0, and the feature would be dropped.

Example
Lasso versus ridge on orthonormal features

Suppose the features are orthonormal, so the least-squares weights are y1=3y_1 = 3 and y2=0.5y_2 = 0.5. Use the penalty strength λ=1\lambda = 1, with each penalty applied in the same units as the squared error.

  1. Lasso: apply the soft threshold to each weight. w1=sign⁡(3)max⁡(3−1,0)=2w_1 = \operatorname{sign}(3)\max(3 - 1, 0) = 2. w2=sign⁡(0.5)max⁡(0.5−1,0)=0w_2 = \operatorname{sign}(0.5)\max(0.5 - 1, 0) = 0.
  2. Ridge with the penalty λ2∥w∥22\tfrac{\lambda}{2}\|w\|_2^2: the minimiser for each weight is yj/(1+λ)y_j / (1 + \lambda). So w1=3/2=1.5w_1 = 3/2 = 1.5 and w2=0.5/2=0.25w_2 = 0.5/2 = 0.25.
  3. Compare. Ridge keeps both features, shrunk by the same factor. Lasso keeps the strong feature, shrunk by a fixed amount, and drops the weak one entirely.
  4. Count the nonzero weights as λ\lambda rises. Feature jj survives exactly when ∣yj∣>λ|y_j| \gt \lambda. With y=(3,2.2,0.5,0.1)y = (3, 2.2, 0.5, 0.1), the count is 3 at λ=0.4\lambda = 0.4, 2 at λ=1\lambda = 1, and 1 at λ=2.5\lambda = 2.5.

Interpretation: under orthonormal features the lasso's selection rule is transparent. Only features whose unpenalised weight beats λ\lambda in size survive. Real features are correlated, and then the pattern is less clean, as the next section explains.

Example
Correlated features: the lasso picks one

Two features are nearly copies of each other, so their correlation is close to 1. Both would carry the same signal, and the least-squares weights are unstable: small changes in the data can move weight between them.

  1. Two exact copies of one signal give the same fit for any pair of weights with the same sum c=w1+w2c = w_1 + w_2. The L1 penalty ∣w1∣+∣w2∣|w_1| + |w_2| is at least ∣c∣|c| and equals ∣c∣|c| for every split where both weights share a sign. So the penalty alone does not choose a split, and the lasso solution is not unique.
  2. Small differences between the copies, or noise in the data, break the tie. The lasso may then put nearly all the weight on one copy, and which copy it picks can change when the data are resampled.
  3. Ridge's penalty w12+w22w_1^2 + w_2^2 is smallest for a fixed sum when the two weights are equal, so ridge splits the weight roughly evenly and is stable here.

Interpretation: lasso selection is stable across many distinct features but can be unstable among near-duplicates. This is why elastic net, which mixes L1 and L2 penalties, is often preferred when features come in correlated groups.

Tradeoffs and failure modes

SituationWhat goes wrongBetter choice
Many noisy features and few samplesOrdinary regression overfits badlyLasso, with λ\lambda chosen by cross-validation
Features are correlated in groupsLasso picks one member and drops the others, and the choice can be unstableElastic net, which adds an L2 term
Signal is spread over many small effectsLasso zeros out real but weak effects and loses predictive powerRidge, which keeps all features with shrinkage
Features on different scalesThe penalty charges large-scale features less, so results depend on unitsStandardise the features before fitting
Reading the selected features as causesSelection reflects prediction, and correlated variables can be swappedTreat the selection as a prediction tool, and test causal claims separately
Choosing lambda on the training dataThe smallest penalty always fits best, so the model is never sparseChoose λ\lambda on validation data or by cross-validation

The main tradeoff is between sparsity and stability. The lasso gives a short, interpretable model, but the set of selected features can change when the data changes. Ridge is more stable and keeps everything, which can be better when no feature is truly irrelevant. The right choice depends on whether the goal is a compact model or the best prediction.

History

Least squares was developed by Legendre and Gauss around 1800, and ridge-style shrinkage was introduced by Hoerl and Kennard in 1970. Those methods shrink coefficients but keep all of them. The idea of an L1 penalty for selection appeared earlier in signal processing and geophysics, where people wanted sparse solutions.

Robert Tibshirani's 1996 paper gave the lasso its name and its statistical framing. It showed that an L1 penalty on the coefficients both shrinks and selects, and it gave a practical algorithm. The name is an acronym for "least absolute shrinkage and selection operator".

In the early 2000s, Donoho and collaborators connected L1 minimisation to compressed sensing, where a signal can be recovered from far fewer measurements than usual if it is sparse. That work gave the lasso a theoretical foundation and made sparse modelling standard in signal processing and machine learning. Efficient coordinate-descent solvers, popularised around 2010, made it practical for very large problems.

Check yourself

QuestionWhat does the lasso penalty add to the squared-error objective, and what does setting lambda to zero give?Show answer

It adds λ\lambda times the sum of the absolute values of the weights. With λ=0\lambda = 0 the penalty vanishes and the objective is ordinary least squares, so the lasso reduces to linear regression.

QuestionWith an unpenalised weight of y = 2 and lambda = 0.5 in the one-dimensional lasso, what is the fitted weight? What about y = 0.3?Show answer

For y=2y = 2, the solution is sign⁡(2)max⁡(2−0.5,0)=1.5\operatorname{sign}(2)\max(2 - 0.5, 0) = 1.5. For y=0.3y = 0.3, ∣y∣≤λ|y| \le \lambda, so max⁡(0.3−0.5,0)=0\max(0.3 - 0.5, 0) = 0 and the fitted weight is 00. The feature is dropped.

QuestionUnder orthonormal features with unpenalised weights 4, 1.5 and 0.2, how many weights remain nonzero at lambda = 1?Show answer

A weight survives when its absolute value exceeds lambda. Here 4>14 \gt 1 and 1.5>11.5 \gt 1 survive, while 0.2<10.2 \lt 1 does not. Two weights remain nonzero. The surviving weights are shrunk to 33 and 0.50.5.

QuestionTwo nearly identical features carry the same signal. Why is the lasso's choice between them unstable, while ridge's is stable?Show answer

For a fixed total weight, the L1 penalty is the same for many splits between the copies, so the penalty cannot decide and small differences in the data do. The choice can then shift when the data are resampled. Ridge's squared penalty is smallest when the weight is shared evenly, so it gives a unique, stable split.

QuestionIn a practical study with many candidate features, why must lambda be chosen on data not used to fit the weights?Show answer

The training error always decreases as lambda decreases, so the training data alone would choose no penalty and keep every feature. Validation or cross-validation measures how well the model predicts new data, which is the quantity that the penalty is meant to improve.

Sources