What is cross-validation and which KFold variant should you use?
Why one train/test split lies, the 5-fold loop, StratifiedKFold vs GroupKFold vs TimeSeriesSplit, and the leakage rule Pipeline solves for free: cross-validation on one page.
Get the free PDF
One page, print-ready, free to share. No signup needed.
It scored 94% once. Your accuracy was one lucky split, and cross-validation is how you find out what the model actually does. One page. The print-ready A4 PDF is at the bottom.
Why
- One split: one lucky draw.
- k folds: k honest scores.
- Report mean ± std, not the best fold.
The honest five lines
from sklearn.pipeline import make_pipeline
pipe = make_pipeline(StandardScaler(),
LogisticRegression())
scores = cross_val_score(pipe, X, y,
cv=StratifiedKFold(5, shuffle=True))
print(scores.mean(), scores.std())
Scaling lives inside the pipeline, so every fold learns preprocessing from its own train side only. Scale before the split instead, and the test rows have already whispered their mean to the model.
Variants
StratifiedKFold: keeps the class ratio in every fold.GroupKFold: the same user never lands on both sides.TimeSeriesSplit: for anything dated.
Leakage
- Scale inside the folds, never before the split.
- Impute inside the folds too, same rule.
Pipeline()does it right for free.
Numbers
- k = 5: the default.
- k = 10: small datasets.
- Leave-one-out: tiny data only.
The trap: the wrong splitter
| Your data | Plain KFold does | Use |
|---|---|---|
| 95/5 class imbalance | folds with zero positives | StratifiedKFold |
| many rows per user | same user on both sides | GroupKFold |
| timestamps | trains on the future | TimeSeriesSplit |
The GroupKFold one is the interview favorite: leaking a user across the split inflates scores and nobody notices until production.
Frequently asked questions
Why is one train/test split not enough?
When should you use StratifiedKFold, GroupKFold or TimeSeriesSplit?
How does cross-validation cause data leakage and how do you avoid it?
How many folds should you use?
Get the free PDF
One page, print-ready, free to share. No signup needed.