Skip to content
elevatedevco
GuideLearn AI & Data Science

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

SituationBetter fitWhy
Many features, few truly relevantL1Zeros out the noise features — a sparse, interpretable model
Most features carry some signalL2Keeps everyone with shrunk weights instead of amputating
Correlated featuresL2 (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 selectionL1The zeroed weights are the deselected features
Deep learning weight decayL2The 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.

AI Learning: AI/ML/DS Q&A

Free · Works offline · Regularization is a top interview topic — practice it until it's easy.

Coming soon toGoogle Play

Sources

Frequently asked questions

elevatedevco builds private, offline Android apps — your data never leaves your phone. Read more articles.