← All AI/ML articles

Regularization, visualised.

Give a model a budget for how big its weights may be. See why Ridge shrinks them, why Lasso deletes them, and how Elastic Net blends both.

1. Drag the target2. See the budget shape3. Watch features survive
Ellipses = how wrong the model is (smaller is better). The shaded shape = the weight budget. The cyan dot is where they touch. Click or drag anywhere to move the white OLS target.
Weight w₁0
Weight w₂0
Weights at exactly 00
Method-
Behind the scenes · 1

Loss plus a penalty

A model with many or large weights can fit noise. Regularisation changes the goal from “minimise the error” to “minimise the error plus λ times the size of the weights”. A bigger λ means a tighter budget. The only difference between the methods is how “size” is measured, and that decides the budget’s shape.


Ridge (L2)
MSE + λ · Σ w²

Round budget. Shrinks every weight smoothly, almost never to exactly zero.

Lasso (L1)
MSE + λ · Σ |w|

Diamond budget with corners on the axes. Can set weights to exactly zero.

Elastic Net
MSE + λ[αΣ|w| + (1−α)Σw²/2]

A blend: some zeros like Lasso, but steadier with correlated features like Ridge.


Convention used on this page. λ is the overall penalty strength and α ∈ [0, 1] is the share given to L1: α = 1 is pure Lasso, α = 0 is pure Ridge. The penalty is always λ[αΣ|w| + (1−α)Σw²/2]. In the top lab the loss is ½(w−w*)ᵀH(w−w*) with H = [[1, link],[link, 1]], which is exactly what the ellipses show. In the 10-feature demo the loss is (1/2n)Σ(y−ŷ)² on standardised features. That matches scikit-learn’s ElasticNet/Lasso (there, alpha = our λ and l1_ratio = our α). scikit-learn’s Ridge uses a different scaling (no 1/2n), so its alpha equals n·λ here. The “Train MSE” tile is Σ(y−ŷ)²/n, which is twice the loss term.

Behind the scenes · 2

Why the diamond makes zeros

The best penalised solution is where the error ellipses first touch the budget shape. A circle is smooth, so the touch point can be anywhere. A diamond has sharp corners on the axes, and ellipses very often touch a corner, which means one weight is exactly zero. Switch to Lasso above and drag the target near an axis to see it.


The “soft threshold” is the heart of Lasso: if the pull toward a weight is smaller than λα, the weight is set to exactly 0. Otherwise it is shrunk by λα and divided by 1 + λ(1−α).

Behind the scenes · 3

Real data: 10 features, only 5 matter

Here the model sees 10 features and 40 examples. Five features truly drive the answer; five are pure noise. x₁ and x₂ are strongly correlated (r ≈ 0.9), so they carry nearly the same signal. Slide λ to see who survives, and watch the test error (on 400 unseen examples) to find the sweet spot. Click the charts to set λ.


real signal (x₁–x₅)noise features (x₆–x₁₀)
Left = tiny penalty, right = huge penalty. Each line is one weight. White line = your λ.
Cyan = train error, pink = test error, star = best λ.
Features kept0
Test MSE0
Train MSE0
Best λ0
Bar = learned weight. White tick = the true weight, on the same standardised scale the model learns on. Pink 0 = removed.

Behind the scenes · 4

The three methods side by side

Same data, same λ, different budget shapes.


Experiment

Try it yourself

Make Lasso delete a weight

Lasso with a firm penalty: one weight lands exactly on the axis.

Near-identical twins

x₁ and x₂ carry the same signal. Lasso tends to keep one. Then switch to Elastic Net.

Too much penalty

Push λ to the far right: every weight shrinks to nothing and the model underfits.

Check yourself

Three quick questions

Key takeaways

  • Regularisation adds a price on large weights, so the model stays simpler and overfits less.
  • Ridge shrinks all weights smoothly and handles correlated features gracefully.
  • Lasso can set weights to exactly zero, so it also selects features.
  • Elastic Net blends both: sparse like Lasso, steadier like Ridge.
  • λ is tuned with held-out data: too small overfits, too large underfits.
← Back to all articles