How noisy is a score? Stratified and repeated folds
With a rare class, an unlucky fold can have no positives at all. Stratify the folds, repeat them when rows are few, and compare the seed's noise with the data's.
What runs in CodeSplash
- Naive Bayes (Gaussian) Verified in Python and R
- LightGBM Verified in Python and R
You will be able to
- Explain what stratified folds protect against
- Say which noise repeats remove and which they can't
- Read seed stability in the Robustness tab
Read first
An unlucky fold
ReadSuppose 5% of 200 rows are positive, so 10 rows, and you cut 5 folds at random. Each fold should hold 2 positives, but a random fold can easily hold none.
On a fold with no positives, ROC AUC is undefined and recall means nothing. On a fold with five, the score swings the other way.
Stratified foldsStratified foldsFolds that keep each class's share about the same as in the full training set. keep each class's share about the same in every fold. Automatic uses them for classes when you haven't set groups or time.
How often is a fold empty?
PredictPredict first
10 positives among 200 rows, folds of 40 chosen at random. What's the chance that one particular fold has no positives?
Reveal
About 10%. Each of the 10 positives lands outside the fold with probability , so roughly . With 5 folds to go wrong, an empty fold is a common event on small data.
Watch the shares
ExplorePress through k-fold, then Stratified k-fold (rare class), and open the table beside the chart. Compare Rare-class share per fold under each scheme, and how much the estimate swings across datasets.
DExplorable · Cross-validation folds
Which rows should be held out so the score predicts real performance?
Each scheme has a tempting shortcut: training error, plain random folds, or the best score from a search. Will the shortcut here look better than the true error on new data?
Scheme
Commit your prediction above to see the result
Make your prediction to see the result.
Repeats fix one kind of noise
ReadReshuffle the folds and you get a different score. Repeats averages over reshuffles, which removes the noise from how you happened to cut the rows.
It can't remove the noise from which rows you happened to collect. Two repeats on small data aren't twice the evidence, and the fold scores stay correlated.
For example, naive Bayes on Pima with 3 folds repeated twice has a cross-validated AUC of 0.844. LightGBM on a rarer version of the class, also with 3 folds repeated twice, reads 0.649 in balanced accuracy.
Which should you use?
PredictPredict first
Two models differ by a small amount, and the leaderboard calls it noise. You can spend extra effort on 10 repeats of the folds or on collecting twice the rows. Which reduces more of the uncertainty about real-world performance?
Reveal
More rows. Repeats make the estimate steadier for this dataset. Only new rows reduce the uncertainty that comes from having sampled these rows and not others. Do repeat when rows are few and fits are cheap, because reshuffling is free. But don't read the steadier number as new evidence.
Seeds are noise too
ReadA random forest's score moves a little with its seed. So do the folds. If swapping the seed moves your score as much as the gap between two models, you can't choose between them on that gap.
The Robustness tab's Seed stability section re-fits with other seeds and reports the spread beside the fold-to-fold SD, and whether the importance ranking keeps its order.
Check your understanding
CheckpointCheck yourself
What does stratification guarantee?
Show the answer
Answer: a
It makes the folds comparable, and it changes neither the model nor the class balance it trains on. Keeping a customer's rows together is what groups do.
Check yourself
You repeat 5-fold cross-validation ten times and get 50 scores. Which reading is right?
Show the answer
Answer: b
They all reuse the same rows. This is why model comparisons use the corrected resampled t test and not a plain one.
Check yourself
You re-run a model with five seeds. The cross-validated AUC ranges over a band as wide as the fold-to-fold SD. What should you do?
Show the answer
Answer: b
Picking the best seed is selection on noise. A Seed ensemble in the Diagnostics tab averages the seeds and gives steadier predictions.
Try it
In the appIn Validation, set Resampling to Stratified k-fold and raise Repeats. The table under Split plan lists Validation classes per fold, so you can see what each one will test on. Then open a finished run's Robustness tab and run Seed stability.
import pandas as pd
from sklearn.model_selection import KFold, StratifiedKFold
df = pd.read_csv("pima_rare.csv")
y = df["diabetic"].to_numpy()
for name, cv in [("random", KFold(5, shuffle=True, random_state=2)),
("stratified", StratifiedKFold(5, shuffle=True, random_state=2))]:
print(name, [int(y[valid].sum()) for _, valid in cv.split(df, y)])df <- read.csv("pima_rare.csv")
y <- df$diabetic == "true" | df$diabetic == "TRUE"
set.seed(2)
random <- sample(rep(1:5, length.out = length(y)))
strat <- integer(length(y))
for (cl in c(TRUE, FALSE)) {
i <- which(y == cl)
strat[i] <- sample(rep(1:5, length.out = length(i)))
}
rbind(random = tapply(y, random, sum), stratified = tapply(y, strat, sum))Recap
Recap- Stratified folds keep class shares steady, so no fold is a lottery.
- Repeats remove the noise of the cut, not the noise of the sample.
- If a seed moves the score as much as a model swap does, don't choose on that gap.
Concepts in this lesson