Skip to content
Lambdian

Ridge, lasso and elastic net

Regularised regression: shrink weights to fight overfitting, and let lasso select features.

Animated explanation

The idea

When a model overfits, its weights are usually large: big positive and negative weights cancel each other to thread through noisy points. Regularisation adds a price for large weights to the loss, so the model only uses a big weight when the data truly demands it.

Ridge charges the sum of squared weights and shrinks all of them smoothly. Lasso charges the sum of absolute values, and its solutions put many weights at exactly zero, so it also selects features. Elastic net mixes the two.

One knob, λ\lambda, sets the price. λ=0\lambda = 0 is plain least squares; a huge λ\lambda squashes every weight to zero. Pick it by cross-validation.

Penalty = budget

Minimising loss + λ⋅\lambda\cdotpenalty is equivalent to minimising the loss subject to the penalty staying under some budget tt (Lagrange duality). Picture the loss as elliptical contours around the least-squares answer. The regularised answer is where the smallest contour touches the allowed region.

For ridge the region is a ball: the contact point can be anywhere on its smooth surface. For lasso it is a diamond (a cross-polytope in higher dimensions) whose corners sit on the axes. Contours hit corners far more often than flat faces, and at a corner some weights are exactly zero.

Practical rules

  • Standardise features first. The penalty treats all weights alike, so a feature measured in grams would be punished more than the same feature in kilograms.
  • Do not penalise the intercept. Centre yy or leave bb out of the penalty.
  • Correlated features: lasso tends to keep one of a correlated group arbitrarily; ridge spreads weight across them; elastic net does a bit of both.
  • Choose λ on a log grid (10−410^{-4} to 10210^{2}) with cross-validation.

The Bayesian view

Ridge is maximum a posteriori estimation with a Gaussian prior wj∼N(0,σ2/λ)w_j \sim \mathcal{N}(0, \sigma^2/\lambda); lasso uses a Laplace prior, which has a sharp peak at zero. Regularisation is prior belief that most effects are small.

The algorithm, step by step

  1. 1
    Standardise each feature to mean 0 and variance 1; centre yy.
  2. 2
    Ridge: solve (X⊤X+λI)w=X⊤y(X^\top X + \lambda I)\mathbf{w} = X^\top\mathbf{y}.
  3. 3
    Lasso: start at w=0\mathbf{w} = 0; repeatedly, for each jj, compute ρj=1n∑ixij(yi−∑k≠jxikwk)\rho_j = \frac1n\sum_i x_{ij}(y_i - \sum_{k\ne j}x_{ik}w_k) and set wj=S(ρj,λ)/zjw_j = S(\rho_j, \lambda)/z_j.
  4. 4
    Repeat for a grid of λ values; keep the λ with the best cross-validated error.

The maths

Ridge

min⁡w ∥y−Xw∥2+λ∥w∥22  ⇒  w∗=(X⊤X+λI)−1X⊤y\min_{\mathbf{w}}\ \lVert\mathbf{y} - X\mathbf{w}\rVert^2 + \lambda\lVert\mathbf{w}\rVert_2^2 \;\Rightarrow\; \mathbf{w}^\ast = (X^\top X + \lambda I)^{-1}X^\top\mathbf{y}

Lasso (scikit-learn's scaling)

min⁡w 12n∥y−Xw∥2+λ∥w∥1\min_{\mathbf{w}}\ \frac{1}{2n}\lVert\mathbf{y} - X\mathbf{w}\rVert^2 + \lambda\lVert\mathbf{w}\rVert_1

Soft-thresholding

S(ρ,λ)=sign⁡(ρ) max⁡(∣ρ∣−λ, 0)S(\rho, \lambda) = \operatorname{sign}(\rho)\,\max(|\rho| - \lambda,\ 0)

Elastic net

12n∥y−Xw∥2+λ(α∥w∥1+1−α2∥w∥22)\frac{1}{2n}\lVert\mathbf{y} - X\mathbf{w}\rVert^2 + \lambda\Big(\alpha\lVert\mathbf{w}\rVert_1 + \frac{1-\alpha}{2}\lVert\mathbf{w}\rVert_2^2\Big)

Strengths and weaknesses

Strengths

  • Reduces variance and overfitting with one knob
  • Ridge handles correlated features and d>nd > n
  • Lasso gives sparse, interpretable models
  • Cheap to fit along a whole λ path

Weaknesses

  • Adds bias: weights are pulled towards zero
  • Needs standardised features
  • Lasso picks arbitrarily among correlated features

Reach for it when…

A linear model overfits, features are many or correlated, or you want the model to keep only the features that matter (lasso).

Check your understanding

Question 1

Q1Why do lasso solutions often contain exact zeros while ridge solutions do not?

Question 2

Q2You forgot to standardise features before ridge. What goes wrong?

Question 3

Q3What happens as λ → ∞ in ridge regression (intercept unpenalised)?

Lambdian Lab

Ridge in closed form, lasso by coordinate descent

Implement both regularised regressions on standardised data with a centred target (so there is no intercept to worry about).

  • ridge(X, y, lam): (X⊤X+λI)−1X⊤y(X^\top X + \lambda I)^{-1}X^\top y, solved with np.linalg.solve.
  • soft_threshold(rho, lam).
  • lasso(X, y, lam, iters): coordinate descent for scikit-learn's objective 12n∥y−Xw∥2+λ∥w∥1\frac{1}{2n}\lVert y - Xw\rVert^2 + \lambda\lVert w\rVert_1.
Open the labPython + NumPy + scikit-learn runs in your browser. Sign-in keeps your code and attempts.