AI Cross-Validation Tutorial: K-Fold, Stratified

TL;DRCross-validation estimates real performance by training/testing on rotated data folds.

A single train/test split can be lucky or unlucky. k-fold cross-validation splits data into k parts, trains on k-1 and tests on the held-out fold, rotating through all k, then averages — a more reliable performance estimate that uses all data for both roles. Stratified folds preserve class ratios. Use it especially on smaller datasets where one split is too noisy.

Key points

Common mistakes

Try it: Explain why averaging over folds beats a single split.

Example code

<!doctype html><html><head><meta charset="utf-8"></head>
<body style="background:#06040d;color:#e6e0ff;font-family:monospace;padding:20px"><pre>5-fold: train on 4 parts, test on 1, rotate ×5
average the 5 scores → reliable estimate</pre></body></html>
Open the interactive lesson →
Overfitting · The Bias-Variance Trade Train · Val · Test Splits