← Master Index
Vol. 05 Module 5.4 Lecture

Regularization

Model Optimization

How This Lesson Fits the Module

You understand bias and variance; regularization is the practical lever that penalizes complexity so models generalize. It appears in linear models (Ridge, Lasso), tree pruning, SVM margin, boosting learning rate, and—in Volume 06—weight decay and dropout in neural networks.

Learning Objectives

By the end of this lesson, students should be able to:

  • Define regularization as a penalty on model complexity added to the loss.
  • Explain how regularization reduces variance at the cost of some bias.
  • Identify the regularization hyperparameter (λ, alpha, C) in common algorithms.
  • Tune regularization strength with cross-validation.
  • Map classical penalties (L1, L2) to sklearn estimators in upcoming lessons.

The Core Idea

Instead of minimizing loss alone, we minimize loss + penalty:

Objective = Data Loss + λ × Complexity Penalty

Larger λ (or alpha in sklearn) shrinks or sparsifies parameters, discouraging fits that exploit noise. Too much regularization causes underfitting; too little invites overfitting.

AlgorithmRegularization knobEffect
Ridge / Lasso / ElasticNetalphaWeight shrinkage / sparsity
Logistic regressionC (inverse strength)Larger C = less penalty
Decision treemax_depth, min_samples_leafLimits tree complexity
SVMCMargin vs misclassification tradeoff
Neural net (Vol. 06)weight decay, dropoutParameter and activation penalties
Engineering Habit — Tune on Validation, Not Test

Sweep alpha or C with GridSearchCV inside pipelines. The regularization hyperparameter is as important as model choice—defaults are rarely optimal for your signal-to-noise ratio.

Tuning Regularization Strength

from sklearn.linear_model import Ridge from sklearn.model_selection import GridSearchCV from sklearn.pipeline import Pipeline from sklearn.preprocessing import StandardScaler pipe = Pipeline([ ("scale", StandardScaler()), ("ridge", Ridge()), ]) search = GridSearchCV( pipe, param_grid={"ridge__alpha": [0.01, 0.1, 1, 10, 100]}, cv=5, scoring="neg_mean_squared_error", ) search.fit(X_train, y_train) print(search.best_params_, search.best_score_)

Implicit vs Explicit Regularization

Explicit penalties

  • L1, L2 on weights
  • ElasticNet combination
  • Added directly to loss

Implicit / structural

  • Early stopping (stop before overfit)
  • Bagging averages high-variance models
  • Bayesian priors on parameters
  • Data augmentation as regularization
Critical Mistake — Regularizing Without Scaling

L2 penalizes large coefficients equally in raw feature units. A feature in dollars dominates one in 0–1 range. Fit StandardScaler in a pipeline before Ridge/Lasso so penalties are fair.

Knowledge Check

  1. Short Answer: What does increasing λ generally do to variance? Answer: Reduces it.
  2. True/False: In sklearn Ridge, larger alpha means weaker regularization. Answer: False—stronger penalty.
  3. Multiple Choice: C in SVM is: (a) penalty strength, (b) inverse regularization, (c) learning rate. Answer: (b).
  4. Short Answer: Why pipeline scaling before L2? Answer: So penalties apply fairly across feature scales.
  5. Short Answer: Name one non-L1/L2 regularization. Answer: e.g., tree depth limit or early stopping.
  6. True/False: Regularization adds a complexity penalty to the training loss. Answer: True.
  7. Multiple Choice: Too large λ typically: (a) overfits, (b) underfits, (c) removes noise. Answer: (b).
  8. Short Answer: How should you choose alpha / λ? Answer: Cross-validation on training data, not the test set.
  9. True/False: Weight decay in neural nets is an L2-style regularizer. Answer: True.
  10. Multiple Choice: Larger Ridge alpha / smaller SVM C means: (a) weaker regularization, (b) stronger regularization, (c) identical names. Answer: (b).

Key Takeaways

  • Regularization = loss + complexity penalty controlled by λ / alpha.
  • It trades a bit of bias for lower variance and better generalization.
  • Always tune the strength with cross-validation in pipelines.
  • Scale features before weight penalties.
  • Next: L1 — sparsity-inducing regularization.
Trainer’s Guide

Hands-on idea: Plot validation MSE vs alpha on log scale for Ridge on a wide dataset. Students identify under- and over-regularized regions.

Discussion prompt: Is early stopping in deep learning “regularization”? Defend yes or no.

Recap: Regularization trades a little bias for less variance by penalizing complexity—tune strength with CV. Continue with L1.