Part VII — Model Evaluation, Validation & Feature Engineering · Chapter 3

k-fold & stratified cross-validation

Hook

Every model in this course so far has been scored by fitting on some data and testing on the rest. But which points end up in the test set is a choice — and a different choice can tell a completely different story about how good the model is.

Intuition

Ten points trend almost perfectly along a line — except one, which sits far off it. Step through five different train/test splits of the same ten points and watch the held-out error swing from barely-there to enormous, depending only on whether that one point happened to land in the test set.

Formalize

k-fold cross-validation partitions the data into kk equal folds. For each fold in turn, train on the other k1k-1 folds and evaluate on the one held out:

CV error=1ki=1kMSE(model trained without fold i, tested on fold i)\text{CV error} = \frac{1}{k}\sum_{i=1}^{k} \text{MSE}(\text{model trained without fold } i,\ \text{tested on fold } i)
  • CV error — the k-fold cross-validation estimate of error: the average held-out error across every fold.
  • kk — the number of folds the data is split into.
  • ii — the index of the current fold, ranging from 11 to kk.
  • MSE — mean squared error: the metric used to score each fold's held-out predictions.
  1. A single split is one sample from the process

    A single train/test split gives you one sample from this process — one particular way the anomaly (or any unusual point) could land, for better or worse.

  2. Repeating it reveals disagreement, not just a mean

    Cross-validation runs the experiment kk times and reports both the mean and how much the folds disagreed — a spread that a single split can never reveal about itself.

Play

Four of the five folds score a modest error. The one fold holding out the anomaly alone scores dramatically worse — over 30 times higher than the best fold. The mean folds all five outcomes into a single honest number, and the stdev quantifies exactly how much that number should be trusted.

Worked example

Ten points following y=2x+1y = 2x + 1, except x=6x=6, where y=30y=30 instead of the line's 1313:

  1. Most folds barely notice the anomaly

    Holding out x=3,4x=3,4, fitting on the other 8 (including the anomaly) recovers y2x+3.125y\approx2x+3.125 — the anomaly pulls the intercept up slightly from the clean line's 11, but the slope stays exactly 22. Predicting the held-out points:

    • x=3x=3: predicted 9.125\approx9.125, actual 77, squared error 4.52\approx4.52
    • x=4x=4: predicted 11.125\approx11.125, actual 99, squared error 4.52\approx4.52

    MSE =(4.52+4.52)/24.52=(4.52+4.52)/2\approx4.52 — the held-out points still land close to the fitted line.

  2. One fold holds out the anomaly itself

    Holding out x=5,6x=5,6 trains on 8 clean points, recovering the line almost exactly (y=2x+1y = 2x+1). Predicting the held-out points:

    • x=5x=5: predicted 1111, actual 1111, squared error 00
    • x=6x=6: predicted 1313, actual 3030 (the anomaly), squared error 172=28917^2=289

    MSE =(0+289)/2=144.5=(0+289)/2=144.5.

  3. One number, or five, or the truth

    Running the same fit-and-hold-out recipe for the remaining three folds gives each fold's MSE:

    • Fold (holdout x=1,2x=1,2): 9.85\approx9.85
    • Fold (holdout x=3,4x=3,4): 4.52\approx4.52 (shown above)
    • Fold (holdout x=5,6x=5,6): 144.5144.5 (shown above)
    • Fold (holdout x=7,8x=7,8): 7.39\approx7.39
    • Fold (holdout x=9,10x=9,10): 26.73\approx26.73

    Mean =(9.85+4.52+144.5+7.39+26.73)/538.60=(9.85+4.52+144.5+7.39+26.73)/5\approx38.60; stdev across those five values 59.83\approx59.83 — a spread almost as large as the mean itself. A single 80/20 split (train on the first 8, test on the last 2) is exactly the holdout-x=9,10x{=}9,10 fold above, scoring 26.7326.73: not wrong, but silent about how much luckier or unluckier it could have been.

Checkpoint

Pick the one fold whose held-out score is far worse than the rest.

Pick a fold to try it
Summary
CV error=1ki=1kMSEi,stdev across folds tells you how much to trust it\text{CV error} = \frac{1}{k}\sum_{i=1}^{k}\text{MSE}_i, \qquad \text{stdev across folds tells you how much to trust it}

A single train/test split answers "how did the model do on this particular split?" k-fold cross-validation answers the question that actually matters: "how did the model do, and how much would that answer have changed if the data had been split differently?" Every metric from the last two chapters — accuracy, F1, AUC — inherits this same fragility, and everything in this chapter applies to them exactly the same way.