Home / Regularisation

Ridge and Lasso Regression

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

Overview

Quick Context

Give a degree-12 polynomial eighteen noisy points and it will pass through nearly all of them. The fit looks superb on the data it has seen, and it is worthless: between the points the curve swings violently, because the only way to hit every point is to use enormous coefficients that cancel each other out.

Regularisation attacks this directly. Instead of asking the model only to fit the data, you ask it to fit the data and keep its coefficients small, and you decide how much the second part matters.

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

Ridge and Lasso: A Practical Guide

Two ways to punish a model for being complicated, and why only one of them deletes features.

One extra term

Ordinary least squares minimises the squared error alone. Ridge and Lasso add a penalty on the size of the weights:

Ridge:   minimise   SSE + λ · Σ wi2

Lasso:   minimise   SSE + λ · Σ |wi|

λ (lambda) is the dial. At λ = 0 both reduce exactly to OLS. As λ grows the penalty dominates and every coefficient is pushed toward zero, until at very large λ the model gives up and predicts a flat line through the mean.

The only difference between the two is whether you square the weights or take their absolute value. That one change has a consequence out of all proportion to its size.

Why Lasso zeroes and Ridge does not

Look at how each penalty behaves as a coefficient approaches zero. The derivative of w2 is 2w, which itself goes to zero — so the closer a Ridge coefficient gets to zero, the weaker the force pushing it further. It approaches zero and never arrives.

The derivative of |w| is a constant ±1, no matter how small w is. Lasso pushes with the same strength all the way down, so a coefficient whose contribution to the fit is worth less than that constant push gets driven to exactly zero and stays there.

That is why Lasso performs feature selection and Ridge does not. Watch the coefficient bars above: under Ridge they all shrink together and stay non-zero; under Lasso they vanish one by one.

Why a penalty helps at all

Plain least squares has one instruction: make the training errors as small as possible. With many features, or with features that are nearly duplicates of each other, it will do exactly that by assigning some enormous coefficients that happen to cancel each other out — +4,300 on one column, −4,290 on a near-copy.

Those coefficients fit the training data beautifully and mean nothing. Move one input slightly and the prediction swings wildly, because two huge numbers are no longer cancelling.

Regularisation adds a second instruction: and keep the coefficients small. The model now minimises

error on the training data  +  λ × (size of the coefficients)

λ (called alpha in scikit-learn) sets the exchange rate between the two. At λ = 0 you are back to ordinary least squares. As λ grows, the model becomes progressively less willing to buy a small error reduction with a large coefficient, and at very large λ every coefficient is pushed to nearly zero and the model predicts the mean.

Deliberately accepting some bias to remove a lot of variance is the whole trade, and on data with many correlated features it is nearly always worth making.

The one difference that changes everything

The two methods differ only in how "size of the coefficients" is measured.

  • Ridge (L2) penalises the sum of squared coefficients.
  • Lasso (L1) penalises the sum of absolute coefficients.

That small change produces a large behavioural difference. Squaring means the penalty on a coefficient of 0.1 is 0.01 — almost nothing — so ridge has very little incentive to push a small coefficient all the way to zero. It shrinks everything smoothly and keeps every feature.

With absolute values, the penalty on 0.1 is 0.1, and the pressure to remove it entirely is exactly as strong as the pressure on any other unit of coefficient. So lasso pushes weak coefficients to exactly zero, and the features attached to them drop out of the model completely.

That makes lasso a feature selector as well as a regulariser. Fit it with a moderate alpha on 200 columns and you may be left with 30 non-zero coefficients and a model you can actually read.

 Ridge (L2)Lasso (L1)
PenaltySum of squaresSum of absolute values
CoefficientsShrunk, never exactly zeroSome become exactly zero
Feature selectionNoYes
Correlated featuresShares weight between themPicks one, drops the rest
SolutionClosed form existsIterative
Use whenAll features plausibly matterYou suspect many are useless

The correlated-features row is the one that decides most real cases. Ridge splits the weight across a group of correlated columns, which is stable and sensible. Lasso arbitrarily keeps one and zeroes the others, and which one it keeps can change with a small change in the data — unstable, though the predictions stay similar.

Elastic Net uses both penalties with a mixing ratio, and it is the standard answer when you want lasso's sparsity without its instability on correlated groups.

Two penalties, two very different results

Ridge shrinks every coefficient toward zero; lasso sends most of them exactly to zero. Watching both on the same correlated data shows why that difference matters.

example_01.pyscikit-learn
Output

Guided tour

  1. See the problem first. Set the Penalty Type to None (plain OLS) and set the Polynomial Degree slider to 12. The curve whips between the training points, train MSE is almost zero, and test MSE is enormous. That gap is overfitting, made visible.
  2. Apply a little Ridge. Switch Penalty Type to Ridge (L2) and set the Lambda slider to -3. The curve calms down immediately and test MSE drops, while train MSE rises slightly. That trade is the entire point of regularisation.
  3. Overdo it. Set the Lambda slider to its maximum, 2. The model is now so heavily penalised that it flattens toward a straight line and underfits — both errors climb together. Regularisation is a dial, not a switch.
  4. Watch Lasso delete. Switch Penalty Type to Lasso (L1) and set the Lambda slider to -1. Count the grey bars: coefficients have gone to exactly zero, and the non-zero count falls. Ridge at the same lambda leaves every bar standing.
  5. Starve it of data. Set the Training Points slider to 8 with degree 12 and no penalty. With fewer points than parameters the fit is wild. Add the penalty back and it becomes usable again — this is why regularisation matters most when data is scarce.
  6. Remove the noise. Set the Noise slider to 0. Now the high-degree fit is fine without any penalty, because there is no noise left to memorise. Overfitting is a response to noise, not to flexibility on its own.

