Home Knowledge Base 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?

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.

cross validationfoldevaluate

Explore 500+ Semiconductor & AI Topics

From EUV lithography to CUDA optimization — search the full knowledge base or chat with our AI assistant.