Cross Validation

Cross-Validation is a model evaluation technique that provides a more reliable estimate of out-of-sample performance than a single train/test split — by systematically rotating which portion of the data serves as the test set and averaging the results across all rotations, eliminating the "lucky split" problem where a single random 80/20 split might accidentally give an optimistic or pessimistic estimate of model quality.

What Is Cross-Validation?

  • Definition: A resampling procedure that splits the data into K equal parts (folds), trains the model K times — each time holding out a different fold as the test set and training on the remaining K-1 folds — then averages the K test scores to produce a single robust performance estimate.
  • The Problem: A single train/test split is unreliable. If your 20% test set happens to contain mostly easy examples, accuracy looks artificially high. If it contains edge cases, accuracy looks artificially low. Cross-validation averages over K different test sets.
  • The Solution: Every data point gets exactly one turn as a test example — providing a performance estimate that uses ALL the data for both training and testing (just never at the same time).

How K-Fold Cross-Validation Works

RoundTraining FoldsTest FoldScore
1Folds 2, 3, 4, 5Fold 185%
2Folds 1, 3, 4, 5Fold 283%
3Folds 1, 2, 4, 5Fold 387%
4Folds 1, 2, 3, 5Fold 484%
5Folds 1, 2, 3, 4Fold 586%
Average85.0% ± 1.4%

Cross-Validation Variants

VariantKUse CaseTrade-off
5-Fold5Standard defaultGood balance of bias and variance
10-Fold10More stable estimate2× slower than 5-fold
Leave-One-Out (LOO)NVery small datasets (<100)N training runs — expensive
Stratified K-FoldAnyImbalanced classesPreserves class proportions in each fold
Group K-FoldAnyGrouped data (patients, users)Prevents data leakage from same group in train/test
Time Series SplitAnyTemporal dataTrain on past, test on future (no future leakage)
Nested CVOuter + InnerHyperparameter tuning + evaluationUnbiased estimate when tuning

Common Mistakes

MistakeProblemFix
Feature scaling before splitTest data leaks into scaling parametersScale inside each fold (use Pipeline)
Feature selection before CVSelected features are biased by test dataSelect features inside each fold
Not using stratified for classificationA fold might have 0% of a minority classUse StratifiedKFold
Ignoring group structureSame patient in train and test → data leakageUse GroupKFold

Python Implementation

from sklearn.model_selection import cross_val_score
from sklearn.ensemble import RandomForestClassifier

scores = cross_val_score(
    RandomForestClassifier(), X, y,
    cv=5, scoring='accuracy'
)
print(f"Accuracy: {scores.mean():.3f} ± {scores.std():.3f}")

Cross-Validation is the standard method for honest model evaluation in machine learning — providing a robust performance estimate that every data scientist uses before reporting results, preventing the self-deception of lucky (or unlucky) train/test splits, and serving as the foundation for proper hyperparameter tuning and model comparison.

Take cross validation further

Ask the copilot about this term, or have our engineers assess it against your process.