2.6.4 · D3Model Evaluation & Selection

Worked examples — K-fold cross-validation

3,103 words14 min readBack to topic

This page is the worked-example gym for K-fold Cross-Validation. The parent note built the machinery; here we run it against every kind of input a real problem can throw at you — clean data, messy data, tiny data, imbalanced data, and the traps that make the number lie to you.

Before we touch numbers, one promise: we will never write a symbol you haven't met. Let us re-anchor every symbol this page uses.

Figure — K-fold cross-validation
Figure 1 — The K-fold layout (). The full bar is the dataset of samples, chopped into equal folds . In iteration (shown), the red pile is the exam (validation) and every black pile together forms the training set . Slide the red pile along and you get all iterations.

And the spread measure:

Figure — K-fold cross-validation
Figure 2 — What measures. Each red arrow is one fold's distance from the dashed mean line. Long arrows on the right (Model Q) mean a big ; the arrows collapsing to nothing on the left (Model P) mean .


The scenario matrix

Every K-fold problem lives in one of these cells. The examples below are chosen so each row gets touched at least once.

Cell What makes it special Covered by
A. Clean divisible case divides evenly by Ex 1
B. Non-divisible is not a whole number → uneven folds Ex 2
C. Degenerate Leave-One-Out (each fold = 1 sample) Ex 3
D. Degenerate / too big invalid / trivial edges Ex 3 (twist)
E. Zero-spread folds all scores identical → Ex 4
F. High-spread folds scores swing wildly → large Ex 4
G. Imbalanced classes stratification needed or a fold breaks Ex 5
H. Regression metric metric is an error (RMSE), lower = better Ex 6
I. Real-world word problem pick under a time budget Ex 7
J. Exam-style twist leakage / nested-CV trap Ex 8

The worked examples

Figure — K-fold cross-validation
Figure 3 — Left: a naive random split can starve some folds of the rare red class-B samples (fold 4 has none — its accuracy is meaningless). Right: stratified splitting places exactly red B-samples in every fold, so each pile mirrors the dataset.


Recall Quick self-test

Fold sizes for , ? ::: (the first folds get the extra sample). How should CV average uneven folds exactly? ::: Sample-weighted mean ; the plain mean is fine when folds differ by at most one sample. Legal range of for ? ::: ; is LOOCV. Two models, same-ish story, one has and one — trust which? ::: The model; its folds agree. For RMSE, is bigger better or worse? ::: Worse — RMSE is an error, lower is better. Whole-dataset normalization (global ) before splitting causes what? ::: Data leakage → optimistically inflated scores. Does our divide by or by then again? ::: By inside the root — it's the population standard deviation, not the standard error of the mean.

Go deeper on the mechanics in the parent: K-fold Cross-Validation · or read it in Hinglish.