Home / Regularisation

Ridge and Lasso Regression

Turn the penalty up and watch a wild polynomial calm down. Ridge shrinks every coefficient; Lasso deletes them.

Parameters

0.001

log scale, 1e-6 to 1e+2

9
0.20
18

The Fit

degree 9
fitted model true curve training points held-out points

Coefficients

Bar height is |coefficient|, on a log scale. Grey means driven to exactly zero.

Error

Train MSE 0.000
Test MSE 0.000
Overfit Gap 0.000

Model Size

Non-zero Coefficients
10
Largest |w| 0.0
Sum of |w| 0.0

Overview

Regularisation discourages a regression model from using unnecessarily large coefficients. Ridge uses a squared penalty; Lasso uses an absolute-value penalty. That difference affects whether fitted coefficients can become exactly zero.

What the penalties change

Ordinary least squares minimises the sum of squared residuals. Ridge and Lasso add a penalty controlled by λ:

Ridge: SSE + λ Σ wᵢ²

Lasso: SSE + λ Σ |wᵢ|

At λ = 0, both reduce to ordinary least squares. As λ grows, large coefficients become more costly and the fit is pulled toward smaller coefficients. In the extreme, predictions lose their feature-dependent variation.

Ridge’s smooth squared penalty usually shrinks coefficients but leaves them nonzero. Lasso’s absolute-value penalty has a sharp point at zero; the optimum can land exactly there. Lasso can therefore produce a sparse model, though which features survive may be unstable when predictors are strongly correlated.

See the effect on a flexible fit

  1. Set Cost Type to None and Polynomial Degree to 12. A high-degree curve can fit the training points closely while behaving erratically between them.
  2. Switch to Ridge and increase Lambda. The curve becomes smoother as coefficients shrink; training error may rise while test error falls.

The visualiser uses a small synthetic dataset. In a real problem, select λ using validation data or cross-validation—not the final test set.

Which one should you choose?

MethodUseful whenWatch for
Ridge (L2)Many features have some signal, especially when predictors are correlatedCoefficients shrink together and are rarely exactly zero.
Lasso (L1)A sparse coefficient vector is useful and many features may be irrelevantIt can select one of several correlated features arbitrarily.
Elastic NetYou want sparsity but also need to handle correlated groupsIt combines L1 and L2 penalties and has an additional mixing parameter.

Scale numeric features before applying coefficient penalties when their units differ. Otherwise the same real-world effect can require different coefficient sizes and receive different penalty strength. Fit scaling inside each training fold; see feature scaling.

Limits and interpretation

Regularisation can reduce overfitting, but it does not guarantee better predictions. A nonzero coefficient is not proof that a feature is important, and a zero Lasso coefficient does not prove the feature has no relationship with the target. With correlated predictors, multiple coefficient patterns may predict similarly. Check performance on held-out data and interpret coefficients in the context of the feature scale and model assumptions.

Sources and next steps

See scikit-learn’s linear-model documentation for Ridge and Lasso estimators. Continue with cross-validation to select regularisation strength.

Predict, then reveal

About to run: set Training Points to its maximum (40). Before it does — what happens to the readout?

Committing to an answer first is the point — the reveal runs the experiment on the visualisation above and reads the real value back, so nothing here is scripted.

Recall check

0 of 3

Say the answer out loud before you reveal it — recalling it is what makes it stick, and rereading it is not.

  1. What does this module say about “How regularisation affects coefficients”?

  2. What does this module say about “What the penalties change”?

  3. What does this module say about “See the effect on a flexible fit”?

Cheat sheet

Ridge and Lasso Regression

Turn lambda up and watch a wild polynomial calm down. Ridge shrinks every coefficient; Lasso drives the useless ones to exactly zero.

MACHINE LEARNING · vizlearn.in/machine_learning/ridge_and_lasso_regression.html

Further reading

About the author

Ashish Jangra builds and maintains VizLearn. Every module here is written and the visualisation behind it hand-built, so the numbers in a readout come from the same code that draws the picture. Corrections are genuinely welcome and get priority over everything else — if a page states something wrong, or an animation misrepresents what the algorithm does, get in touch.