Random and stratified folds
Quick answer
Cross-validation (CV) rotates validation subsets through development data to estimate how a training procedure performs on unseen rows. K-fold splits divide rows into k disjoint validation folds; stratified folds additionally aim to preserve class proportions. Reserve a separate final test set before any model selection. Static examples tested locally with Python 3.13.5, scikit-learn 1.8.0 SciPy 1.17.0 and NumPy 2.3.5 where imported. ByteMind package execution is unverified.
Inspect validation membership
from sklearn.model_selection import KFoldfor tr,va in KFold(n_splits=3).split(list(range(7))): print(va.tolist(), len(tr))Observed local output:
[0, 1, 2] 4[3, 4] 5[5, 6] 5Unequal fold sizes still cover every row exactly once.
How it works
For each fold, create a fresh estimator, fit on the other k-1 folds, and score on the held-out fold. The mean of fold scores summarizes performance at that training size. For an additive metric such as accuracy, weighting by validation size is equivalent to pooling all out-of-fold correct predictions; unweighted means give each fold equal influence. Neither summary guarantees future performance. KFold uses contiguous index blocks by default; enable shuffle with a fixed seed when exchangeable rows arrive in a meaningful order that should not define folds. StratifiedKFold uses target classes to distribute rare examples approximately evenly; it does not make dependent observations independent. Check that the minority class has enough examples for the chosen number of folds and metric. Fit every learned transformation inside the fold, preferably using a Pipeline. A single fixed holdout is cheaper but sensitive to one partition; k-fold uses more fits and each row is evaluated once per run. Use explicit scoring and record split IDs, seed and preprocessing. For patient, customer or time-dependent rows, use a group or chronological split instead.
Compare class distributions
from sklearn.model_selection import StratifiedKFoldy=[0]*9+[1]*3for tr,va in StratifiedKFold(n_splits=3,shuffle=True,random_state=7).split(y,y): print(len(va),sum(y[i] for i in va))Observed local output:
4 14 14 1Each validation fold contains one positive, without oversampling.
Common mistakes
Do not fit a scaler before CV, treat a shuffled split as evidence of independence, or use the final test to choose k. Stratification is not class rebalancing and cannot create missing minority examples.
Practice and next steps
For 12 independent rows with three positives, predict positive counts in three stratified validation folds. Explain why the same approach would be invalid if all positives came from one patient. Complete the knowledge check and staged exercise. Next: Group splits.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Random and stratified folds — knowledge check
Apply random and stratified folds to new scenarios.
What does one ordinary K-fold run validate?
Why stratify classification folds?
Where should a scaler be fitted?
What if fold sizes differ?
When is random row CV appropriate?