L1 vs L2 Regularization: Sparsity, Shrinkage, and When to Use Each
How L1 (lasso) and L2 (ridge) penalties differ, why L1 produces sparse models and L2 shrinks weights smoothly, and how to choose between them.
Regularization is the standard cure for overfitting: add a penalty to the loss function so the model pays a price for large weights, trading a little bias for a big cut in variance — the bias–variance tradeoff put to work. The two classic penalties look almost identical on paper and behave very differently in practice.
The two penalties
- L1 (lasso) adds λ × Σ|w| — the sum of the *absolute values* of the weights.
- L2 (ridge) adds λ × Σw² — the sum of the *squares* of the weights.
- In both, λ controls the strength: λ = 0 is no regularization; a huge λ crushes the model toward zero (and into underfitting).
Why L1 zeroes weights and L2 doesn't
The intuition lives in the gradients. The L2 penalty's pull on a weight is proportional to the weight itself — as a weight approaches zero, the pull fades, so weights glide *toward* zero but settle just short of it. The L1 penalty's pull is constant regardless of size — a weight whose usefulness can't outbid that fixed force gets pushed all the way to exactly zero and stays there. Geometrically: the L1 constraint region is a diamond whose sharp corners sit on the axes (where coordinates are zero), and optimal solutions tend to land on those corners; the L2 region is a circle with no corners to catch on.
Choosing between them
| Situation | Better fit | Why |
|---|---|---|
| Many features, few truly relevant | L1 | Zeros out the noise features — a sparse, interpretable model |
| Most features carry some signal | L2 | Keeps everyone with shrunk weights instead of amputating |
| Correlated features | L2 (or elastic net) | L1 arbitrarily picks one of a correlated group and drops the rest; L2 shares weight across them |
| You need built-in feature selection | L1 | The zeroed weights are the deselected features |
| Deep learning weight decay | L2 | The standard choice, usually alongside dropout |
Elastic net: don't choose
Elastic net combines both penalties with a mixing ratio, giving L1's sparsity with L2's stability on correlated features. When in doubt — especially with wide, correlated data — it's a strong default, with λ tuned by cross-validation.
In neural networks the same L2 idea appears as weight decay, typically combined with dropout — see regularization: L1, L2 and dropout and bias, variance and regularization for the drill-down. Always tune λ with cross-validation rather than guessing. **AI Learning** covers regularization across its cheat sheets and ~10,000 practice questions — a favourite interview topic — fully offline, no account.
Free · Works offline · Regularization is a top interview topic — practice it until it's easy.
Sources
- Tibshirani (1996) — 'Regression Shrinkage and Selection via the Lasso', JRSS B
- Hastie, Tibshirani & Friedman — The Elements of Statistical Learning, ch. 3
- Zou & Hastie (2005) — 'Regularization and variable selection via the elastic net', JRSS B
Frequently asked questions
More in Learn AI & Data Science
- Coding Interview Strategy: A Framework for Solving Problems Under PressureKnowing algorithms isn't enough — you have to deploy them under pressure. Here's a repeatable framework for solving unseen problems and a study plan that builds durable skill.
- P vs NP and Complexity Classes: What 'Hard' Really Means (Intuition, No Proofs)Why do some problems have fast algorithms and others resist every attempt? P vs NP is the deepest open question in computer science — here's the intuition, minus the proofs.
- Union-Find (Disjoint Set Union): Near-Constant Connectivity with Two OptimizationsUnion-Find answers 'are these two things connected?' and 'connect them' in almost O(1) — thanks to two beautifully simple optimizations that make the trees nearly flat.