School/✦ Research Engineer/Elective · AIP-520
Cross-validation: grading a model fairly
Your classifier scores 97% on your data, then 71% on the device in the field. Which number was lying, and how could you have known before shipping?
About 40 min · 3 goals · Checked by automated tests
1.Why should I care?
A small dataset is the normal situation in embedded machine learning: a few hundred labelled vibration recordings, not millions of images. One lucky train/test split can make a model look far better or worse than it is. Cross-validation uses every example for testing exactly once, so the score you report is the one you can defend.
- ◇ Run k-fold cross-validation: split, train on k − 1 folds, test on the held-out fold, repeat.
- ◇ Report the mean and spread of fold scores and say what the spread tells you.
- ◇ Spot data leakage and keep a final test set untouched by model choices.
2.Predict
With 5-fold cross-validation, how many times is each example used for testing?
Guess first. Being wrong here is how the surprise works.
3.Discover
Choose k = 2, then 4, then 6. Click through the held-out folds. How do the fold accuracies, their mean and their spread change as k grows?
Loading the interactive…
Can't use the interactive? Read a description instead
The k-fold diagram shows 24 labelled readings as a row of boxes numbered by fold (example i belongs to fold i mod k); the held-out fold is filled in. For each fold a threshold classifier is trained on the other folds (threshold halfway between the two class means) and tested on the held-out fold. A list gives each fold's training size, test size and accuracy, and the readout reports the mean accuracy, its standard deviation and its range for k = 2, 3, 4 or 6.
4.Checkpoint: guided practice
Check what you just discovered before going on. Hints appear after a wrong answer.
Practice 1 · a fresh version every time
A dataset of 48 labelled examples is split into 8 equal folds for cross-validation. Each model is trained on all folds except the one it is tested on. How many examples does each model train on?
5.Watch
→ Watch for every block of data taking a turn as the test set, and how the results are combined.
Cross-validation: how to test a model on data it hasn't seen.
6 min 4 s · captions on YouTube · source
Nothing loads from YouTube until you press play.
The video is optional: everything you need is also in the lesson text and the interactive.
6.Understand
Why one split is not enough
Testing on the data you trained on rewards memorising: an overfitted model scores brilliantly and fails in the field. A single held-out split is honest but noisy, especially with little data: which examples happened to land in the test set can swing the score by several per cent. k-fold cross-validation reduces that luck by repeating the split k times.
The k-fold recipe
Shuffle (unless order matters), then cut the data into k equal folds. For each fold in turn: train a fresh model on the other k − 1 folds, test it on this one, record the score. With 1000 examples and k = 5, each model trains on 800 and is tested on 200. Report the mean of the k scores and their spread (standard deviation or range). Common choices are k = 5 or 10.
Reading the spread
Fold scores of 82, 85, 79, 88 and 81% have mean 83.0% and sample standard deviation about 3.5 points. That spread is part of the result: a rival model scoring 84% is not clearly better. A large spread means the model is sensitive to which data it sees, often a sign of too little data or of a few unusual examples clustered in one fold.
Leakage and the final test
Leakage is test information sneaking into training: normalising with statistics computed on all the data before splitting, near-duplicate recordings in different folds, or shuffling time-series so the model trains on the future. Do every preprocessing step inside each fold, and split by recording session or device, not by row. Use cross-validation to choose models; keep a separate test set you touch once, at the very end.
Explain it another way
It is like a fair exam rota. Instead of one mock exam that might happen to cover your favourite topics, everyone in the class writes one question each, and you are tested on every question once while studying from the rest. Your average across all rounds is a far better guide to the real exam.
k-fold CV estimates generalisation error with lower variance than a single split. Larger k means more training data per model (less pessimistic bias) but more models to train and correlated estimates; leave-one-out (k = n) is the extreme. Stratified folds keep class proportions equal; grouped folds keep all samples from one session or device together to avoid leakage. Nested CV separates hyperparameter tuning from evaluation.
Folds F₁…F_k partition the n examples, |Fᵢ| = n/k. Model mᵢ is trained on all data except Fᵢ and scored sᵢ on Fᵢ. Report s̄ = (1/k)Σsᵢ and s = √(Σ(sᵢ − s̄)²/(k − 1)). Example: 82, 85, 79, 88, 81 → s̄ = 83.0, deviations −1, 2, −4, 5, −2, Σ squares = 50, s = √(50/4) ≈ 3.54.
For a vibration classifier recorded on 6 machines, split by machine (6 grouped folds): each fold asks “does it work on a machine it has never heard?”, which is the question the field deployment will ask. Splitting by individual recordings would mix each machine into both sides and report a falsely high score.
Worked example 1
A dataset has 500 examples and you use 10-fold cross-validation. How many examples does each model train on and test on?
Each fold has 500 / 10 = 50 examples. Each model tests on 50 and trains on the other 450.
Worked example 2
Fold accuracies are 82, 85, 79, 88 and 81%. Report the result properly.
Mean = 415 / 5 = 83.0%. Sample standard deviation = √(50 / 4) ≈ 3.5 points. Report 83.0% ± 3.5 (5-fold CV), range 79–88%.
Common mix-ups
- Cross-validation replaces a final test set.
✓ It is for choosing between models and settings. Keep a separate test set untouched until the end, or your choices quietly overfit the CV score. - A higher mean CV score always means a better model.
✓ Compare against the spread: a 1-point gain inside a 3.5-point standard deviation is not convincing evidence. - Shuffling rows is always safe.
✓ For time series or repeated recordings, shuffling leaks the future or near-duplicates into training; split by time, session or device.
7.Practice on your own
Practice, not a test. Wrong answers get a hint and another try.
Practice 2
You run 10-fold cross-validation on 500 examples. How many examples are in each held-out test fold?
Practice 3
Which of these causes data leakage in cross-validation?
Practice 4
Fold accuracies are 90, 72, 88, 91 and 74%. What should you report?
Practice 5 · a fresh version every time
A classifier is evaluated with 10-fold cross-validation. The accuracies on the 10 held-out folds are 81.7%, 71.2%, 86.5%, 84.4%, 81.8%, 81.9%, 74.2%, 72.9%, 74.9%, 81.5%. What is the mean cross-validated accuracy, in percent? (2 decimal places)
8.Apply it
You have 240 labelled audio clips from 8 rooms (30 per room) for a keyword spotter. Design the evaluation: how you would form folds (and why by room), how many clips each model trains and tests on, what you keep back as a final test, and the one table you would publish.
9.Reflect
Concepts: Overfitting and held-out tests · Evaluation metrics