🔥Limited Offer: Get 50% OFFon AI & Full Stack Courses🔥
Back to Machine Learning Notes
Topic #198

L1 Regularization

L1 regularization (Lasso) penalizes the sum of coefficients' absolute values — its distinctive, genuinely useful property is that it can shrink some coefficients to exactly zero, performing automatic feature selection as a side effect of training.

Formula

\[ J_{\text{Lasso}}(w) = J(w) + \lambda\sum_{i=1}^{n}|w_i| \]

Worked Example — Soft Thresholding

For a simplified single-coefficient case, L1-regularized regression has a closed-form "soft thresholding" solution:

\[ w_{\text{Lasso}} = \text{sign}(w_{\text{OLS}}) \times \max\bigl(|w_{\text{OLS}}| - \tfrac{\lambda}{2},\ 0\bigr) \]

Starting from an unregularized coefficient \(w_{\text{OLS}}=5\):

λCalculationResult
6\(\max(5 - 3, 0) = 2\)2 (shrunk, still nonzero)
12\(\max(5 - 6, 0) = \max(-1, 0)\)0 (exactly zero!)

Once the penalty (\(\lambda/2 = 6\)) exceeds the coefficient's original magnitude (5), the coefficient is pushed all the way to exactly zero — this "corner" behavior is unique to L1, and is exactly why it performs feature selection.

from sklearn.linear_model import Lasso
import numpy as np

X = np.random.rand(100, 20)   # 20 features, only 3 actually matter
y = 3*X[:,0] - 2*X[:,1] + 5*X[:,2] + np.random.normal(0, 0.1, 100)

model = Lasso(alpha=0.1)
model.fit(X, y)
print(np.round(model.coef_, 3))
print("Nonzero coefficients:", np.sum(model.coef_ != 0))   # often close to 3 -- Lasso found them

Why the Diamond Shape Matters — The Geometric Explanation

L1's constraint region (\(\sum|w_i| \leq t\)) is a diamond in coefficient space, with sharp corners aligned exactly on the axes. Because the cost function's optimum tends to land on a corner of this constraint region when the two shapes intersect, and corners correspond to some coefficients being exactly zero, L1 solutions naturally end up sparse. L2's circular constraint region has no corners, so its solutions shrink toward — but essentially never exactly reach — zero.

Practical Use Cases

  • High-dimensional data where you suspect only a subset of features are genuinely relevant
  • Wanting both a predictive model AND automatic feature selection from a single training run — see Embedded Methods

Common Mistakes

  • Using L1 when features are highly correlated with each other — Lasso tends to arbitrarily pick one from a correlated group and zero out the rest, which can be unstable (small data changes shift which one "wins").
  • Not scaling features first, same requirement as any regularization technique.

Interview Relevance

Q: "Why does L1 produce exactly-zero coefficients while L2 doesn't?" L1's penalty (sum of absolute values) creates a constraint region with sharp corners on the coordinate axes — the optimal solution often lands exactly on one of these corners, where some coefficients are precisely zero; L2's penalty (sum of squares) creates a smooth circular region with no corners, so its optimal solutions shrink toward zero without typically reaching it exactly.

Practice Question

Using the soft-thresholding formula, compute the Lasso-regularized coefficient for \(w_{\text{OLS}}=8\) with \(\lambda=10\).

Want to go beyond the notes?

Join CodingNow 2.0's Machine Learning course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

L1 Regularization – FAQs

Quick answers about learning L1 Regularization in Machine Learning.

This free note from CodingNow 2.0 explains L1 Regularization in Machine Learning — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Machine Learning topic on CodingNow 2.0, including L1 Regularization, is 100% free with no signup required.
With focused practice, most students grasp L1 Regularization in 1–3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) — expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now