Why L1 and L2 are Commonly Used for Regularization
L1 and L2 norms dominate regularization because they strike the optimal balance between statistical usefulness, convexity, and computational tractability. Ideal sparsity (L0 pseudo-norm) requires NP-hard combinatorial subset search, whereas L1 is its tightest convex relaxation and yields sparse models efficiently, and squared L2 is strictly convex, smooth everywhere, and admits a closed-form linear solution in Ridge regression. Probabilistically, L1 and L2 correspond directly to Laplace and Gaussian MAP priors.
Study Card: Why L1 and L2 are Commonly Used for Regularization
Direct Answer
Among infinitely many vector penalties ( for , ), L1 and L2 dominate machine learning because they pair complementary regularization behavior with tractable convex optimization:
- Why not or ()? The pseudo-norm (, counting non-zeros; not a true norm because it violates homogeneity) directly enforces sparsity, but subset selection is combinatorial and NP-hard in general. Fractional penalties () are non-convex with many local optima.
- Why L1? The L1 norm () is the tightest convex relaxation of on unit-bounded weights, enabling fast global optimization (coordinate descent, proximal gradient) while still inducing exact zeros.
- Why L2? The squared L2 penalty () is smooth, differentiable everywhere, strictly convex, rotationally invariant, and—in Ridge linear regression—yields the closed-form solution that stabilizes ill-conditioned matrices.
- Why not ? While convex, penalizes only the largest-magnitude coordinate(s), offering weak control over remaining weights and tending to push weights toward uniform magnitudes rather than sparsity.
- Bayesian MAP equivalence: Adding L2 corresponds to a zero-mean Gaussian prior on weights, while adding L1 corresponds to a sharper Laplace prior.
Key Terms
- Convexity: Ensures local optima are global optima, enabling reliable convergence.
- Pseudo-Norm: Counts non-zero parameters; ideal theoretically for sparsity but NP-hard to optimize at scale.
- Norm: ; convex but non-smooth and penalizes only extreme coordinates.
- Ridge Closed-Form Solution: (for linear regression), always invertible whenever .
- MAP Estimation: Maximum A Posteriori inference unifying empirical loss minimization with prior weight regularization.
Example
Consider fitting a model with 1,000 candidate features where only 10 truly matter. Exhaustive search would require evaluating subsets—completely infeasible. L1 (Lasso) solves a single convex problem in milliseconds to select a sparse active subset, while L2 (Ridge) cleanly regularizes correlated features in closed form (for linear regression) or via smooth gradients (for neural networks). By contrast, primarily caps the largest single coefficient while leaving smaller noisy weights unpenalized.
Code Implementation
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import Lasso, Ridge
from scipy.optimize import minimize
# Generate zero-intercept data so fit_intercept=False compares L1, L2, and L-inf fairly
rng = np.random.default_rng(0)
X = rng.normal(size=(120, 2))
y = 3.0 * X[:, 0] + rng.normal(scale=0.5, size=120)
lasso = Lasso(alpha=0.1, fit_intercept=False).fit(X, y)
ridge = Ridge(alpha=10.0, fit_intercept=False).fit(X, y)
def l_infinity_loss(beta, X, y, alpha):
residuals = y - X @ beta
return 0.5 * np.sum(residuals ** 2) + alpha * np.max(np.abs(beta))
# Use derivative-free Powell method because max(|beta|) is non-smooth
res_inf = minimize(l_infinity_loss, np.zeros(2), args=(X, y, 10.0), method="Powell")
print("True weights: [3.0, 0.0]")
print("Lasso (L1) weights: ", np.round(lasso.coef_, 4))
print("Ridge (L2) weights: ", np.round(ridge.coef_, 4))
print("L-infinity weights: ", np.round(res_inf.x, 4))
# Visualize 2D geometry: L1 norm vs. Squared L2 (Ridge penalty) vs. L-inf norm
b1 = np.linspace(-2, 2, 80)
b2 = np.linspace(-2, 2, 80)
B1, B2 = np.meshgrid(b1, b2)
L1 = np.abs(B1) + np.abs(B2)
L2_sq = B1 ** 2 + B2 ** 2
L_inf = np.maximum(np.abs(B1), np.abs(B2))
fig = plt.figure(figsize=(15, 4.5))
for idx, (Z, title) in enumerate([
(L1, "L1 Norm (|b1| + |b2|)"),
(L2_sq, "Squared L2 Penalty (b1^2 + b2^2)"),
(L_inf, "L-infinity Norm (max(|b1|, |b2|))"),
], start=1):
ax = fig.add_subplot(1, 3, idx, projection="3d")
ax.plot_surface(B1, B2, Z, cmap="viridis", alpha=0.85)
ax.set_title(title)
ax.set_xlabel("b1")
ax.set_ylabel("b2")
plt.tight_layout()
plt.show()
Related Concepts
- Optimization Solvers: Smooth squared L2 supports standard gradient descent, L-BFGS, or closed-form linear solves; non-smooth L1 uses coordinate descent or proximal gradient methods.
- Elastic Net: Bridges L1 and L2 when correlated predictors cause pure L1 to be unstable.
- Matrix Conditioning: Adding to lifts eigenvalues from to , fixing multicollinearity.
- Regularization Path: Tracing coefficients across values shows how L1 enters features sequentially while L2 shrinks all weights continuously.