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)=2n1i∑(y^i−yi)2+λ[α∥w∥1+21−α∥w∥22]
- λ — penalty strength. α — the L1/L2 mix.
- α=1 → pure lasso. α=0 → pure ridge.
- Elastic net is the convex combination; those are its endpoints.
The update: soft-threshold over a shrink factor
wj←1+λ(1−α)S(ρj,λα),S(z,γ)=sign(z)max(∣z∣−γ,0)
- Numerator: the L1 part — soft-threshold, can snap a coefficient to exactly zero.
- Denominator: the L2 part — a constant shrink that ties correlated features together.
- Cycle it over the coefficients (coordinate descent) until they stop moving.
Watch it: sweep ridge → lasso
Fix λ=1, sweep α 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 λ, 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 λ and
α, held-out diabetes patients.
Test R² 0.3653 for both — coefficients agree to fourteen decimals. One convex
minimizer, every correct solver finds it.
Takeaways
- The default for many correlated features: sparsity without lasso's instability.
- The L2 part ties correlated coefficients together (the grouping effect); the L1 part still zeros the dead weight.
- On this data the penalty didn't buy accuracy (R² ≈ 0.37 either way) — it bought a stable, readable coefficient vector.
- It's not a third model. It's the dial between ridge and lasso; the right setting is a cross-validation search, not a guess.