Visual walkthrough — Elastic Net regularization
We only need one idea from calculus and one picture-fact about slopes. We introduce each exactly when it is needed — nowhere earlier.
Step 1 — What are we even minimizing? Draw the bowl.
WHAT. We want the weight (the number multiplying feature ) that makes our cost as small as possible. "Cost" here means: how badly we predict, plus a penalty for using large weights.
Forget all the other weights for a moment — pretend they are frozen. Then the cost depends on one number , so we can plot it as a curve: the horizontal axis is , the vertical axis is the cost .
WHY. A function of one variable is just a curve, and "minimize" means "find the lowest point of the curve." That is something we can literally look at. This trick — freeze everything, wiggle one thing — is the heart of Coordinate Descent.
PICTURE. The plain prediction-error part alone is a smooth bowl (a parabola). Its lowest point sits at some value we'll call (defined properly in Step 3). The red dot marks that bottom.
Step 2 — The slope trick: how do you find the bottom?
WHAT. At the very bottom of a smooth curve, the curve is momentarily flat — its slope is zero. Rolling a marble: it stops where the ground is level.
WHY THIS TOOL (the slope / derivative). We want a lowest point. The one property that singles out a lowest point of a smooth bowl is "flat here." The derivative is exactly the machine that reports the slope at each spot. That is why we reach for it and not, say, plugging in random numbers: it turns "find the minimum" into "solve slope ", an equation.
PICTURE. Three tangent lines: on the left the slope points down (negative), on the right it points up (positive), and at the bottom it is perfectly horizontal. Where the slope crosses from negative to positive is the minimum.
Step 3 — Define : where the smooth part alone wants to sit.
WHAT. Take only the prediction-error bowl (ignore penalties for a second) and find its bottom. Standardize features so (every feature has the same "size"). Then the bowl's slope is
Read the symbols where they sit:
- = how strongly feature still correlates with the leftover . Big → this feature explains a lot that no one else does.
- = the slope. Set it to zero → . So without any penalty, the answer is just .
WHY. is the single number that summarizes "what the data wants." Everything the penalties do from now on is push this number around. Naming it lets us watch that push.
PICTURE. The unpenalized bowl with its bottom pinned at (red). This is our starting point before penalties bite.
Step 4 — Add the L2 penalty: the bowl gets narrower (a squeeze).
WHAT. Add . This is another parabola centered at . Adding two parabolas gives a parabola — but a steeper, narrower one whose bottom is pulled toward .
Setting the combined smooth slope to zero: Term by term:
- = slope of the data bowl (Step 3).
- = extra slope contributed by L2; it grows the farther you are from , always pulling you back toward .
- Result: divide by — a proportional shrink. Nothing gets killed, everything just gets smaller.
WHY. L2 is smooth (a plain ), so it keeps the slope trick working. It is Ridge Regression's entire contribution: gentle, democratic shrinkage — never zero.
PICTURE. The original bowl (thin black) and the L2-tightened bowl (red), with the minimum sliding from toward but not reaching it.
Step 5 — The L1 penalty has a KINK, so the slope trick breaks. Introduce the subgradient.
WHAT. Now add . The absolute value is a V shape with a sharp corner at . On the right it has slope ; on the left slope ; at it has no single slope — the corner is ambiguous.
WHY A NEW TOOL (the subgradient). Our slope trick needs a slope, but the V has no slope at its tip. The subgradient repairs this: at the corner, instead of one slope we allow the whole range of slopes that could "lean against" the V there. Formally: A minimum now means: zero is one of the allowed slopes ( total subgradient), not that the slope equals zero.
PICTURE. The V of with the fan of dashed slope-lines pivoting through the corner — every line from slope to is "valid" there. That fan is exactly the set .
Step 6 — Solve with the corner: the soft-threshold appears.
WHAT. Write "zero is an allowed total slope" for the full cost:
Now split into cases by the sign of — we must cover all of them:
- Case : here , so , giving . Valid only if the numerator , i.e. .
- Case : here , giving . Valid only if .
- Case (the dead zone): the corner allows any slope in . Zero is a minimum precisely when contains , i.e. when . The feature is killed.
Stitching the three cases together gives one clean formula — the soft-thresholding operator :
Read it:
- = "slide toward by ; if you reach , stop there." That is the L1 shrink-and-kill (numerator).
- = the L2 proportional squeeze (denominator).
WHY. The three sign cases are forced by the V; folding them into is just bookkeeping. See Soft-Thresholding Operator for this shape in isolation.
PICTURE. The graph of versus : a flat dead band of width around the origin (output stays ), then two straight lines of slope shifted inward. The red segment is the dead zone.
Step 7 — Degenerate & limiting cases (so nothing surprises you).
WHAT / WHY / PICTURE for the corners of the knob-space:
- (pure Ridge Regression): threshold , so . Update . Dead band has zero width → nothing is ever killed. The graph is a single tilted line through the origin.
- (pure Lasso Regression): denominator . Update . Pure shift, no squeeze; the dead band is at its widest for given .
- (no penalty): both effects vanish, — back to plain least squares (Step 3).
- exactly (the boundary): numerator is exactly , so — the feature sits right on the edge of the dead zone and gets dropped.
PICTURE. Three soft-threshold curves overlaid: Ridge (, no flat part), Elastic Net (, medium flat + shallow slope), Lasso (, wide flat + slope 1). The Elastic-Net middle curve is red.
The one-picture summary
One figure, the whole journey: start at (what data wants) → L1 slides it inward by and may drop it into the dead zone → L2 divides what survives by . Two shrinks, one kill, in order.
Recall Feynman: tell the whole walkthrough in plain words
Freeze every weight but one, so the cost becomes a single curve you can eyeball (Step 1). A curve's lowest point is where it's flat, and the slope is the tool that finds flat (Step 2). Strip away the penalties and the data alone wants the weight to equal , its correlation with the leftover (Step 3). Add the smooth L2 penalty — a second parabola centered at zero — and the whole bowl narrows, pulling the answer toward zero by division but never quite reaching it (Step 4). Add the L1 penalty and you hit a snag: it's a V with a sharp corner, and a corner has no single slope, so we upgrade to the subgradient, the fan of allowed slopes at the tip (Step 5). Ask "is zero an allowed slope?" case by case on the sign of the weight, and out pops soft-thresholding: slide inward by , and if it lands in the dead band it snaps to zero (Step 6). Finally sanity-check the knobs — gives Ridge with no dead band, gives Lasso with no squeeze, gives raw least squares (Step 7). Numerator kills, denominator squeezes.
Connections
- Elastic Net regularization — the parent this walkthrough derives.
- Soft-Thresholding Operator — the that Step 6 produces.
- Coordinate Descent — the "freeze all but one" strategy of Step 1.
- Ridge Regression — the limit (Step 7).
- Lasso Regression — the limit (Step 7).
- Feature Correlation & Multicollinearity — why the L2 squeeze matters.
- Bias-Variance Tradeoff — both shrinks trade variance for bias.
- Cross-Validation — how get chosen in practice.
Reveal-practice: