How to Implement Resampling Methods From Scratch In Python
Estimating performance on data you do not have
A model's score on its training data is not a performance estimate — it is a memory test. Resampling methods carve your dataset into pieces so that every score is computed on rows the model has never seen. Which method you choose is a trade-off between bias, variance and compute.
1. Train/test split
Split once, typically 60/40 or 80/20. Cheap and unbiased, but high variance: with a small dataset, a different random split can move your score by several points.
2. k-fold cross-validation
Split into k folds; each fold takes a turn as the test set. You get k scores, so you can report a mean and a spread. k = 5 or k = 10 are the standard choices — large enough to keep the training sets representative, small enough to run.
3. Stratified folds
On classification problems, keep the class ratio the same in every fold. Without stratification a rare class can vanish entirely from a training fold, and that fold's score becomes meaningless.
4. Repeated k-fold and the bootstrap
Repeated k-fold runs the whole procedure several times with different shuffles and averages the results — the cheapest way to reduce the variance of your estimate. The bootstrap samples with replacement, leaving roughly 36.8% of rows out of each sample to serve as the test set, and gives you a genuine confidence interval.
Which method, when
- Thousands of rows or more, expensive model: single train/test split, plus a held-out validation set.
- Hundreds to low thousands: 10-fold cross-validation, stratified for classification.
- Very small datasets: repeated stratified k-fold, or leave-one-out if you can afford it.
- Time series: never shuffle. Use forward-chaining splits where the test window is always after the training window.
- Grouped data (multiple rows per customer): split by group, or the same customer appears on both sides.