Overfitting, Underfitting & Regularization

Machine Learning Fundamentals

Chapter 8 · Overfitting, Underfitting & Regularization

ml1-7 showed the clearest possible overfitting example in this course — a tree memorizing training rows one at a time. ml1-2 previewed cross-validation and promised this chapter would deliver it in full. Both threads close here.

Two Failure Modes, Formally

OverfittingUnderfitting
What happensModel too complex — fits noise/idiosyncrasies in the training dataModel too simple — misses real patterns entirely
Training errorLowHigh
Test errorHighHigh
Exampleml1-7's own unconstrained treePredicting the same average price for every car, ignoring mileage and year entirely

The diagnostic signature is the gap: overfitting shows low training error but high test error; underfitting shows high error on both — the model was never good, even on data it directly saw.

The Bias-Variance Tradeoff — Built on ds1-6's Own Vocabulary

Bias is systematic, consistent wrongness — error from a model too simple to capture the real pattern, present regardless of which training sample it happened to see (underfitting). Variance reuses ds1-6's own definition directly: how much would this model's own predictions change if trained on a different random sample from the same population? A single unconstrained tree (ml1-7) has high variance — a different training sample would grow a noticeably different tree. A random forest has lower variance — averaging many trees stabilizes the result regardless of which specific sample any one tree happened to see.

The tradeoff, honestly
Total error breaks down, conceptually, into bias² + variance + irreducible noise (noise no model could ever remove). Reducing bias (a more complex, flexible model) tends to increase variance; reducing variance (a simpler, more constrained model) tends to increase bias. There is no free way to drive both to zero simultaneously — every technique in this chapter is a deliberate, controlled trade of one against the other.

Reading a Learning Curve

Plotting training error and validation error against training-set size (or model complexity) gives a real diagnostic picture. High bias shows both curves converging to a similarly poor error — more data doesn't help, because the model itself is too simple to use it. High variance shows a persistent gap — low training error, meaningfully higher validation error — that more data typically narrows, since a larger, more representative sample gives a flexible model less room to fit pure noise.

Regularization — Deliberately Trading Bias for Variance

Regularization adds a penalty term to ml1-3's own least-squares fitting objective, discouraging large coefficient values. This is the bias-variance tradeoff, applied on purpose: a regularized model fits the training data very slightly worse (a bit more bias) in exchange for being noticeably less sensitive to the specific training sample it saw (less variance).

from sklearn.linear_model import Ridge, Lasso

ridge = Ridge(alpha=1.0)   # L2
lasso = Lasso(alpha=1.0)   # L1
L2 (Ridge)L1 (Lasso)
Effect on coefficientsShrinks all of them, smoothlyCan shrink some coefficients to exactly zero
Practical side effectNone beyond shrinkageAutomatic feature selection — zeroed features are effectively dropped

Cross-Validation — ml1-2's Own Promise, Delivered

ml1-2 flagged the real risk: a single validation split can be unlucky on a small dataset. k-fold cross-validation fixes this directly: split the data into k equal folds, train on k−1 of them and validate on the one held out, rotate which fold is held out across k full rounds, then average the k resulting scores.

from sklearn.model_selection import cross_val_score

scores = cross_val_score(model, X, y, cv=5)
scores.mean(), scores.std()

Every row gets used for validation exactly once and for training k−1 times — a far more reliable estimate of real performance than any single split, and the standard deviation across the k scores is itself informative: a small spread means the model performs consistently regardless of which slice of data it sees; a large spread is itself a sign of high variance.

A Practical Checklist

SymptomDiagnosisTry
High error, train and testUnderfittingMore features, less regularization, a more flexible model
Low train error, high test errorOverfittingRegularization, more data, ml1-7's own random forest, cross-validation to confirm

Hands-On Exercises

Exercise 1

Using this chapter's own compare-table, explain why overfitting and underfitting produce different patterns of training vs. test error, and explain why "high error on both" specifically rules out overfitting as the diagnosis.

📄 View solution
Exercise 2

Explain, using ds1-6's own variance definition and this chapter's own comparison between a single tree and a random forest, exactly what "variance" means for a model, and why a random forest has lower variance than a single unconstrained tree.

📄 View solution
Exercise 3

Explain why this chapter frames regularization as "the bias-variance tradeoff, applied on purpose" rather than a separate, unrelated technique, and explain the specific practical difference between L1 and L2's effect on coefficients.

📄 View solution

Chapter 8 Quick Reference

  • Overfitting — low train error, high test error · Underfitting — high error on both
  • Bias — systematic error from too-simple a model · Variance — ds1-6's own definition: how much predictions change across different training samples
  • Every technique in this chapter deliberately trades one against the other — no free way to reduce both at once
  • Regularization (L1/Lasso, L2/Ridge) — a controlled bias-for-variance trade; L1 can zero out coefficients entirely (feature selection)
  • k-fold cross-validation — ml1-2's own deferred promise, delivered: a far more reliable performance estimate than one split
  • Next chapter: Unsupervised Learning: Clustering