← chapter

Elastic net

Linear regression with both penalties at once: lasso's L1 for sparsity, ridge's L2 for stability.

One knob, the mix ratio, slides from ridge to lasso. The default when you have many correlated features and want sparsity without lasso picking favorites.

The problem: correlated features

s1 and s2 — cholesterol and LDL — correlate at 0.897. Lasso keeps one and drops the other at random; ridge keeps both but never zeros anything.

The objective

L(w,b)=12ni(y^iyi)2+λ[αw1+1α2w22]L(w, b) = \frac{1}{2n}\sum_{i}\left(\hat{y}_i - y_i\right)^2 + \lambda\left[\alpha\lVert w\rVert_1 + \frac{1-\alpha}{2}\lVert w\rVert_2^2\right]

The update: soft-threshold over a shrink factor

wjS ⁣(ρj,  λα)1+λ(1α),S(z,γ)=sign(z)max(zγ,0)w_j \leftarrow \frac{S\!\left(\rho_j,\; \lambda\alpha\right)}{1 + \lambda(1-\alpha)}, \qquad S(z, \gamma) = \operatorname{sign}(z)\,\max(|z| - \gamma, 0)

Watch it: sweep ridge → lasso

Fix λ=1\lambda = 1, sweep α\alpha from 0 (ridge) to 1 (lasso), refitting from scratch every frame. Watch the dense ridge fit go sparse.

Ten coefficients nonzero under ridge; seven under lasso. Watch s2 vanish at the far right — that's lasso breaking the correlated group.

The grouping effect

Same λ\lambda, three regimes. Ridge and elastic net keep the s1/s2 pair together; lasso pours everything into s1 (-4.84) and cuts s2 to zero.

Scratch vs. library

Coordinate descent vs. sklearn.linear_model.ElasticNet, same λ\lambda and α\alpha, held-out diabetes patients.

Test R² 0.3653 for both — coefficients agree to fourteen decimals. One convex minimizer, every correct solver finds it.

Takeaways