Ridge, lasso and elastic net
Regularised regression: shrink weights to fight overfitting, and let lasso select features.
Animated explanation
The idea
When a model overfits, its weights are usually large: big positive and negative weights cancel each other to thread through noisy points. Regularisation adds a price for large weights to the loss, so the model only uses a big weight when the data truly demands it.
Ridge charges the sum of squared weights and shrinks all of them smoothly. Lasso charges the sum of absolute values, and its solutions put many weights at exactly zero, so it also selects features. Elastic net mixes the two.
One knob, , sets the price. is plain least squares; a huge squashes every weight to zero. Pick it by cross-validation.
Penalty = budget
Minimising loss + penalty is equivalent to minimising the loss subject to the penalty staying under some budget (Lagrange duality). Picture the loss as elliptical contours around the least-squares answer. The regularised answer is where the smallest contour touches the allowed region.
For ridge the region is a ball: the contact point can be anywhere on its smooth surface. For lasso it is a diamond (a cross-polytope in higher dimensions) whose corners sit on the axes. Contours hit corners far more often than flat faces, and at a corner some weights are exactly zero.
Practical rules
- Standardise features first. The penalty treats all weights alike, so a feature measured in grams would be punished more than the same feature in kilograms.
- Do not penalise the intercept. Centre or leave out of the penalty.
- Correlated features: lasso tends to keep one of a correlated group arbitrarily; ridge spreads weight across them; elastic net does a bit of both.
- Choose λ on a log grid ( to ) with cross-validation.
The Bayesian view
Ridge is maximum a posteriori estimation with a Gaussian prior ; lasso uses a Laplace prior, which has a sharp peak at zero. Regularisation is prior belief that most effects are small.
The algorithm, step by step
- 1Standardise each feature to mean 0 and variance 1; centre .
- 2Ridge: solve .
- 3Lasso: start at ; repeatedly, for each , compute and set .
- 4Repeat for a grid of λ values; keep the λ with the best cross-validated error.
The maths
Ridge
Lasso (scikit-learn's scaling)
Soft-thresholding
Elastic net
Strengths and weaknesses
Strengths
- Reduces variance and overfitting with one knob
- Ridge handles correlated features and
- Lasso gives sparse, interpretable models
- Cheap to fit along a whole λ path
Weaknesses
- Adds bias: weights are pulled towards zero
- Needs standardised features
- Lasso picks arbitrarily among correlated features
Reach for it when…
Check your understanding
Lambdian Lab
Ridge in closed form, lasso by coordinate descent
Implement both regularised regressions on standardised data with a centred target (so there is no intercept to worry about).
- ridge(X, y, lam): , solved with np.linalg.solve.
- soft_threshold(rho, lam).
- lasso(X, y, lam, iters): coordinate descent for scikit-learn's objective .