Why L1 Regularization Zeros Out Coefficients but L2 Does Not
Machine LearningModel answerLast updated
Compare Lasso and Ridge penalties on a 500-feature regression model, explaining mathematically and geometrically why one produces sparse weights and the other does not.
Predicting Sensor Failures with 500 Candidate Features
Your team is building a linear regression model to forecast equipment degradation scores from 500 candidate sensor readings. You have three modeling options on the table: plain Ordinary Least Squares (OLS), Lasso (L1 penalty), and Ridge (L2 penalty).
A colleague claims that both penalties simply "shrink the weights" and that the choice between them is mostly cosmetic. Your job is to explain why that characterization is wrong, and what actually differs between the two approaches.
Address the following in your answer:
-
Write out the two penalized objective functions and identify what is structurally different about the penalty terms:
- Lasso:
RSS(w) + λ · ||w||₁ - Ridge:
RSS(w) + λ · ||w||₂²
- Lasso:
-
Explain why Lasso can set individual coefficients to exactly zero, while Ridge only shrinks them toward zero without ever reaching it. Ground your explanation in the behavior of the gradient (or subgradient) of each penalty at
w_j = 0, and in the geometry of the corresponding constraint regions. -
Discuss what sparsity does and does not give you when the 500 input features are noisy sensor readings that may be correlated with each other.
Assumptions and setup
wis the vector of model coefficients.λ > 0is the regularization strength.- You may use the orthonormal design case (
XᵀX = I) to derive closed-form solutions for each penalty, then generalize your reasoning to the full case. - Treat the constraint-region form (budget
t, where||w||₁ ≤ tor||w||₂² ≤ t) as equivalent to the penalized form via Lagrangian duality.
Hints
Hint 1
Start with the orthonormal design simplification (XᵀX = I). Under this assumption the objective separates into one independent scalar problem per coefficient. Solve each one — you will get Ridge's closed form first, then Lasso's soft-thresholding operator. The two solutions look very different, and that difference is the core of your answer.
Hint 2
For the general case, ask what happens at the point w_j = 0. The L2 penalty's derivative there is 2λ·0 = 0, so it exerts no restoring force at the origin. The L1 penalty is not differentiable at 0 — its subgradient is the entire interval [−λ, λ]. Write the optimality condition for each and identify when zero is a valid solution.
Hint 3
For the geometric argument, think about what the constraint region looks like in two dimensions and then reason about what changes in 500 dimensions. The L1 ball has corners and flat faces aligned with the coordinate axes. Ask yourself: when an elliptical RSS contour expands outward from the OLS solution, where is it most likely to make first contact with each shape?
Model answer
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
A complete answer has three layers: the algebra (what the two optimality conditions look like), the geometry (why the shapes of the constraint regions matter), and the practical judgment (what sparsity actually buys you with correlated, noisy sensor data). Mentioning the geometry without deriving the subgradient argument — or vice versa — is only half an answer.
1. The two objectives
Both add a scalar penalty to the residual sum of squares (RSS):
- Lasso:
L(w) = ||y − Xw||² + λ Σ |w_j| - Ridge:
L(w) = ||y − Xw||² + λ Σ w_j²
Ridge is strictly convex and differentiable everywhere, so it has a unique closed-form solution: ŵ = (XᵀX + λI)⁻¹Xᵀy. Adding λI to the Gram matrix also fixes the invertibility problem when the number of features (500) exceeds the number of observations. In SVD terms, writing X = UDVᵀ, Ridge shrinks the OLS estimate along each principal direction by the factor d_j² / (d_j² + λ) — directions with low variance are shrunk the most.
Lasso is convex but not differentiable at any point where some w_j = 0. It has no closed-form solution in general and is typically solved by coordinate descent or LARS.
2. Why L1 produces exact zeros: the algebra
Orthonormal case. When XᵀX = I, the objective separates into one independent scalar problem per coordinate. Let z_j = xⱼᵀy (the OLS estimate for feature j).
-
Ridge per coordinate: minimize
w_j² − 2z_j w_j + λw_j². Setting the derivative to zero givesŵ_j = z_j / (1 + λ). The estimate is zero only whenz_j = 0exactly — a measure-zero event with real data. -
Lasso per coordinate: minimize
w_j² − 2z_j w_j + λ|w_j|. Solving this (accounting for the kink at 0) gives the soft-thresholding operator:ŵ_j = sign(z_j) · max(|z_j| − λ/2, 0)Any feature whose raw signal satisfies
|z_j| ≤ λ/2is set to exactly zero. Features that survive are shifted toward zero by a constantλ/2.
General case: the subgradient argument. Let g_j = −2xⱼᵀ(y − Xw) be the gradient of the loss with respect to w_j.
-
For the L2 penalty, the derivative at
w_j = 0is2λ · 0 = 0. The optimality condition isg_j + 2λw_j = 0, which at zero requiresg_j = 0— again, generically false. Because the L2 penalty gradient is proportional tow_j, it exerts vanishingly little restoring force whenw_jis small. Any nonzerog_jwill push the coefficient off zero. -
For the L1 penalty,
|w_j|has no derivative at 0. Its subgradient there is the interval[−λ, λ]. The optimality condition0 ∈ g_j + λ · ∂|w_j|is satisfied atw_j = 0whenever|g_j| ≤ λ. In plain terms: a feature stays exactly at zero unless its correlation with the current residual exceeds a fixed threshold λ. The penalty exerts a constant pull of size λ regardless of how smallw_jis — that constant pull is what makes zero a stable solution.
3. Why L1 produces exact zeros: the geometry
Frame each problem as: minimize RSS subject to a budget constraint on the weights.
-
L1 ball (diamond / cross-polytope): In two dimensions it is a rotated square with corners on the axes. In 500 dimensions, the boundary is dominated by corners, edges, and low-dimensional faces aligned with coordinate subspaces — the vast majority of the "surface area" lives near sparse points.
-
L2 ball (sphere): Perfectly smooth with no preferred directions.
The OLS solution sits outside the constraint region. As you shrink the budget t, the elliptical RSS contours expand inward from ŵ_OLS until they first touch the boundary of the constraint region. With the L1 ball, that first contact is overwhelmingly likely to occur at a vertex or sparse face, where many coordinates are zero. With the L2 ball, the tangency point is wherever the ellipse's normal aligns with the radius, which generically has all coordinates nonzero.
Bayesian framing (useful to mention). Lasso is the MAP estimate under a Laplace prior on weights, which has a sharp spike at zero. Ridge is the MAP estimate under a Gaussian prior, which is flat at zero. The prior geometry mirrors the constraint-region geometry.
4. Practical implications for 500 sensor features
Where Lasso helps
- Performs built-in feature selection. With enough data you might get a model using 20–30 sensors that an engineer can actually interpret and maintain.
- Reduces the number of signals that need to be monitored, calibrated, and validated in production.
Where Lasso causes problems with correlated features
- When several sensors track the same underlying physical quantity, Lasso tends to pick one arbitrarily and zero out the rest. Which one survives can flip entirely across different data samples — selection is unstable.
- Surviving coefficients are biased toward zero by
λ/2. If you need unbiased estimates for the selected features, refit OLS on just the selected set (the "relaxed Lasso"). - With
p > n, Lasso can select at mostnfeatures, which may be a hard constraint.
Where Ridge is the better predictor
- If the true signal is spread across many sensors (dense, weak effects), Ridge will typically outperform Lasso on out-of-sample prediction because it retains all features with proportional shrinkage.
- Ridge is always well-conditioned and gives a unique, stable solution even under heavy multicollinearity.
Elastic Net as a middle ground
Adding both penalties — λ[α||w||₁ + (1−α)||w||²] — gives a strictly convex objective that still produces exact zeros (unlike Ridge alone) but also groups correlated features together rather than picking one arbitrarily (unlike Lasso alone). In a sensor setting with correlated readings, Elastic Net is usually the most defensible default.
Common mistakes to avoid
- Saying "L2 makes weights zero and L1 makes them small." It is exactly the reverse.
- Claiming Ridge does feature selection because some coefficients become "very close to zero." Close is not zero, and the distinction matters operationally.
- Drawing the diamond-versus-circle picture without explaining why corners get hit — the subgradient interval at zero is the real answer.
- Forgetting to standardize features before fitting either model. The penalties are not scale-invariant, so a sensor measured in millivolts would be penalized differently from one measured in volts. Standardize inputs; do not penalize the intercept.
- Treating Lasso's selected features as the "true" causal drivers. Selection reflects correlation with the outcome in your training sample, not physical causation.
- Tuning
λwith standard k-fold cross-validation when the data has a time component (sensor readings over time). Use walk-forward validation with a gap between the training window and the validation window to avoid data leakage.