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

L2 Regularization

L2 regularization (Ridge) penalizes the sum of squared coefficients — it shrinks every coefficient smoothly toward zero, without typically reaching it exactly, making it the standard choice when you want a simpler model without discarding any features entirely.

Formula

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

Worked Example — Shrinkage in a Simplified Case

For a single feature \(x=2\), target \(y=10\) (so the unregularized OLS coefficient is \(w_{\text{OLS}}=xy/x^2=5\)), the Ridge-regularized solution for this simplified case works out to:

\[ w_{\text{Ridge}} = \frac{xy}{x^2+\lambda} \]
λCalculationw
0 (unregularized)\(20/4\)5.0
4\(20/(4+4)\)2.5 (shrunk by half)
16\(20/(4+16)\)1.0 (shrunk further, still nonzero)

Notice the coefficient keeps shrinking smoothly as \(\lambda\) grows, but — unlike L1 — never actually reaches exactly zero, no matter how large \(\lambda\) gets.

from sklearn.linear_model import Ridge
import numpy as np

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

for alpha in [0, 1, 10, 100]:
    model = Ridge(alpha=alpha).fit(X, y)
    print(f"alpha={alpha}: max|coef|={np.max(np.abs(model.coef_)):.3f}, "
          f"min|coef|={np.min(np.abs(model.coef_)):.3f}")
# All coefficients shrink together as alpha grows -- none typically hit exactly zero

L1 vs L2 — When to Choose Which

L1 (Lasso)L2 (Ridge)
Produces sparse (zero) coefficients?YesNo — shrinks smoothly, rarely exactly zero
Handles correlated features well?Arbitrarily picks one, can be unstableShares weight smoothly across correlated features — more stable
Best forSuspecting many irrelevant features existBelieving most/all features contribute somewhat
Interpretability benefitAutomatic feature selection built inNone directly — all features remain, just shrunk

Why L2 Handles Multicollinearity Better Than L1

When two features are highly correlated, L2's squared penalty distributes the coefficient "weight" between them roughly evenly, since splitting a large coefficient into two smaller ones reduces the sum-of-squares penalty. L1's absolute-value penalty has no such incentive to split — it's often just as happy to put all the weight on one of the correlated features and zero out the other, which can be an arbitrary, unstable choice.

Practical Use Cases

  • The standard default regularization choice for linear/logistic regression when feature selection specifically isn't the goal
  • Situations with meaningfully correlated features, where L2's smoother, more stable shrinkage is preferable to L1's sometimes-arbitrary sparsity

Common Mistakes

  • Choosing L2 when the actual goal is feature selection — L2 alone won't zero out any coefficients, however small \(\lambda\) makes them.
  • Not tuning \(\lambda\)/alpha via cross-validation, same caveat as any regularization technique.

Interview Relevance

Q: "Why might Ridge be preferable to Lasso when features are highly correlated?" Ridge's squared penalty distributes weight relatively evenly across correlated features rather than arbitrarily favoring one and zeroing the rest — producing a more stable model whose coefficients don't swing unpredictably with small changes in the training data.

Practice Question

Using the shrinkage formula, compute \(w_{\text{Ridge}}\) for \(x=3\), \(y=12\), \(\lambda=9\).

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

L2 Regularization – FAQs

Quick answers about learning L2 Regularization in Machine Learning.

This free note from CodingNow 2.0 explains L2 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 L2 Regularization, is 100% free with no signup required.
With focused practice, most students grasp L2 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