2.2.13Linear & Logistic Regression

L1 (Lasso) regularization

1,930 words9 min readdifficulty · medium6 backlinks

WHY do we need it?


WHAT is Lasso?


HOW does L1 create exact zeros? (Derivation from scratch)

Consider a single weight ww with the simplest problem (orthonormal features so features decouple). The cost is: J(w)=12(wz)2+λwJ(w) = \frac{1}{2}(w - z)^2 + \lambda|w| where zz is the OLS solution for that feature (the value ww would take with no penalty).

Why this step? With orthonormal features each coordinate optimizes independently, so studying one ww tells us everything.

Now minimize. The absolute value is not differentiable at 00, so we handle two cases.

Case 1: w>0w > 0. Then w=w|w| = w, and dJdw=(wz)+λ=0    w=zλ\frac{dJ}{dw} = (w - z) + \lambda = 0 \;\Rightarrow\; w = z - \lambda Valid only if w>0w>0, i.e. z>λz > \lambda.

Case 2: w<0w < 0. Then w=w|w| = -w, and dJdw=(wz)λ=0    w=z+λ\frac{dJ}{dw} = (w - z) - \lambda = 0 \;\Rightarrow\; w = z + \lambda Valid only if z<λz < -\lambda.

Case 3: λzλ-\lambda \le z \le \lambda. Neither of the above is consistent, and the minimum sits at the kink w=0w = 0.

Compare Ridge in the same setting: minimizing 12(wz)2+λw2\frac12(w-z)^2 + \lambda w^2 gives w=z1+2λw = \frac{z}{1+2\lambda} — always shrunk, but never exactly zero unless z=0z=0.


Worked Examples


Common Mistakes (Steel-man + fix)


Active Recall

Recall What does the soft-threshold operator do?

Sλ(z)=sign(z)max(zλ,0)S_\lambda(z) = \operatorname{sign}(z)\max(|z|-\lambda,0): shrink toward zero by λ\lambda, and clamp to exactly 00 if the magnitude is below λ\lambda.

Recall Why does L1 give sparsity but L2 does not?

L1's constraint region (diamond) has corners on the axes; MSE contours touch it there, forcing coordinates to 00. L2's circle is smooth — no corners — so weights shrink but stay nonzero.

Recall Feynman: explain to a 12-year-old

Imagine you're packing for a trip but your bag has a strict weight limit, and each item you add costs the same "weight tax" no matter how small it is. Because the tax is fixed per item, you'd rather leave small useless items completely out than pay tax to carry a bit of them. Lasso does this with features: it drops the ones not worth their "tax," keeping only a few important ones. That's why the model ends up short and simple.


Flashcards

What penalty term does Lasso add to the loss?
λjwj=λw1\lambda\sum_j |w_j| = \lambda\|w\|_1 (the L1 norm of the weights).
Why does Lasso produce exactly-zero weights while Ridge does not?
L1's constraint region is a diamond with corners on the axes; the loss contours first touch at a corner (zero). Ridge's circle is smooth, so no exact zeros.
Write the soft-thresholding solution for orthonormal features.
w=sign(z)max(zλ,0)w^\star = \operatorname{sign}(z)\max(|z|-\lambda, 0), where zz is the OLS coefficient.
What happens as λ0\lambda \to 0 and as λ\lambda \to \infty in Lasso?
λ0\lambda\to 0: recovers OLS. λ\lambda\to\infty: all penalized weights become 0 (model predicts the mean).
Why must features be standardized before Lasso?
The L1 penalty treats all coefficients equally; different feature scales cause unequal effective penalization.
Why can't plain gradient descent be used cleanly for Lasso?
w|w| is non-differentiable at 0; use subgradients / coordinate descent / proximal (ISTA) methods instead.
What is Elastic Net and why use it?
A mix λ1w1+λ2w22\lambda_1\|w\|_1 + \lambda_2\|w\|_2^2; it keeps groups of correlated features together, fixing Lasso's instability.
For z=0.4z=0.4, λ=0.5\lambda=0.5, what is the Lasso weight?
00, since z<λ|z|<\lambda so it is clamped to zero.

Connections

  • Ridge (L2) Regularization — smooth shrinkage, no sparsity; complementary geometry.
  • Elastic Net — combines L1 and L2 to handle correlated features.
  • Bias-Variance Tradeoffλ\lambda tunes this directly.
  • Cross-Validation — how we actually pick λ\lambda.
  • Feature Selection — Lasso is an embedded selection method.
  • Coordinate Descent & Proximal Gradient (ISTA/FISTA) — optimizers that respect the L1 kink.
  • Logistic Regression — L1 penalty transfers directly to classification.

Concept Map

leads to

motivates

Lasso adds

controlled by

bigger lambda

produces

gives

derived via

clamps to zero when

contrast with

shrinks smoothly

OLS minimizes training error

Overfitting, big weights

Regularization

L1 penalty sum of abs weights

lambda strength

More shrinkage more zeros

Exact zeros, sparse model

Automatic feature selection

Soft-threshold operator

Non-differentiable kink at 0

Ridge L2 penalty

Never exactly zero

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, Lasso ka simple funda ye hai: normal regression sirf training error kam karta hai, isliye kabhi kabhi weights bahut bade ho jaate hain aur model noise ko bhi fit karne lagta hai — yani overfitting. Lasso ek extra "tax" lagata hai jo weights ki absolute values ka sum hai, λwj\lambda\sum|w_j|. Jitna bada λ\lambda, utna zyada weights zero ki taraf khinchte hain.

Sabse important baat: Lasso kuch weights ko bilkul zero bana deta hai. Iska matlab wo feature model se hat gaya — automatic feature selection. Ridge (L2) sirf chhota karta hai, zero nahi karta. Iska reason geometry hai: L1 ka region ek diamond hota hai jiske corners axes par hote hain, aur loss ke contours usi corner pe touch karte hain jaha coordinate zero hota hai. L2 ka circle smooth hota hai, isliye zero nahi milta.

Formula wise, ek simple case mein solution nikalta hai soft-thresholding: w=sign(z)max(zλ,0)w = \operatorname{sign}(z)\max(|z|-\lambda,0). Matlab har coefficient ko λ\lambda jitna zero ki taraf khisko, aur agar wo zero cross karne lage to seedha zero pe clamp kar do. Ye hi wo trick hai jo sparsity deti hai.

Do practical baatein yaad rakhna: (1) Lasso lagane se pehle features ko standardize karo warna scale ki wajah se penalty unfair ho jayegi. (2) λ\lambda ko cross-validation se choose karo — bahut bada λ\lambda underfit kar dega. Agar features correlated hain to Lasso thoda unstable hota hai, tab Elastic Net use karo. Bas itna samajh gaye to Lasso clear hai!

Go deeper — visual, from zero

Test yourself — Linear & Logistic Regression

Connections