Part IV — Supervised Learning: Regression & Linear Classifiers · Chapter 5

ElasticNet regularization

Hook

Lasso picks one of two nearly-identical predictors and throws the other away — but which one it keeps can be an arbitrary accident of the data, not a meaningful choice. What if you wanted the sparsity of lasso, but without that fragile all-or-nothing bet on correlated features?

Intuition

Same two correlated predictors as the last two chapters, same total penalty strength — only the mix between L1 and L2 changes. At α=1 it's pure lasso: w2w_2 sits at exact zero. Slide α down and watch w2w_2 revive, smoothly, long before you reach pure ridge.

Formalize

ElasticNet's penalty is a weighted blend of both:

MSE(w)+λ[αjwj  +  1α2jwj2]\text{MSE}(\mathbf{w}) + \lambda\left[\alpha \sum_j |w_j| \;+\; \frac{1-\alpha}{2}\sum_j w_j^2\right]
  • λ\lambda — the total penalty strength, same role as in ridge and lasso.
  • α\alpha — the mixing parameter, between 0 and 1: α=1\alpha=1 is pure lasso, α=0\alpha=0 is pure ridge.
  • jwj\sum_j |w_j|, jwj2\sum_j w_j^2 — the L1 and L2 penalty terms from the last two chapters, now combined.
  1. Each coordinate update generalizes both single-penalty rules

    Coordinate descent still soft-thresholds by the L1 share of the penalty, then divides by the data term plus the L2 share — set α=1\alpha=1 and the L2 share vanishes, recovering lasso's update exactly; set α=0\alpha=0 and the soft-threshold cutoff vanishes, recovering a ridge-like update.

  2. The L2 term restores ridge's grouping effect

    Even a small amount of L2 (α\alpha just under 1) is enough to stop lasso's arbitrary all-or-nothing choice between correlated predictors — both stay in the model, sharing the fit instead of one evicting the other.

  3. The L1 term keeps sparsity available

    Push α\alpha close enough to 1 and the sparsity comes back — ElasticNet doesn't have to choose between "shrinks everything" and "zeros out weak predictors"; α\alpha dials between them.

Play

Drag α down from 1. Somewhere before you reach 0, w2w_2 stops being exactly zero and starts climbing — that's the L2 term's grouping effect outweighing L1's corner.

Worked example

Same toy setup as ridge and lasso: x1=[1,2,3,4]x_1=[1,2,3,4], x2=[1,2,3,4.5]x_2=[1,2,3,4.5], y=[2,4,7,8]y=[2,4,7,8], fixed λ=0.5\lambda=0.5.

  1. α = 1 (pure lasso): sparse, as expected

    Pure L1: the L1 share is Nλα=4(0.5)(1)=2N\lambda\alpha=4(0.5)(1)=2, the L2 share is 00. One coordinate-descent round from w1=w2=0w_1=w_2=0:

    1. ρ1=ix1i(yiw2x2i)=ix1iyi=63\rho_1 = \sum_i x_{1i}(y_i-w_2x_{2i}) = \sum_i x_{1i}y_i = 63 (since w2=0w_2=0), so soft-thresholding gives w1=(632)/302.033w_1 = (63-2)/30 \approx 2.033
    2. ρ2=ix2i(yiw1x1i)1.933\rho_2 = \sum_i x_{2i}(y_i-w_1x_{1i}) \approx 1.933 — inside the dead zone [2,2][-2,2], so w2=0w_2=0 exactly

    Plugging w2=0w_2=0 back into step 1 reproduces the same ρ1\rho_1, so this is already the fixed point — no further rounds change anything.

  2. α = 0.5: both predictors survive, close together

    Now the L1 share is 4(0.5)(0.5)=14(0.5)(0.5)=1 and the L2 share is also 4(0.5)(0.5)=14(0.5)(0.5)=1. One round from w1=w2=0w_1=w_2=0:

    1. ρ1=63\rho_1=63, so w1=(631)/(30+1)=62/31=2.000w_1=(63-1)/(30+1)=62/31=2.000
    2. ρ2=ix2i(yiw1x1i)=3\rho_2 = \sum_i x_{2i}(y_i-w_1x_{1i}) = 3, so w2=(31)/(34.25+1)0.057w_2=(3-1)/(34.25+1) \approx 0.057

    Unlike α=1\alpha=1, this isn't a fixed point yet — repeating the same two-line cycle (recompute ρ1\rho_1 with the latest w2w_2, then ρ2\rho_2 with the latest w1w_1) nudges both weights further each round, converging to w11.069w_1\approx1.069, w20.902w_2\approx0.902 — no longer sparse, but the two nearly-collinear predictors are now sharing credit almost the way ridge would.

  3. α = 0 (pure ridge): fully grouped

    Now the L1 share is 00 (no soft-thresholding) and the L2 share is 4(0.5)(1)=24(0.5)(1)=2. One round from w1=w2=0w_1=w_2=0:

    1. ρ1=63\rho_1=63, so w1=63/(30+2)=63/321.969w_1=63/(30+2)=63/32\approx1.969
    2. ρ2=ix2i(yiw1x1i)=4\rho_2 = \sum_i x_{2i}(y_i-w_1x_{1i}) = 4, so w2=4/(34.25+2)0.110w_2=4/(34.25+2)\approx0.110

    Repeating that same cycle converges to w11.028w_1\approx1.028, w20.941w_2\approx0.941 — about as close together as ridge alone would produce (a differently-scaled λ than the closed-form ridge chapter, since this loss averages the squared error instead of summing it — but the qualitative grouping behavior matches).

Checkpoint

Lower α from pure lasso until w₂ climbs back above 0.5 — enough L2 influence to bring the zeroed predictor back into the model.

Move the α slider to try it
Summary
MSE(w)+λ[αjwj+1α2jwj2]\text{MSE}(\mathbf{w}) + \lambda\left[\alpha \sum_j |w_j| + \frac{1-\alpha}{2}\sum_j w_j^2\right]

ElasticNet doesn't pick a side between L1 and L2 — it interpolates. Lasso's sparsity stays available near α=1\alpha=1, and even a little L2 mixed in restores ridge's grouping effect on correlated predictors, fixing the exact fragility that pure lasso has no way to fix on its own.