Cross-Validation Explained: How K-Fold Works and Why It Beats a Single Split
Why a single train/test split gives noisy estimates, how k-fold cross-validation fixes it, choosing k, stratification, and the leakage mistakes to avoid.
Every model needs to be judged on data it hasn't seen — that part everyone knows. The subtler problem is that a single train/test split is a lottery: shuffle differently and your accuracy can swing by several points, because which examples happen to land in the test set matters. Cross-validation fixes this by making *every* example take a turn in the test set, then averaging the results into one steadier, more trustworthy estimate.
How k-fold cross-validation works
- Shuffle the dataset and split it into k equal parts, called folds (k = 5 or 10 is standard).
- Train the model on k−1 folds and evaluate it on the one held-out fold.
- Repeat k times, so each fold serves as the test set exactly once.
- Average the k scores — that average is your performance estimate, and their spread tells you how stable it is.
Choosing k — and the variants that matter
- k = 5 or 10 is the practical default: a good balance of reliability and compute cost.
- Stratified k-fold keeps the class ratio identical in every fold — essential for imbalanced classification, so no fold ends up with barely any positives.
- Leave-one-out (k = n) trains n models, each missing a single point — nearly unbiased but expensive and often high-variance; mainly for very small datasets.
- Time-series split — for temporal data you must always train on the past and test on the future. Ordinary shuffled k-fold lets the model peek ahead, which inflates scores.
The leakage mistakes that inflate your scores
Cross-validation is only honest if each fold's test data stays truly unseen. The classic mistake is preprocessing before splitting: if you fit a scaler, imputer or feature selector on the whole dataset, information from the test folds leaks into training, and your scores drift optimistically. The fix is to put every fitted step inside the cross-validation loop (a pipeline), so it's re-fit on each training fold only — the same fit-on-train-only rule we cover in feature scaling. The second classic mistake: tuning hyperparameters on the same folds you report results from. Use nested cross-validation, or keep a final untouched test set for the last word — the same discipline behind hyperparameter tuning.
What cross-validation is actually estimating
CV estimates how your modelling procedure generalizes to unseen data from the same distribution — it's a model-selection and estimation tool. Once you've chosen the winner, it's standard to retrain it on all available training data before deployment.
Cross-validation is also your first line of defence for diagnosing overfitting vs underfitting: a large gap between training scores and CV scores is the overfitting signature. For deeper drills, see the cross-validation and data splits topic page. And **AI Learning** lets you practice all of it — evaluation, splits, leakage traps — through ~10,000 offline questions with explanations, no account needed.
Free · Works offline · Master evaluation and validation with spaced practice questions.
Sources
- scikit-learn documentation — Cross-validation: evaluating estimator performance
- Kohavi (1995) — 'A Study of Cross-Validation and Bootstrap for Accuracy Estimation and Model Selection', IJCAI
- Hastie, Tibshirani & Friedman — The Elements of Statistical Learning, ch. 7
Frequently asked questions
More in Learn AI & Data Science
- Coding Interview Strategy: A Framework for Solving Problems Under PressureKnowing algorithms isn't enough — you have to deploy them under pressure. Here's a repeatable framework for solving unseen problems and a study plan that builds durable skill.
- P vs NP and Complexity Classes: What 'Hard' Really Means (Intuition, No Proofs)Why do some problems have fast algorithms and others resist every attempt? P vs NP is the deepest open question in computer science — here's the intuition, minus the proofs.
- Union-Find (Disjoint Set Union): Near-Constant Connectivity with Two OptimizationsUnion-Find answers 'are these two things connected?' and 'connect them' in almost O(1) — thanks to two beautifully simple optimizations that make the trees nearly flat.