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.
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.
k-fold cross-validation partitions the data into equal folds. For each fold in turn, train on the other folds and evaluate on the one held out:
- CV error — the k-fold cross-validation estimate of error: the average held-out error across every fold.
- — the number of folds the data is split into.
- — the index of the current fold, ranging from to .
- MSE — mean squared error: the metric used to score each fold's held-out predictions.
- 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.
- Repeating it reveals disagreement, not just a mean
Cross-validation runs the experiment times and reports both the mean and how much the folds disagreed — a spread that a single split can never reveal about itself.
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.
Ten points following , except , where instead of the line's :
- Most folds barely notice the anomaly
Holding out , fitting on the other 8 (including the anomaly) recovers — the anomaly pulls the intercept up slightly from the clean line's , but the slope stays exactly . Predicting the held-out points:
- : predicted , actual , squared error
- : predicted , actual , squared error
MSE — the held-out points still land close to the fitted line.
- One fold holds out the anomaly itself
Holding out trains on 8 clean points, recovering the line almost exactly (). Predicting the held-out points:
- : predicted , actual , squared error
- : predicted , actual (the anomaly), squared error
MSE .
- 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 ):
- Fold (holdout ): (shown above)
- Fold (holdout ): (shown above)
- Fold (holdout ):
- Fold (holdout ):
Mean ; stdev across those five values — 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- fold above, scoring : not wrong, but silent about how much luckier or unluckier it could have been.
Pick the one fold whose held-out score is far worse than the rest.
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.