Choosing between them

  • Ridge when you believe most features carry some signal, and especially when features are correlated. Ridge spreads weight across a correlated group rather than picking one arbitrarily.
  • Lasso when you suspect most features are irrelevant and you want the model to tell you which. The zeros are the output you care about, as much as the predictions are.
  • Elastic Net, which adds both penalties, when you want Lasso's selection but have correlated groups that Lasso would otherwise split arbitrarily.

In practice λ is not chosen by eye. It is selected by cross-validation: fit at many values, keep the one with the best held-out error.

Failure modes

  • Forgetting to scale first. The penalty acts on raw coefficient magnitudes, and a coefficient's size depends on its feature's units. A feature measured in metres gets a coefficient a thousand times larger than the same feature in millimetres, so it absorbs a thousand times more penalty. Standardise before regularising; this is not optional.
  • Penalising the intercept. The bias term should not be shrunk — doing so drags predictions toward zero rather than toward the mean. Every good implementation excludes it; if you write your own, remember to.
  • Reading Lasso's zeros as truth. With correlated features Lasso picks one from a group more or less arbitrarily, and a small change in the data can flip which one survives. Its selection is useful, not stable.
  • Tuning lambda on the test set. That makes the test score optimistic and no longer an estimate of anything. Use a validation split or cross-validation, and keep the test set for the end.

Where that leaves you

Both methods add a penalty on coefficient size to the loss, trading a little training accuracy for a lot of stability, with lambda controlling how hard that trade is pushed. Ridge squares the weights, so its pressure fades as a coefficient nears zero and everything merely shrinks; Lasso uses absolute values, so its pressure stays constant and weak coefficients are driven to exactly zero, which makes it a feature selector as well as a regulariser. Scale your features first, never penalise the intercept, and pick lambda by cross-validation rather than by eye.

Choosing alpha, and the scaling requirement

Alpha is not a setting you reason your way to — it is chosen by cross-validation, and scikit-learn provides estimators that do exactly that.

from sklearn.linear_model import RidgeCV, LassoCV
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import make_pipeline
import numpy as np

alphas = np.logspace(-3, 3, 25)      # 0.001 to 1000, log-spaced

ridge = make_pipeline(StandardScaler(), RidgeCV(alphas=alphas, cv=5))
lasso = make_pipeline(StandardScaler(), LassoCV(alphas=alphas, cv=5))

ridge.fit(X_train, y_train)
print(ridge[-1].alpha_)               # the value cross-validation chose

Search alpha on a logarithmic scale — the interesting range spans orders of magnitude, and a linear grid wastes almost all its points.

Scaling is mandatory. The penalty is applied to raw coefficient values, so a feature measured in thousands naturally has a tiny coefficient and is barely penalised, while a feature measured in units has a large one and is penalised heavily. Standardise first and the penalty falls evenly. Skipping this is the most common way to get nonsense out of a regularised model.

Note also that the intercept is not penalised, and should not be — shrinking it towards zero would mean insisting the target is centred at zero.

Reading the coefficient path

The most informative plot in this topic is the coefficient path: alpha on a log x-axis, coefficient values on the y-axis, one line per feature.

For ridge, the lines converge smoothly towards zero as alpha grows, but never touch it. For lasso, lines hit zero one after another and stay there — and the order in which they drop out is a ranking of how much each feature was contributing. The last few survivors are the features carrying the signal.

Two things to look for. A coefficient that changes sign as alpha varies is a warning about collinearity, not a finding. And a large gap between the alpha with the lowest cross-validation error and the "one standard error" alpha — the largest alpha within one standard error of the best — is a chance to buy a much simpler model for almost no accuracy. That heuristic is standard practice in statistics and underused elsewhere.

Questions people ask

Which should I use by default? Ridge, if you have no strong reason to prefer otherwise. It is stable, has a closed-form solution, and does not throw away information. Move to lasso when you specifically want a smaller feature set, and to elastic net when you want both.

Does regularisation work with logistic regression? Yes, identically, and scikit-learn applies L2 by default with C = 1/alpha. Everything here transfers.

Can lasso select more features than there are rows? No — lasso can select at most n features from a dataset with n rows. Elastic net does not have this limitation, which is another reason it is preferred in wide, short datasets such as genomics.

Should I trust lasso's feature selection? As a ranking, broadly. As a definitive statement about which features matter, no — refit with different random splits and see how stable the selected set is. Stability selection formalises this.

Is regularisation the same as feature selection? Lasso happens to do both. Ridge does not select anything; it keeps every feature and reduces their influence.

What if alpha comes out at essentially zero? Then your data does not need regularisation — plenty of rows, few features, low collinearity. That is a legitimate outcome and worth noticing.

Recap in one screen

  • Both methods add a penalty on coefficient size to the usual squared-error objective.
  • Ridge squares the coefficients: smooth shrinkage, nothing reaches zero, correlated features share the weight.
  • Lasso uses absolute values: weak coefficients hit exactly zero, so the model selects features.
  • Elastic net mixes the two and is the safe default on correlated, wide data.
  • Standardise your features, search alpha on a log scale by cross-validation, and read the coefficient path.

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 “Quick Context”?

  2. What does this module say about “One extra term”?

  3. What does this module say about “Why Lasso zeroes and Ridge does not”?

Cheat sheet

Ridge and Lasso Regression

Give a degree-12 polynomial eighteen noisy points and it will pass through nearly all of them. The fit looks superb on the data it has seen, and it is worthless: between the points the curve swings violently, because the only way to hit every point is to use enormous coefficients that cancel each other out.

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.