What is the difference between L1 and L2 regularization?
Last updated
L1 penalizes the sum of absolute weights and produces sparse models, setting some weights exactly to zero. L2 penalizes the sum of squared weights and shrinks all weights smoothly without zeroing them.
Penalty form:
L1 (Lasso):
λ Σ|wⱼ|L2 (Ridge / weight decay):
λ Σwⱼ²
Why L1 gives sparsity:
The gradient of the L1 penalty has constant magnitude λ, so it keeps pushing a weight even when the weight is small. It drives weights all the way to 0, and the solution uses soft-thresholding.
The L2 gradient is
2λw, which fades as w shrinks, so weights get small but rarely reach exactly zero.Geometrically, the L1 constraint region is a diamond with corners on the axes. Loss contours usually touch it first at a corner, where some coordinates are zero.
Correlated features:
L1 tends to pick one feature from a correlated group somewhat arbitrarily, which makes its selection unstable.
L2 spreads weight evenly across the group, which is more stable.
Optimization:
L2 is smooth and has a closed form for linear regression.
L1 is non-differentiable at 0, so it needs coordinate descent, proximal methods, or subgradients.
Priors: L1 corresponds to a Laplace prior and L2 to a Gaussian prior.
When to use each:
L1 when you expect few relevant features or want built-in feature selection.
L2 when many features each contribute a little, or when features are collinear.
Elastic Net when you want both.