Regularised Regression: Ridge, Lasso and Elastic Net Compared with Worked Examples

Compare ridge, lasso and Elastic Net through two small, fully worked coefficient examples. Learn what each penalty does, how to tune it safely and where common interpretations fail.

KnowledgeGate Team

Exam prep & CS education

Updated 23 Nov 20266 min read
Regularised Regression: Ridge, Lasso and Elastic Net Compared with Worked Examples

Ridge, lasso and Elastic Net are often reduced to “L2 shrinks, L1 selects”. That slogan does not show what happens to actual coefficients or why correlated predictors make the choice harder. For z=(3,0.8,-0.2), the three penalties produce different coefficient vectors; with perfectly correlated predictors, the penalty determines how weight is allocated. For a wider AI foundation before studying model penalties, review Approaches to AI: Four Approaches and a Worked Example.

Regularised regression changes the fitting objective

Ordinary least squares minimises residual error. With numerous, noisy or highly correlated predictors, however, similar training fits can come from coefficient vectors that change sharply with the sample. Regularisation adds a controlled cost for coefficient size. This usually adds bias in an attempt to reduce variance, but it does not guarantee better test performance.

The objective is:

J(beta) = (1/2)||y - X beta||_2^2 + lambda[alpha||beta||_1 + ((1-alpha)/2)||beta||_2^2].

Here X is the design matrix, y is the target, beta is the slope vector, lambda >= 0 is total penalty strength, and alpha in [0,1] is the L1 mixing fraction. Setting lambda=0 recovers the unregularised loss. A larger lambda makes coefficient size more expensive. Under this convention, alpha=0 is ridge, alpha=1 is lasso, and 0<alpha<1 is Elastic Net.

Split the data first. Fit centring and scaling only on each training fold, and usually leave the intercept unpenalised. Otherwise coefficient units are not comparable. For broader practical study routes, explore Skill Development Courses.

Ridge, lasso and Elastic Net impose different coefficient shapes

Method

Penalty in this convention

Exact zeros?

Correlated predictors

Best first hypothesis

Ridge

(lambda/2)||beta||_2^2

Normally no

Distributes weight

Many predictors carry small signals

Lasso

lambda||beta||_1

Possible

May choose unstably among near-duplicates

A sparse model is plausible

Elastic Net

lambda[alpha||beta||_1 + ((1-alpha)/2)||beta||_2^2]

Possible

Can retain a group

Sparsity and grouped retention both matter

The geometry explains the difference. An L2 constraint has a smooth circular or spherical boundary, so a loss contour usually touches it away from an axis. An L1 boundary has axis-aligned corners, making a zero coordinate more likely. Elastic Net combines the two and has rounded corners. These coefficient vectors, norms and correlated columns are linear-algebra objects. Linear Algebra for GATE CS builds the same foundations used throughout machine learning.

So ridge, lasso and Elastic Net are useful first hypotheses for distributed signal, sparsity and correlated sparse groups respectively. They are not universal winners. Validation must decide among them.

Worked coefficients: shrink the same three OLS scores

Assume centred, orthonormal predictors with X^T X=I. Centring handles the omitted intercept. Let z=X^T y=(3,0.8,-0.2), which is also the OLS coefficient vector here. Define soft-thresholding by S(z_j,c)=sign(z_j) max(|z_j|-c,0).

Set lambda=1 for every fit. Ridge gives beta_j=z_j/(1+lambda), so (3/2,0.8/2,-0.2/2)=(1.5,0.4,-0.1). Lasso gives beta_j=S(z_j,1), so (3-1,0,0)=(2,0,0).

For Elastic Net, set alpha=0.5. Then

beta_j=S(z_j,lambda alpha)/[1+lambda(1-alpha)].

The threshold is 0.5 and the denominator is 1.5. Therefore the result is (2.5/1.5,0.3/1.5,0/1.5)=(1.6667,0.2,0) after rounding.

Fit

Coefficients

Nonzero count

OLS

(3,0.8,-0.2)

3

Ridge

(1.5,0.4,-0.1)

3

Lasso

(2,0,0)

1

Elastic Net

(1.6667,0.2,0)

2

Ridge halves every coordinate. Lasso removes both magnitudes that do not clear 1. Elastic Net uses the lower threshold 0.5, followed by L2 shrinkage, so the middle feature survives at 0.2. These coefficients follow from the stated orthonormal assumptions; they are not empirical performance estimates from a fitted dataset.

Four aligned coefficient bar charts compare OLS, ridge, lasso, and Elastic Net estimates, including OLS coefficients 3, 0.8, and -0.2 and lasso sparsity.

Correlated predictors reveal the practical difference

