The idea
An unregularized linear model will use any freedom it has — including noise directions — to shave training error. Regularization charges the model rent for coefficient size, so it only keeps weights the data genuinely pay for:
Both shrink coefficients toward zero and trade a little bias for a lot of variance reduction. The interview-famous difference: lasso sets coefficients exactly to zero; ridge only shrinks them. Two ways to see why:
Geometry. Penalized form is equivalent to minimizing RSS subject to a budget (a diamond) or (a disk). The solution sits where the elliptical RSS contours first touch the budget region. A diamond has corners on the axes, and expanding ellipses tend to strike a corner first — and at a corner, some coordinates are exactly zero. A disk has no corners, so the tangent point is generically off-axis: every coefficient survives, smaller.
Subgradients. At , the L1 term contributes any value in to the gradient. If the data's pull on that coefficient satisfies , zero is optimal and stays optimal — zero is sticky. The ridge penalty's force is , which vanishes as : it can push a coefficient arbitrarily close to zero but never hold it there.
Bayesian reading: ridge is MAP estimation under a Gaussian prior on ; lasso under a Laplace prior, whose sharp peak at zero encodes the belief "most coefficients are exactly irrelevant."
Practicalities that interviewers probe: standardize features first (the penalty is unit-blind, coefficients are not), do not penalize the intercept, and remember lasso's instability on correlated features — it picks one member of a correlated group nearly arbitrarily, while ridge splits weight across the group. Elastic net, , buys sparsity and grouping together.
Worked example
With an orthonormal design () the solutions are closed-form, so you can watch both penalties act on the same OLS fit. Take and :
Ridge divides everything by :
Lasso soft-thresholds — move each coefficient toward zero, clip at zero:
| Coefficient | OLS | Ridge () | Lasso (shift by ) |
|---|---|---|---|
| 3.0 | 2.0 | 2.5 | |
| 0.4 | 0.27 | 0 | |
The story in one row: is weaker than the threshold , so lasso deletes it outright, while ridge keeps it at — nonzero forever, no matter how large grows, it only approaches zero asymptotically. Also note ridge punished the largest coefficient hardest in absolute terms (3.0 lost 1.0; 0.4 lost 0.13): proportional shrinkage, versus lasso's flat "everyone pays ".
Cheat sheet
Objectives: lasso ; ridge ; elastic net adds both.
Orthonormal closed forms: ridge ; lasso .
| Question | Ridge (L2) | Lasso (L1) |
|---|---|---|
| Exact zeros? | never | yes — feature selection built in |
| Correlated features | shares weight, stable | picks one arbitrarily, unstable |
| Closed form? | yes: | no — coordinate descent / LARS |
| Prior | Gaussian | Laplace |
| Use when | many small effects, correlated inputs | few strong effects, want sparsity |
- = bias , variance ; is OLS; kills all penalized coefficients.
- Standardize features first; never penalize the intercept.
- Ridge fixes the singular / near-singular problem: is always invertible — this is why ridge is the standard answer to multicollinearity.
- Choose by CV on a log grid; the one-standard-error rule trades a whisker of CV score for a sparser, more robust model.
- Elastic net default when or features come in correlated blocks.
- Gotcha: lasso can select at most features — relevant when .