TensorTau PathToAGI Doing Machine Learning Honestly Level 05

Level 05 · Doing Machine Learning Honestly · Chapter 066

Cross Validation

When data is scarce, how can we estimate generalization without permanently sacrificing a large validation set?

1. Let every example take a turn

In KK-fold cross validation, split the training data into KK folds. Train on K−1K-1 folds and validate on the remaining fold. Repeat until every fold has served as validation once.

run 1: V T T T T
run 2: T V T T T
run 3: T T V T T
...

Average the validation scores.

mˉ=1K∑k=1Kmk.\bar m=\frac1K\sum_{k=1}^{K}m_k.

2. Why it helps

One train/validation split can be lucky or unlucky. Cross validation exposes the model to several different held-out subsets and gives us both a mean score and a sense of variability.

3. The split must respect the task

Ordinary random K-fold is wrong for some problems. Use stratified folds for imbalanced classification, group-aware folds when records from one entity must stay together, and time-aware validation for forecasting.

The validation design should mimic deployment.

4. Preprocessing belongs inside each fold

For every fold, scalers, imputers, feature selectors and PCA must be fitted only on that fold’s training portion. Otherwise information leaks from its validation portion.

5. Cross validation is not the final test

Use cross validation for model selection and uncertainty during development. Keep a final test set untouched when an unbiased final estimate matters.

What to Remember

Cross validation reuses scarce training data efficiently while repeatedly testing on unseen folds. Its power depends on choosing folds that respect real-world independence.

Next → Generalization: what do we really mean when we say performance on sampled data tells us something about unseen data?

Six ways to work the chapter, climbing from explaining it in your own words to investigating it like a researcher. Reading is not enough — this is where the chapter lands.

  1. Explain: Why does K-fold CV give more evidence than one split?
  2. Calculate: Compute mean and standard deviation for fold scores [0.80,0.84,0.78,0.82,0.86].
  3. Predict: What goes wrong if one patient’s visits appear in multiple folds?
  4. Break: Compare ordinary K-fold with time-aware validation on time-ordered data.
  5. Build: Implement K-fold indices from scratch.
  6. Investigate: Compare 5-fold and 10-fold estimates across repeated runs, including compute cost.