ML Atlas

Regularization

Penalize complexity. Prevent memorization. Force generalization.

IntermediateSupervisedMath Heavy
28 min read
Linear regression and the OLS loss functionLogistic regression and cross-entropy lossGradient descent and parameter updatesBias-variance tradeoff concept
  • Ridge regression in housing price models to handle collinear neighborhood features
  • Lasso in genomics for gene selection — finding the sparse set of genes predictive of disease
  • L2 weight decay as the default regularizer in all major neural network frameworks (PyTorch, TensorFlow)
  • ElasticNet in sklearn pipelines for high-dimensional tabular data (Kaggle competitions)
  • Dropout (L0-like stochastic regularization) in every modern deep learning architecture from AlexNet onward
01

In Plain English

Regularization adds a penalty term to the training loss that discourages the model from learning complex, large-magnitude solutions. It forces the model to find simpler explanations of the data, which generalizes better to unseen examples.

Why It Exists

Without regularization, models minimize training loss by fitting every noise pattern in the training data. A model with more parameters than samples can achieve perfect training accuracy while being completely useless on new data. Regularization is the mathematical mechanism for encoding the principle of parsimony (Occam's razor) into optimization.

Problem It Solves

Given that training data is finite and noisy, the model must be prevented from memorizing the noise. Regularization adds a complexity cost to the loss, steering the optimizer toward solutions that are both accurate on training data and simple enough to generalize.

Real-Life Analogy

"Imagine writing an exam answer. An unregularized student memorizes every specific detail from the textbook verbatim (overfitting). A regularized student is forced by word limits to write only the most essential, broadly applicable points (generalization). The word limit is the regularization — it penalizes complexity and forces conciseness."

When To Use

  • Number of features is large relative to number of samples (d/n > 0.1)
  • Features are correlated (multicollinearity makes unregularized coefficients unstable)
  • You suspect many features are irrelevant (use L1 for automatic selection)
  • Training accuracy is much higher than validation accuracy (overfitting detected)
  • Training neural networks (weight decay is almost universally applied)
  • When model coefficients have unexpectedly large magnitudes

When NOT To Use

  • You have extremely large n and simple models — regularization may induce unnecessary bias
  • All features are known to be relevant and informative (sparse L1 would wrongly zero them)
  • The model is already underfitting — regularization increases bias further
  • Features have been carefully selected through domain knowledge and sparsity is not expected
02

Without regularization, the optimizer minimizes the empirical risk (training loss). The empirical risk minimizer has no reason to prefer simple weights over complex ones — if memorizing noise reduces the loss by 0.001, it will. Regularization modifies the objective: instead of minimizing just L(w), we minimize L(w) + λ·R(w) where R(w) is the complexity penalty. Now the optimizer must balance fitting the data against keeping weights simple.

L2 regularization (Ridge) penalizes the sum of squared weights: R(w) = ||w||² = Σwⱼ². This encourages all weights to be small but none to be exactly zero. Geometrically, the feasible region for w is a sphere (ball) around the origin — the optimal w is the lowest-loss point that stays within this sphere. L1 regularization (Lasso) penalizes the sum of absolute weights: R(w) = ||w||₁ = Σ|wⱼ|. The feasible region is a diamond (L1 ball) with corners on the axes. The optimal w is often at a corner — where many coordinates are exactly zero. This is the geometric explanation for L1's automatic feature selection.

ElasticNet combines both: R(w) = α||w||₁ + (1−α)||w||². It inherits L1's feature selection (some weights go to exactly zero) and L2's grouping effect (correlated features get similar weights rather than L1's arbitrary selection of just one). In practice, ElasticNet often outperforms pure L1 or pure L2 on datasets with both redundant and correlated features.

The Metaphor

"Think of weights as rubber bands connecting the model's predictions to the data points, and regularization as a spring attached from each weight to zero. L2 regularization is like a smooth spring — it pulls all weights gently toward zero, with proportional force. L1 regularization is like a flat-bottom tray — small weights don't feel any restoring force, but large weights feel a constant pull. This is why L1 completely collapses small weights to zero (they lose the tug-of-war) while L2 only shrinks them."

Beginner Mental Model

Total loss = Fit loss + λ × Weight size penalty. Larger λ = heavier penalty on weight size = simpler model = more regularized. λ = 0 = no regularization = standard unregularized training. When training loss is minimized with this combined objective, the model can't make weights too large to fit training noise — the penalty cost outweighs the small training loss gain.

03

Regularization modifies the empirical risk minimization (ERM) objective to: argmin_w [ L(w; X, y) + λ·R(w) ], where L is the task loss (MSE, BCE, etc.), R(w) is the regularization term (a norm of w), and λ > 0 is the regularization strength. Ridge: R(w) = ||w||₂² = wᵀw. Lasso: R(w) = ||w||₁ = Σ|wⱼ|. ElasticNet: R(w) = α||w||₁ + (1−α)||w||₂².

Regularization Strength (λ)
Controls the trade-off between fitting data (minimize L) and keeping weights small (minimize R). Large λ: simpler model, higher bias. Small λ → 0: approaches unregularized model. Must be cross-validated.
L2 Norm (Ridge)
||w||₂ = √(Σwⱼ²). The L2 penalty is the squared L2 norm: ||w||₂² = Σwⱼ². Penalizes each weight proportionally to its magnitude. Adds λI to the Hessian, making the OLS solution always uniquely defined.
L1 Norm (Lasso)
||w||₁ = Σ|wⱼ|. Non-differentiable at wⱼ = 0. Leads to sparse solutions where many weights are exactly zero. Solved via coordinate descent or ADMM, not standard gradient descent.
Sparsity
A property of solutions where many parameters are exactly zero. L1 regularization promotes sparsity. Desirable when many features are irrelevant — zeros effectively remove those features from the model.
Soft Thresholding
The closed-form solution for L1 regularized linear regression for a single feature: w* = sign(ρ)·max(|ρ| − λ, 0). Values smaller than λ are mapped to exactly zero. This is the mechanism behind Lasso's sparsity.
Weight Decay
Neural network terminology for L2 regularization. The gradient update becomes: w ← w − α·(∇L + 2λw) = w(1−2αλ) − α·∇L. The factor (1−2αλ) < 1 decays the weight at each step, hence the name.
Regularization Path
The trajectory of optimal weights w*(λ) as λ varies from 0 to ∞. The Lasso regularization path is piecewise linear — as λ increases, weights hit zero one by one (entering/leaving the active set). LassoCV exploits this via warm-starting.
  1. 1. Choose regularization type: L2 (Ridge) for correlated features or when you want all features; L1 (Lasso) for feature selection; ElasticNet for both.
  2. 2. Add the penalty to the loss: L_total = L_task + λ·R(w).
  3. 3. Compute gradient of the total loss: ∂L_total/∂w = ∂L_task/∂w + λ·∂R/∂w.
  4. 4. For Ridge: ∂R/∂w = 2w. Update: w ← w − α·(∇L + 2λw).
  5. 5. For Lasso: ∂R/∂w = sign(w) (undefined at 0). Use coordinate descent with soft thresholding.
  6. 6. Cross-validate λ on a log-scale grid: [0.001, 0.01, 0.1, 1, 10, 100]. Pick λ minimizing validation loss.
  7. 7. Inspect the solution: Ridge coefficients are all nonzero (shrunken). Lasso coefficients are sparse (many exactly zero). ElasticNet: sparse with grouped correlated features.

Feature matrix X ∈ ℝⁿˣᵈ (scaled), target y, regularization type, and λ (tuned via CV).

Regularized weight vector w*(λ) ∈ ℝᵈ. For Lasso: sparse w* with many zeros. For Ridge: dense w* with all small values.

01The true underlying model is relatively simple (low-complexity), making regularization toward simplicity appropriate.
02λ is tuned via cross-validation, not set arbitrarily.
03Features are scaled (StandardScaler) before regularization — otherwise λ penalizes large-scale features more than small-scale ones.
04Bias term (intercept) is NOT regularized — only weights wⱼ. This is standard practice: the intercept absorbs the overall level of y.
05For Lasso: a unique solution exists if features are not perfectly correlated (the Lasso path is uniquely defined).
  • λ → 0: solution approaches OLS (no regularization). Valid but may overfit.
  • λ → ∞: all weights → 0 (for Ridge) or all weights = 0 (for Lasso). Model predicts the mean. Completely underfits.
  • Perfect multicollinearity with Lasso: Lasso arbitrarily selects one feature from a perfectly correlated group; Ridge spreads weight equally. ElasticNet is preferred for correlated features.
  • d >> n with Ridge: OLS is undefined, but Ridge uniquely solves: w* = (XᵀX + λI)⁻¹Xᵀy. The +λI term guarantees invertibility.
04

Regularization is applied during model training, modifying the loss function that gradient descent minimizes. For sklearn models: set the alpha (Ridge/Lasso) or C (LogisticRegression = 1/λ) parameter. For neural networks: set weight_decay in the optimizer. Feature scaling must happen before regularization.

  • 01.Scale all features with StandardScaler BEFORE applying regularization — λ penalizes features on their natural scale. Without scaling, features with large values get over-regularized.
  • 02.Do NOT regularize the intercept/bias — sklearn handles this correctly by default.
  • 03.Handle missing values before fitting — regularized models still fail with NaN.
  • 04.For Lasso on correlated features: run ElasticNet instead to get stable feature selection.
  • 05.For neural networks: ensure weight initialization is appropriate (He/Xavier) — poorly initialized weights interact badly with weight decay.
  • 01.Cross-validate λ using RidgeCV, LassoCV, or GridSearchCV with a log-scale grid.
  • 02.For Ridge/ElasticNet: use warm-start path (sklearn fits all λ simultaneously via efficient path algorithms).
  • 03.For neural networks: set weight_decay parameter in the optimizer (Adam or SGD) — pytorch applies L2 per-step.
  • 04.Monitor regularization path (plot coefficients vs. log(λ)) to understand which features are most important.
  • 05.Check that coefficients have sensible magnitudes after regularization — if all coefficients are near zero, λ is too large.

alpha (λ) — Ridge/Lasso/ElasticNet

Regularization strength. Larger = simpler model (more regularization). In LogisticRegression: C = 1/alpha.

10^[-3, -2, -1, 0, 1, 2] — search log-scale. Start with alpha=1.0

l1_ratio (ElasticNet)

Mix of L1 and L2: 0 = pure Ridge, 1 = pure Lasso, 0.5 = equal mix.

0.5 as starting point; search [0.1, 0.3, 0.5, 0.7, 0.9]

weight_decay (neural networks)

L2 penalty coefficient applied per parameter update in the optimizer. Equivalent to lambda/n in the loss formulation.

1e-4 to 1e-2 for most deep learning tasks. AdamW default: 0.01

  1. 1Preprocess: StandardScaler().fit_transform(X_train)
  2. 2Choose model: Ridge (all features relevant), Lasso (select features), ElasticNet (correlated + irrelevant)
  3. 3Use RidgeCV or LassoCV for efficient cross-validated alpha selection
  4. 4Fit: model.fit(X_train_scaled, y_train)
  5. 5Inspect: plot coef_ vs. feature names; count zero coefficients (Lasso)
  6. 6Evaluate: validation MSE/AUC at optimal alpha vs. unregularized baseline
05
06
python
1import numpy as np
2
3# ── Ridge Regression (closed-form) ────────────────────────────────────────────
4class RidgeRegression:
5    """w* = (XᵀX + λI)⁻¹ Xᵀy — always has a unique solution."""
6    def __init__(self, alpha=1.0):
7        self.alpha = alpha
8        self.weights = None
9        self.bias = None
10
11    def fit(self, X, y):
12        n, d = X.shape
13        # Augment X with column of ones for bias
14        X_b = np.c_[np.ones(n), X]          # (n, d+1)
15        # Build regularization matrix: don't regularize the bias (position 0)
16        reg = self.alpha * np.eye(d + 1)
17        reg[0, 0] = 0                         # bias is NOT regularized
18        w_full = np.linalg.solve(X_b.T @ X_b + reg, X_b.T @ y)
19        self.bias = w_full[0]
20        self.weights = w_full[1:]
21        return self
22
23    def predict(self, X):
24        return X @ self.weights + self.bias
25
26
27# ── Lasso via Coordinate Descent ──────────────────────────────────────────────
28class LassoRegression:
29    """Coordinate descent: update one weight at a time via soft thresholding."""
30    def __init__(self, alpha=1.0, max_iter=1000, tol=1e-4):
31        self.alpha = alpha
32        self.max_iter = max_iter
33        self.tol = tol
34        self.weights = None
35        self.bias = None
36
37    @staticmethod
38    def soft_threshold(rho, alpha):
39        """sign(rho) * max(|rho| - alpha, 0) — core of Lasso coordinate descent."""
40        return np.sign(rho) * np.maximum(np.abs(rho) - alpha, 0)
41
42    def fit(self, X, y):
43        n, d = X.shape
44        self.weights = np.zeros(d)
45        self.bias = np.mean(y)   # bias = mean of y when all weights are 0
46
47        for iteration in range(self.max_iter):
48            w_old = self.weights.copy()
49
50            # Update bias (not regularized): mean of residuals
51            residuals = y - X @ self.weights - self.bias
52            self.bias = self.bias + np.mean(residuals)
53
54            # Update each weight using coordinate descent
55            for j in range(d):
56                # Partial residual: residuals ignoring contribution of feature j
57                residual_j = y - self.bias - X @ self.weights + X[:, j] * self.weights[j]
58
59                # Partial correlation (how much feature j explains remaining residuals)
60                rho_j = (1 / n) * X[:, j] @ residual_j
61
62                # Soft threshold: set to zero if |rho_j| < alpha
63                self.weights[j] = self.soft_threshold(rho_j, self.alpha)
64
65            # Check convergence
66            if np.max(np.abs(self.weights - w_old)) < self.tol:
67                print(f"Converged at iteration {iteration}")
68                break
69
70        return self
71
72    def predict(self, X):
73        return X @ self.weights + self.bias
74
75    @property
76    def n_nonzero(self):
77        return np.sum(self.weights != 0)
78
79
80# ── Demo: Compare unregularized vs. Ridge vs. Lasso ───────────────────────────
81np.random.seed(42)
82n, d = 100, 50         # 100 samples, 50 features (underdetermined regime)
83
84# Only 5 features are truly informative; rest are noise
85w_true = np.zeros(d)
86w_true[:5] = [3.0, -2.0, 1.5, 4.0, -1.0]
87
88X = np.random.randn(n, d)
89y = X @ w_true + np.random.randn(n) * 0.5
90
91from sklearn.model_selection import train_test_split
92from sklearn.preprocessing import StandardScaler
93
94scaler = StandardScaler()
95X_s = scaler.fit_transform(X)
96X_train, X_test, y_train, y_test = train_test_split(X_s, y, test_size=0.2, random_state=42)
97
98# Ridge
99ridge = RidgeRegression(alpha=10.0).fit(X_train, y_train)
100ridge_mse = np.mean((ridge.predict(X_test) - y_test)**2)
101print(f"Ridge:  Test MSE = {ridge_mse:.4f}, nonzero = {np.sum(ridge.weights != 0)}/50")
102
103# Lasso
104lasso = LassoRegression(alpha=0.1).fit(X_train, y_train)
105lasso_mse = np.mean((lasso.predict(X_test) - y_test)**2)
106print(f"Lasso:  Test MSE = {lasso_mse:.4f}, nonzero = {lasso.n_nonzero}/50")
107print(f"Lasso nonzero weights at indices: {np.where(lasso.weights != 0)[0].tolist()}")
Ridge uses np.linalg.solve (Cholesky) instead of explicit matrix inversion — numerically superior. The regularization matrix explicitly excludes the bias term (reg[0,0] = 0) — the bias should never be regularized. Lasso's coordinate descent is exact and efficient: it cycles over features, soft-thresholding each one. The key insight: soft thresholding produces exact zeros (not just very small values), which is why Lasso gives true sparsity.
X = np.random.randn(100, 50)  # 100 samples, 50 features
w_true = [3.0, -2.0, 1.5, 0, 0, 0, ...]  # only 3 true features
y = X @ w_true + noise
OLS:        Test MSE = 8.74, features used = 50/50 (all noise)
Ridge:      Test MSE = 1.23, features used = 50/50 (all shrunk)
Lasso:      Test MSE = 0.38, features used = 3/50 (exact recovery!)
ElasticNet: Test MSE = 0.41, features used = 5/50
  • Never regularize the bias/intercept. The bias absorbs the overall level of y and should not be penalized — otherwise the model underfits systematically. sklearn handles this correctly with fit_intercept=True (default).
  • Lasso's coordinate descent works because each weight update has a closed-form solution (soft thresholding). The L1 norm is separable across dimensions, enabling exact coordinate-wise minimization.
  • Ridge has one global minimum (unique closed-form). Lasso also has one global minimum for any full-rank X. For underdetermined X (d > n), Lasso may not be unique but coordinate descent still converges.
  • The regularization path for Ridge is smooth and monotone — every coefficient shrinks uniformly as λ increases. For Lasso, the path is piecewise linear with 'kinks' where coefficients hit zero. This structure enables efficient path algorithms.
  • For neural networks, weight decay is applied AFTER the gradient step: w ← w(1 − 2αλ) − α·∇L. The factor (1−2αλ) multiplies the current weight, gradually decaying it toward zero — hence 'weight decay'.
  • Applying regularization to unscaled features — a feature with range [0, 1000] gets penalized 1000× more than a feature in [0, 1]. Always StandardScaler first.
  • Regularizing the intercept — some manual implementations include the bias in ||w||², causing systematic underfitting. Always exclude the bias from regularization.
  • Using the same lambda for both Ridge and Lasso — optimal lambdas differ by orders of magnitude because L1 and L2 norms have different scales.
  • Not increasing max_iter for Lasso on large datasets — the default 1000 iterations often triggers ConvergenceWarning. Set max_iter=10000.
07
📐

High-Dimensional Data (d >> n)

Excellent

Regularization was designed for this regime. Ridge uniquely solves OLS when XᵀX is singular. Lasso performs implicit feature selection, keeping only the most predictive features. Without regularization, models overfit completely.

💡 Use Lasso or ElasticNet for d >> n. RidgeCV for when all features are relevant. Always use cross-validation to select alpha.
🔗

Correlated Features (Multicollinearity)

Excellent

Ridge handles multicollinearity perfectly — it distributes weight evenly among correlated features. Lasso arbitrarily selects one from a group of correlated features (unstable). ElasticNet is the best choice: L2 groups correlated features while L1 selects among groups.

💡 If VIF > 10 for several features, use Ridge or ElasticNet. Check if Lasso's arbitrary selection is acceptable for your use case.
🎯

Sparse Signal (Few Truly Relevant Features)

Excellent

Lasso is designed for sparse signals. If only k << d features truly matter, Lasso recovers the exact sparse solution under the restricted isometry property (RIP). Ridge keeps all features with small weights — less interpretable.

💡 Lasso requires roughly n > k·log(d/k) samples to exactly recover a k-sparse signal. ElasticNet is safer when the sparsity assumption is uncertain.
📊

Small Dataset (n < 100)

Good

Regularization is critical for small datasets — the variance of unregularized estimates is enormous with few samples. Ridge or Lasso with cross-validated alpha prevents overfitting. Use LOOCV (leave-one-out) for very small n.

💡 With n < 30, prefer Ridge (stable) over Lasso (selection can be unstable with so few samples). Use very strong regularization.
🧠

Neural Networks

Good

L2 weight decay is the primary regularizer for neural networks (applied via optimizer weight_decay parameter). Effectively controls weight magnitude and prevents overfitting. Used alongside dropout, batch normalization, and data augmentation.

💡 AdamW (Adam + decoupled weight decay) is the correct implementation — standard Adam+L2 regularization interacts incorrectly with adaptive learning rates.

Already Well-Specified Model

Context-Dependent

If the model has exactly the right features and large n, regularization introduces unnecessary bias. The optimal lambda would be near zero. But cross-validation will correctly identify this — the penalty for applying regularization with cross-validated alpha is small.

💡 Even in this case, running RidgeCV and seeing best alpha ≈ 0 is informative — it confirms no regularization is needed. Never set alpha=0 without checking.
08

Interactive: Lambda, Ridge Shrinkage, and Lasso Feature Selection

Ridge shrinks coefficients

Lasso zeros 2 features

Ridge vs. Lasso: Regularization Path (Coefficients vs. λ)

As regularization strength λ increases: Ridge shrinks all coefficients smoothly toward zero (none reach exactly zero). Lasso shrinks coefficients to zero abruptly — features 'turn off' one by one. The Lasso path has sharp kinks at each feature's zero-crossing point.

Comparison visualization data is documented in this section.

Number of Non-Zero Features vs. Lambda (Lasso Sparsity)

As λ increases, Lasso selects fewer features. The model becomes increasingly sparse. At the optimal cross-validated λ, the model retains only the truly predictive features and discards noise. Ridge never produces exact zeros.

Gradient descent convergence — MSE decreasing over iterations

Geometric Interpretation: L1 Ball vs. L2 Ball

The L1 constraint region (diamond shape) has corners on the axes — the loss contours (ellipses) most often first touch the L1 ball at a corner, producing zeros. The L2 constraint region (circle) has no corners — the ellipse touches it at a smooth point, never forcing exact zeros.

The corners of the L1 diamond lie on the coordinate axes (where one weight is zero). The loss contour (ellipse) is most likely to first touch the L1 ball at one of these corners — not at an arbitrary smooth point. This is the geometric proof of Lasso sparsity.
09
  • Prevents overfitting — the primary benefit

    Regularization directly controls the bias-variance tradeoff. By penalizing weight magnitude, it prevents the model from fitting noise in the training data. Consistently improves generalization on held-out data, especially when n/d is small.

  • Lasso provides automatic feature selection

    L1 regularization drives irrelevant feature weights to exactly zero, effectively removing them from the model. This is more principled than stepwise regression and automatically scales to high-dimensional datasets where manual selection is infeasible.

  • Ridge enables stable solutions with collinear features

    Adding λI to the Gram matrix XᵀX makes it always invertible, regardless of rank deficiency. Ridge handles d > n, perfect multicollinearity, and near-singular matrices gracefully — scenarios where OLS completely fails.

  • Improves coefficient interpretability by stabilizing estimates

    Unregularized coefficients with multicollinearity can swing wildly with small data changes (high variance). Ridge shrinks correlated coefficients toward their average, producing stable estimates that are more interpretable and reliable.

  • Computationally cheap to add to existing training

    For Ridge: adds λI to one matrix computation. For neural networks: weight decay adds one multiplication per parameter per step. Essentially free compared to model training cost. The cross-validation to find λ is the most expensive part.

  • Bayesian interpretation provides probabilistic justification

    Ridge corresponds to MAP estimation with a Gaussian prior on weights. Lasso corresponds to a Laplace prior. This means regularization is equivalent to encoding a prior belief about weight distributions — a principled probabilistic framework.

  • Introduces bias — the fundamental trade-off

    Regularization penalizes large weights even when they are the correct values. A truly strong feature (e.g., wⱼ = 10) gets shrunk toward zero, introducing bias. The bias is worthwhile when it reduces variance by more — but near the optimal lambda boundary, this trade-off is delicate.

  • Lambda must be tuned — cross-validation cost

    There is no universal lambda. Cross-validating over a grid of 50–100 lambda values multiplies training time by 5–10× (for k-fold CV). RidgeCV/LassoCV mitigate this with efficient path algorithms, but the cost is non-negligible for large models.

  • Lasso is unstable with correlated features

    When multiple features are highly correlated, Lasso arbitrarily selects one and drives others to zero. Which feature is selected can change dramatically with small data perturbations — making the selected feature set unreliable. Use ElasticNet for correlated features.

  • L1 (Lasso) is not differentiable at zero — requires special solvers

    Standard gradient descent cannot handle the non-differentiability of |wⱼ| at 0. Lasso requires coordinate descent, ADMM, or sub-gradient methods. This makes Lasso slower to train than Ridge and incompatible with standard neural network autodiff without modification (proximal methods).

  • Risk of under-regularization if cross-validation is not stratified

    If the validation fold doesn't represent the test distribution (especially with imbalanced data or time-series), the selected lambda may not generalize. Stratified cross-validation or time-series cross-validation is essential for reliable lambda selection.

10
Genomics / Bioinformatics

Gene selection for disease prediction (GWAS)

Datasets with n=500 patients and d=20,000 genetic variants are the norm. Lasso selects the sparse set of genes actually predictive of disease. The selected genes are biologically interpretable — researchers investigate only the non-zero coefficient genes, not all 20,000.

Finance

Factor model regularization for portfolio optimization

Covariance matrix estimation for n assets from historical returns is ill-conditioned when n is large. Ridge regularization (shrinkage estimation, Ledoit-Wolf) stabilizes the covariance matrix. Lasso used for return prediction with many economic factors — selecting the sparse set of predictive factors.

Natural Language Processing

Regularized text classification with bag-of-words features

TF-IDF feature matrices have d=100,000+ features (vocabulary size) with n=10,000 documents. L1 logistic regression selects the sparse set of discriminative words. Coefficients reveal the most predictive words for each class — interpretable and deployable.

Healthcare / Clinical Prediction

Mortality prediction models (ICU, sepsis scoring)

Clinical prediction models use ridge/lasso on tabular patient features. Ridge handles correlated clinical measurements (blood pressure, heart rate correlate with severity). Lasso selects interpretable subsets of variables for clinical guidelines (simpler = more adoptable by clinicians).

Deep Learning

L2 weight decay in neural network training

Every major neural network trained in PyTorch/TensorFlow uses optimizer weight_decay (L2). BERT, ResNet, GPT use weight_decay=0.01 in AdamW. Without it, models memorize training data. Weight decay is the standard, always-on regularizer before dropout and batch normalization are added.

Signal Processing

Compressed sensing and sparse signal recovery

L1 minimization (basis pursuit) recovers sparse signals from fewer measurements than Shannon-Nyquist requires. Applications include MRI image reconstruction (JPEG/MRI scanners), seismic signal processing, and radar. The mathematical foundation is the same as Lasso — L1 promotes sparsity.

11

The three main regularization methods (Ridge, Lasso, ElasticNet) form a spectrum. The choice depends on whether you expect all features to be relevant, whether features are correlated, and whether you need exact sparsity.

Ridge (L2)

Same MSE fit loss; same gradient descent training framework

Shrinks all coefficients toward zero proportionally but never exactly to zero. Analytical closed-form: (XᵀX + λI)⁻¹Xᵀy. Best for multicollinearity — distributes weight evenly among correlated features. No feature selection.

When you believe all features are relevant (or don't want to discard any). When features are correlated. When you need a closed-form solution. Default regularizer for linear regression.

Lasso (L1)

Same MSE fit loss; requires special solver (coordinate descent)

Drives many coefficients to exactly zero — automatic feature selection. Unstable with correlated features. No closed-form; uses coordinate descent with soft thresholding. The regularization path is piecewise linear.

When you believe many features are irrelevant. When interpretability requires a sparse model. When d >> n and true sparsity is expected (genomics, text).

ElasticNet (L1 + L2)

Same MSE fit loss; uses coordinate descent

Combines L1 sparsity with L2 grouping. Selects a sparse set of features while distributing weight among correlated ones (unlike Lasso's arbitrary selection). Best of both worlds at the cost of an additional hyperparameter (l1_ratio).

When features are both correlated AND many are irrelevant. Default choice when uncertain between Ridge and Lasso. Kaggle standard for high-dimensional tabular data.

Dropout

Both regularize neural networks to prevent overfitting

Dropout randomly zeros activations during training — a stochastic regularization that prevents co-adaptation of neurons. Not equivalent to L1/L2 mathematically. Applied to activations, not weights. Doesn't produce sparse weight vectors.

Neural networks, particularly deep networks with fully-connected layers. Often used alongside weight decay. Rate typically 0.1–0.5; higher for large networks.

PropertyRidge (L2)Lasso (L1)ElasticNet
Produces exact zerosNoYesYes
Handles multicollinearityExcellentPoor (arbitrary)Good (groups)
Closed-form solutionYesNo (coord. desc.)No (coord. desc.)
Feature selectionNoYes (automatic)Yes (sparse)
Hyperparametersalphaalphaalpha + l1_ratio
Bayesian priorGaussianLaplaceHybrid

Ridge: correlated features, all relevant. Lasso: sparse signal expected, many irrelevant features. ElasticNet: default for high-d tabular data in production. Ridge for neural networks (weight decay).

12

Validation MSE / AUC-ROC at Optimal λ

The metric of primary interest: does regularization improve validation performance compared to unregularized baseline? Plot validation MSE vs. log(λ) — the U-shape indicates the optimal λ.

Target: Validation MSE at optimal λ should be lower than unregularized validation MSE; if not, regularization isn't helping

Number of Non-Zero Coefficients (Lasso)

Sparsity measure: how many features did Lasso select? Smaller = simpler model. Should stabilize across cross-validation folds — if it varies wildly, the model is selection-unstable (use ElasticNet).

Target: Stable across CV folds; consistent with domain knowledge about expected number of relevant features

Coefficient Stability (Bootstrap)

Bootstrap the training data B times, refit the regularized model each time, and measure what fraction of bootstraps agree on the sign of each coefficient. High stability (> 0.9) means the coefficient is reliable. Low stability means the model is uncertain about this feature.

Target: > 0.8 for each retained coefficient; unstable coefficients should be dropped or investigated

Degrees of Freedom (Ridge Effective df)

Effective degrees of freedom of the Ridge model. At λ=0: df = d (full OLS). As λ→∞: df → 0. Measures model complexity. Useful for AIC/BIC-based model selection as an alternative to cross-validation.

Target: Decreases smoothly with λ; at optimal λ, df is a reasonable fraction of d (not too close to 0 or d)

  1. 01.1. Establish unregularized baseline: fit OLS/logistic regression and record validation MSE/AUC.
  2. 02.2. Fit RidgeCV/LassoCV over log-scale alpha grid. Record best alpha and corresponding validation metric.
  3. 03.3. Compare regularized vs. unregularized validation performance — regularization should improve it.
  4. 04.4. For Lasso: inspect selected features (nonzero coefficients). Validate against domain knowledge.
  5. 05.5. For Ridge: inspect coefficient magnitudes — ensure they're in plausible range, not uniformly near-zero (over-regularized).
  6. 06.6. Check coefficient stability via bootstrap (10-50 resamples) — unstable selections indicate model uncertainty.
  • Selecting alpha based on training performance — training loss always decreases with smaller alpha. Must use cross-validation on a held-out set.
  • Not scaling features before regularization — regularization penalizes large-scale features disproportionately.
  • Assuming Lasso's selected feature set is 'correct' — with correlated features, different runs may select different (equally valid) subsets.
  • Using RidgeCV with cv=None — this uses efficient LOO-CV which is fast but doesn't work with Pipeline objects. Use cv=5 for pipeline compatibility.

Gene expression dataset: d=10,000 genes, n=500 patients, target = disease status (binary). Unregularized LR AUC = 0.61 (barely better than random). LassoCV: best alpha = 0.05, AUC = 0.84, 47 genes selected. Interpretation: 47 out of 10,000 genes are predictive; the remaining 9,953 are noise. The regularization improved AUC by 0.23 — a dramatic improvement. Bootstrap stability check: 38/47 selected genes are stable across 50 bootstraps. The 9 unstable ones are borderline — remove them and recheck AUC (probably 0.82 with 38 stable genes, acceptable for simplicity).

13
  • ×Confusing the regularization parameter conventions: sklearn Ridge uses alpha (larger = more), LogisticRegression uses C = 1/alpha (smaller = more). Always check the documentation.
  • ×Thinking Lasso 'deletes' features — it sets weights to zero, not features. The feature is still in X, but its coefficient is zero so it contributes nothing to predictions.
  • ×Applying regularization to a model that's underfitting — if training error is already high, adding more penalty makes it worse. Regularization only helps overfitting.
  • ×Not understanding why L1 gives sparse solutions — just memorizing 'L1 = sparse' without understanding the geometric or subgradient reason.
  • ×Fitting StandardScaler on the entire dataset (before train/test split) — causes data leakage. Always fit scaler only on training data.
  • ×Forgetting to set max_iter high enough for Lasso — default 1000 often insufficient; use 10000 or check for ConvergenceWarning.
  • ×Using Adam + explicit L2 loss instead of AdamW for weight decay in neural networks — they are not equivalent.
  • ×Setting alpha in Ridge vs. lambda in Lasso to the same value — optimal values differ by orders of magnitude between L1 and L2.
  • ×Saying 'L2 is always better than L1' — wrong: Lasso beats Ridge when the true model is sparse, and vice versa.
  • ×Not being able to explain why L1 gives sparse solutions from either a geometric or subgradient perspective.
  • ×Confusing Ridge and Lasso regression with Ridge and Lasso classifiers — they're the same penalty applied to different loss functions (MSE vs. BCE).
  • ×Saying 'regularization always helps' — not true when the model is already underfitting or when the training data exactly represents the test distribution.
  • ×Not cross-validating alpha — picking alpha=1 'as a default' without validating leads to either over- or under-regularization.
  • ×Applying the same alpha to all features globally when feature scales differ massively — scale first, always.
  • ×Using Lasso when features are highly correlated and then over-interpreting which features were 'selected' — selection is unstable in this regime.
  • ×Forgetting to include regularization during fine-tuning of neural networks — pre-trained models loaded for fine-tuning often lose weight_decay if the optimizer is reinitialized.
14

What kind of bias does this model have?

Bias depends on model assumptions and feature expressiveness.

What kind of variance does it have?

Variance grows with model flexibility and weak regularization.

How does it overfit?

Overfitting usually appears as strong train performance but weaker validation/test behavior.

How do we regularize it?

Use complexity constraints, robust validation, and data-centric cleanup.

What kind of data does it like?

Prefers representative, low-leakage data with stable feature definitions.

What kind of data breaks it?

Breaks under leakage, severe distribution drift, noisy labels, and poorly engineered features.

14

Quick Revision Reference

  • Regularization adds a penalty to the loss: L_total = L_task + λ·R(w)
  • Ridge (L2): R(w) = ||w||₂², shrinks all weights, no exact zeros, closed-form
  • Lasso (L1): R(w) = ||w||₁, produces exact zeros (sparsity = feature selection)
  • ElasticNet: combines L1 + L2 — sparse like Lasso but groups correlated features like Ridge
  • Always scale features before regularization (StandardScaler)
  • Never regularize the bias/intercept
  • Select λ via cross-validation (RidgeCV, LassoCV, or GridSearchCV)
  • For neural networks: L2 weight decay via optimizer's weight_decay parameter (use AdamW)
Ridge Loss
Ridge Solution
Lasso Loss
Soft Threshold
ElasticNet
  • High-dimensional data (d close to or exceeding n)
  • Correlated features (Ridge or ElasticNet)
  • Sparse signal recovery (Lasso)
  • Neural network training (weight decay)
  • Model is already underfitting
  • Features are confirmed relevant and n >> d
  • Exact coefficient values are needed (Ridge shrinks them, Lasso zeros them)
Explain geometrically why L1 gives sparse solutions (diamond vs. circle constraint region)
Derive Ridge closed-form solution: (XᵀX + λI)⁻¹Xᵀy
Explain soft thresholding for Lasso coordinate descent
Bayesian interpretation: Ridge = Gaussian prior, Lasso = Laplace prior
Differences between Adam+L2 and AdamW for neural network regularization
15
16

These questions are designed to break assumptions and expose weak understanding. Most people will answer them wrong on their first attempt. Work through each one carefully.