Lite / Independent Engineering Research / Week 23
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?
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.
Predict
With 5-fold cross-validation, how many times is each example used for testing?
- Never: testing uses separate data
- Exactly once
- Five times
Guess first, then reveal
Exactly once. The data are split into 5 folds; each fold takes one turn as the test set while the model trains on the other 4. Every example is tested once and used for training 4 times.
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?
What the interactive shows (text version)
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.
Watch (optional)
Watch for: 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.
“Machine Learning Fundamentals: Cross Validation” by StatQuest with Josh Starmer (opens YouTube; streaming video uses a lot of data)
The video is optional. Everything you need is in this lesson's text.
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
Intuitive
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.
Technical
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.
Mathematical
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.
Applied
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.
Practice
Practice, not a test. Answers are checked on our server; a wrong answer gets a hint and another try. Nothing is saved.
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.
Reflect
Why does testing a model on the data it was trained on reward the wrong thing, and how does k-fold cross-validation stop that without wasting data?
Write your answer on paper or in your own notes. Lite never stores what you write.