Skip to content
AI360Xpert
Beta
Playgrounds

Supervised Learning

Ridge & Lasso Regularization

A penalty on coefficient size buys generalisation with training error — and L2 shrinks every coefficient towards zero while L1 sets some of them exactly to zero.

Stage 1 of 5: 1. Eight powers, sixteen points

Ridge (L2) at lambda 0.0032. The fitted curve spans 2.90 across the plotted range. Training error 0.239.

Ridge (L2) at lambda 0.0032. 8 of 8 coefficients are non-zero, with a combined size of 3.874. Surviving powers: x, x^2, x^3, x^4, x^5, x^6, x^7, x^8.

  • Training point
  • Held-out point
  • Generating function
  • Fitted model
  • Coefficient the target uses
  • Redundant coefficient
Training Error0.239Training Error: 0.239
Active Coefficients8Active Coefficients: 8

With the penalty turned all the way down, the fit hands a weight to every power it is offered, including the six the target never used. The curve threads the training points, and wanders away from the cubic that generated them.

  1. Penalty strength
  2. Coefficient sizes
  3. Curve shape
  4. Held-out error

Check your understanding

A penalty on coefficient size buys generalisation with training error. Watch L2 shrink all eight coefficients together and never reach zero, watch L1 switch most of them off entirely, and watch both models fall apart when the penalty goes too far.

The model in this lab is offered eight features — x,x2,…,x8x, x^2, \dots, x^8 — to fit sixteen noisy points. The points were generated by a cubic, y=2x−1.5x3+εy = 2x - 1.5x^3 + \varepsilon, so six of those eight features are pure decoration. Least squares does not know that, and uses all eight anyway.

Regularization is what you add to the loss to stop it. Instead of minimising the error alone, the model minimises the error plus a penalty on how large its coefficients are:

J(w,b)=12n∑i(yi−b−zi⋅w)2+P(w)J(w, b) = \frac{1}{2n}\sum_i \left(y_i - b - z_i \cdot w\right)^2 + P(w)

Ridge (L2 penalty)

Ridge sets P(w)=λ∑jwj2P(w) = \lambda \sum_j w_j^2. Differentiate it and the gradient is 2λwj2\lambda w_j — which shrinks towards nothing as wjw_j does. So ridge can pull a coefficient arbitrarily close to zero and never arrive. In the lab, every one of the eight bars shortens together, and at the strongest penalty on the control all eight are still standing.

That is often exactly what you want. When a signal is genuinely spread across several correlated features, discarding any one of them throws away information the others cannot fully replace, and shrinking them all is the better trade.

Lasso (L1 penalty)

Lasso sets P(w)=λ∑j∣wj∣P(w) = \lambda \sum_j |w_j|, and the absolute value has no derivative at zero — it has a corner. Its subgradient there spans [−λ,+λ][-\lambda, +\lambda], so whenever the unpenalised pull on a coefficient falls inside that interval, zero genuinely solves the stationarity condition and the coefficient is switched off. That is the soft-thresholding operator:

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

Because six of this dataset's eight features really are redundant, removing them beats shrinking them: lasso reaches a held-out error of 0.663 where ridge floors at 0.728. And the two features it keeps hold of hardest are xx and x3x^3 — the two the target was built from.

Standardise before you penalise

The penalty is applied to ww, not to the features, so it only means the same thing in every column if the columns are on the same scale. These are not: the mean squares of xx through x8x^8 over this domain span 0.85 to 91. A large column needs a tiny weight to do the same work, so it pays almost no penalty.

This matters more than it sounds. Run lasso on the unstandardised columns and it keeps x7x^7 and x8x^8 and throws away xx and x3x^3 — precisely the wrong answer, arrived at confidently. The lab standardises each column using the training set's own mean and standard deviation, which is why λ\lambda is comparable across features at all.

Where it breaks

Push λ\lambda past 0.497 with lasso selected and there is nothing left. Every coefficient is zero, the intercept survives untouched — the penalty never applies to it — and the model predicts the training mean, −0.307-0.307, for every input. The curve is a flat line, and held-out error climbs to 1.172, worse than at any other setting on the control.

That value is not a coincidence, and the lab marks it. It is λmax⁡=max⁡j∣1nzj⊤(y−yˉ)∣\lambda_{\max} = \max_j \left|\tfrac{1}{n} z_j^\top (y - \bar{y})\right|: the largest correlation between any standardised feature and the centred target. Above it, zero satisfies the stationarity condition for all eight coefficients at once.

Being simple is not the same as being right. A model that is wrong everywhere has not been regularised — it has been deleted.

Reference

Objective
J(w, b) = (1/2n) * sum_i (y_i - b - z_i . w)^2 + P(w)
Ridge (L2)
P(w) = lambda * sum_j w_j^2 — shrinks every coefficient, reaches zero at none
Lasso (L1)
P(w) = lambda * sum_j |w_j| — shrinks, and switches coefficients off outright
Soft threshold
S(rho, lambda) = sign(rho) * max(|rho| - lambda, 0) — where the exact zeros come from
Standardise first
z_ij = (x_i^j - mean_j) / sd_j — so one unit of weight costs the same in every column
Collapse point
lambda_max = max_j |(1/n) * z_j . (y - mean(y))| = 0.497 on this data
Intercept
b = mean(y) exactly, at every lambda: the penalty never touches b

Break it on purpose

Push the penalty past lambda = 0.497 on Lasso and every coefficient is driven to exactly zero. The model predicts the training mean, -0.307, for every input; the fitted curve is a flat line; and held-out error climbs to 1.172, worse than at any other setting on the control.