Stratified k-fold cross-validation is a resampling procedure that divides a labeled dataset into k folds of roughly equal size while keeping the class proportions of every fold close to those of the full dataset. In model evaluation, it is the standard way to estimate how a classifier will generalize when classes are unevenly represented, because each training and validation split reflects the same label distribution.
The method is a refinement of ordinary k-fold cross-validation. The model is trained k times, each time on k minus one folds, and scored on the remaining fold; the k scores are then averaged. Stratification does not change this loop. It only changes how rows are assigned to folds, which reduces the variance of the estimate and prevents folds that contain few or no examples of a rare class.
How stratified folds are built
Stratified fold assignment works class by class. The rows of each class are shuffled, if shuffling is enabled, and then dealt out across the k folds in turn, so every fold receives about one k-th of each class. The folds are then combined into training and validation splits exactly as in plain k-fold.
The table below traces the assignment for a binary dataset of 1,000 rows with a 10% positive rate and k set to 5.
| Fold | Rows | Positive rows | Negative rows |
|---|---|---|---|
| Fold 1 | 200 | 20 | 180 |
| Fold 2 | 200 | 20 | 180 |
| Fold 3 | 200 | 20 | 180 |
| Fold 4 | 200 | 20 | 180 |
| Fold 5 | 200 | 20 | 180 |
With random, unstratified splitting, the number of positives per fold would fluctuate, typically by several rows in either direction. On small or highly imbalanced datasets that fluctuation can leave a fold with almost no minority examples, making recall, precision and the F1 score for that fold unreliable or undefined.
Choosing k and other settings
The value of k controls a bias and variance trade-off in the performance estimate. Larger k trains on more data per round, which lowers pessimistic bias, but it increases computation and makes the fold scores more correlated with one another.
| Setting | Common values | What it affects |
|---|---|---|
| Number of folds (k) | 5 or 10 | Training size per round, run time, estimate variance |
| Shuffling | Enabled with a fixed seed | Removes ordering effects; seed makes runs reproducible |
| Repetitions | 1 to 10 repeats | Averaging repeats lowers variance of the final score |
| Minimum class count | At least k rows per class | Below this, some folds lack a class entirely |
| Scoring metric | F1, balanced accuracy, AUC | Should match the cost of errors in the task |
A practical rule is that every class needs at least k members; common libraries warn or fail when a class is smaller than the number of folds. For very rare classes, reducing k or using repeated stratified k-fold with a smaller k gives more stable results than forcing ten folds.
Comparison with other validation schemes
Stratified k-fold is one member of a family of resampling methods, and the correct choice depends on the structure of the data rather than on habit. The comparison below covers its closest relatives.
| Scheme | How splits are formed | Best suited for |
|---|---|---|
| Plain k-fold | Random contiguous or shuffled blocks | Regression and balanced classification |
| Stratified k-fold | Folds preserve class proportions | Imbalanced or small classification datasets |
| Group k-fold | All rows of one group stay in one fold | Repeated measurements of patients, users or devices |
| Stratified group k-fold | Keeps groups intact and balances classes approximately | Grouped data with imbalanced labels |
| Leave-one-out | Each single row is a validation fold | Very small datasets; high variance |
| Time series split | Training always precedes validation in time | Forecasting and any temporally ordered data |
| Holdout split | One fixed train and test partition | Very large datasets or quick checks |
Stratification is not a substitute for grouping or temporal ordering. If the same customer appears in both training and validation folds, the estimate is optimistic regardless of how balanced the classes are, because the model partly recognizes individuals rather than learning general patterns.
Using it correctly in a modeling workflow
The most important rule is that every data-dependent step must happen inside the folds. Scaling, feature selection, target encoding and oversampling such as SMOTE must be fitted on the training part of each split only, typically by wrapping them in a pipeline that the cross-validation loop refits each round.
- Keep a separate final test set untouched for the last, unbiased evaluation.
- Use nested cross-validation when hyperparameters are tuned, so the tuning does not inflate the score.
- Report the mean and standard deviation of fold scores, not only the mean.
- For regression, stratify on binned target values when the target distribution is skewed.
- For multilabel problems, use iterative stratification, which balances many labels at once.
The fold scores also carry diagnostic value. A wide spread between folds suggests that the dataset is small, that a few unusual rows dominate some folds, or that the model is unstable, all of which deserve investigation before deployment.
Common mistakes and their consequences
Most problems with stratified cross-validation come from data leakage or from applying it to data whose structure it ignores. The table lists the typical errors.
| Mistake | Consequence | Remedy |
|---|---|---|
| Oversampling before splitting | Synthetic copies leak into validation folds | Resample inside each training fold |
| Ignoring groups | Overly optimistic scores | Use stratified group k-fold |
| Shuffling time series | Future information trains the model | Use a time-ordered split |
| Using accuracy on rare classes | High score from predicting the majority | Use F1, recall or balanced accuracy |
| Tuning and reporting on the same folds | Inflated final estimate | Apply nested cross-validation |
In simple terms
To test a model fairly, data is split into several equal parts. The model learns from most parts and is tested on the one left out. This repeats until every part has been the test once.
Stratified means each part gets a fair share of every category. Imagine dealing a deck of cards so that each player gets the same mix of suits. If one category is rare, every test still includes some of it. That makes the final score more trustworthy.
A practical example
A hospital analytics team builds a model to flag patients at risk of readmission within 30 days. Its dataset has 4,000 discharge records, of which only 8% are readmissions, about 320 cases. With plain five-fold splitting, one fold in a trial run contained just 49 positives and another 78, and recall swung between 0.52 and 0.71.
The team switches to stratified five-fold cross-validation, repeated three times with different seeds, and keeps each patient’s records in a single fold. Every fold now holds about 64 readmissions. Recall settles at 0.63 with a standard deviation of 0.03, a figure the clinical committee can rely on when deciding whether the model is ready for a pilot.