The Memorization Fallacy
A student who memorizes every question in the practice textbook may score 100% on practice homework, but fails completely on the unseen final exam. Machine learning is not about achieving zero loss on training samples; it is about Generalization, the ability to make accurate predictions on data the algorithm has never seen before.
Hypothesis Spaces & Empirical Risk Minimization
We assume an unknown target distribution generates pairs (x, y). We select a Hypothesis Space H of candidate functions f(x; w). The Loss Function L(y, y_hat) measures individual prediction error. The Cost Function J(w) averages this loss across all N training samples. Empirical Risk Minimization (ERM) searches for the weight vector w* that minimizes this training cost.
Parameters vs Hyperparameters
Parameters are the internal coefficients learned directly by the algorithm from data during training (e.g. slopes w_1 and bias w_0 in regression, split thresholds in trees). Hyperparameters are structural knobs set by the engineer before training begins (e.g. learning rate alpha, polynomial degree d, tree max_depth, regularization penalty lambda). Hyperparameters are tuned using validation data.
The Polynomial Capacity Test
You have 15 noisy data points generated from a gentle sine wave y = sin(x) + noise. You fit a degree-14 polynomial through all 15 points, achieving exactly 0.000 training error.
What will happen to the model’s prediction error on 100 new out-of-sample test points?
The Bias-Variance Decomposition
The expected out-of-sample prediction error of any estimator decomposes into three fundamental terms: 1) Bias Squared: error stemming from wrong assumptions (e.g. assuming a linear line for a curved phenomenon). 2) Variance: sensitivity to fluctuations in the training set. 3) Irreducible Error sigma^2: noise inherent in the physical universe.
Diagnosing Bias vs Variance from Loss Curves
Compare your training loss and validation loss: 1) High Bias (Underfitting): Both training loss and validation loss are high and close together. The model is too simple. Solution: Increase complexity, add features. 2) High Variance (Overfitting): Training loss is very low, but validation loss is significantly higher (a large Generalization Gap). Solution: Collect more data, apply regularization, reduce features.
Regularization: Taming Model Capacity with L1 & L2
To prevent overfitting without discarding features, we append a complexity penalty to the loss function: J_reg(w) = J_loss(w) + lambda * Penalty(w). L2 Ridge penalizes the squared sum of weights (shrinking coefficients toward zero smoothly). L1 Lasso penalizes the absolute sum of weights (forcing uninformative weights to become exactly zero, yielding sparse feature selection).
Ridge vs Lasso in Python
Inspect how L1 Lasso forces irrelevant coefficients to exactly 0.0, performing automatic feature selection.
import numpy as np
from sklearn.linear_model import Ridge, Lasso
# Synthetic high-dimensional data (100 samples, 10 features, only 2 informative)
X = np.random.randn(100, 10)
y = 3.0 * X[:, 0] - 2.5 * X[:, 1] + 0.1 * np.random.randn(100)
# 1. Ridge Regression (L2 penalty) - shrinks weights smoothly
ridge = Ridge(alpha=1.0)
ridge.fit(X, y)
# 2. Lasso Regression (L1 penalty) - zeros uninformative weights
lasso = Lasso(alpha=0.3)
lasso.fit(X, y)
print("Ridge weights:", np.round(ridge.coef_, 2))
print("Lasso weights:", np.round(lasso.coef_, 2)) # Notice zeros!Lasso produces sparse models because its L1 diamond constraint boundary touches loss contours at axis vertices where coordinates are zero.
Diagnostic Knowledge Check
Verify your mastery of hypothesis spaces, generalization, and regularization.
Synthesis & Path Ahead
You have mastered hypothesis spaces, empirical risk minimization, the bias-variance tradeoff, and regularization geometry. Now, you are fully prepared to build and inspect your first complete machine learning model in Stage 05: Linear Regression & Ordinary Least Squares.