Now take one response y=3, identical predictors x_1=x_2=1, no intercept, and s=beta_1+beta_2. The loss is (1/2)(3-s)^2. OLS has infinitely many exact fits on beta_1+beta_2=3, including (3,0), (1.5,1.5) and (0,3). Perfectly duplicate predictors collapse the fit to one identifiable sum and leave individual OLS allocations unidentified.

At lambda=1, ridge minimises (1/2)(3-s)^2+(1/2)(beta_1^2+beta_2^2). For fixed s, equal sharing minimises the L2 term. Substituting beta_1=beta_2=s/2 gives (1/2)(3-s)^2+s^2/4. The derivative is (s-3)+s/2, so s=2 and the unique solution is (1,1).

Lasso minimises (1/2)(3-s)^2+|beta_1|+|beta_2|. In the nonnegative quadrant, its derivative in s is (s-3)+1, giving s=2. Yet every (t,2-t) for 0<=t<=2 is optimal, including (2,0), (1,1) and (0,2).

With Elastic Net and alpha=0.5, minimise (1/2)(3-s)^2+0.5s+0.25(beta_1^2+beta_2^2). Equal sharing gives (1/2)(3-s)^2+0.5s+s^2/8. Its derivative is (s-3)+0.5+s/4=0, hence s=2 and (1,1). In this exact duplicate-feature example, the L2 component makes equal sharing unique. Pure lasso cannot identify one unique allocation.

A beta_1 versus beta_2 plane shows the duplicate-predictor example, with the OLS sum-3 line, penalized sum-2 line, ridge and Elastic Net at (1,1), and the lasso-optimal segment.

Choose lambda and alpha without leaking the test set

Use a reproducible workflow. Make the train/test split once. Inside five-fold cross-validation, fit the scaler on each fold's training portion. Evaluate ridge and lasso over lambda in {0.01,0.1,1,10}. Evaluate Elastic Net over the same values with alpha in {0.2,0.5,0.8}. Choose by mean validation error, refit the chosen pipeline on all training data, then evaluate the untouched test set once. Useful candidate values depend on the dataset; this four-value grid is only a starting point.

lambda controls total shrinkage, while alpha controls the L1 versus L2 mixture. Use the same folds and metric for every model. If prediction is the goal, validation error leads. If stable sparse interpretation also matters, inspect selection stability across folds instead of counting zeros from one fit.

Any method may win. A selected lambda near zero may mean shrinkage offers little benefit on that dataset, and cross-validation uncertainty may leave several settings practically tied.

Regularised regression traps and repairs

Wrong move

What goes wrong

Correct move

Penalise unscaled features

Coefficient units distort the penalty

Scale within the pipeline

Fit the scaler before cross-validation

Validation folds leak into preprocessing

Fit it inside each fold

Tune on the test set

The final estimate becomes optimistic

Touch the test set once

Penalise the intercept by default

Baseline level is shrunk without justification

Usually exclude the intercept

Treat one lasso selection as unique importance

Correlated alternatives may fit equally well

Check selection stability and correlations

Objective conventions differ by factors such as n, 1/2 and library parameter names. The same printed lambda can therefore produce different shrinkage across implementations. Ridge coefficients can be extremely small but are not normally exactly zero. A displayed 0.000 may only be rounded.

Regularisation controls coefficient complexity. It does not repair omitted-variable bias, leakage, bad labels or nonlinearity. A stable cross-validation score also cannot turn an associational coefficient into a causal effect.

How questions and interviews test ridge, lasso and Elastic Net

Common assessment patterns ask you to match L1 and L2 with coefficient behaviour, compute a soft-thresholded coordinate, reason about duplicated predictors, or identify leakage in a scaling and cross-validation workflow.

Use three retrieval checks:

  1. With z_j=0.8 and lasso lambda=1, S(0.8,1)=0.

  2. With the same z_j, Elastic Net at lambda=1, alpha=0.5 gives (0.8-0.5)/1.5=0.2.

  3. In the pure-lasso duplicate-feature example, (2,0) and (0,2) have the same objective. Selecting one does not prove it is uniquely relevant.

For adjacent practice when moving from linear models towards neural-network questions, work through Artificial Neural Networks MCQs: 10 Solved Questions with Explanations. That practice set shifts from coefficient derivations to neural-network concepts.

Regularised regression short version and the next step

Scale inside the validation pipeline. Use ridge for distributed shrinkage, lasso when sparsity is plausible, and Elastic Net when correlated groups and sparsity both matter. Tune instead of guessing, then audit coefficient stability and held-out error. Under the orthonormal assumptions above, the nonzero counts are 3,1,2 for ridge, lasso and Elastic Net, while the duplicate-feature example exposed lasso's non-unique split.

Readers who want to place regularised regression inside broader AI and machine-learning study can explore Artificial Intelligence (AI). The essential habit remains simple: choose the penalty from evidence, not from a slogan.