Split the way it will be used: groups and time
Random folds assume independent rows. Repeat customers and time series break that, and the score looks better than the model is. Match the split to deployment.
What runs in CodeSplash
- XGBoost Verified in Python and R
- Extra trees Verified in Python and R
You will be able to
- Pick grouped or time-ordered folds from how the model will be used
- Explain why random folds flatter models on dependent rows
- Read the Fold scheme check in the Robustness tab
Read first
One question decides
ReadAsk: what will be new when this model is used?
| In use, the model meets… | So hold out… | Set |
|---|---|---|
| New rows from the same mix | Random rows | Stratified or k-fold |
| New customers, patients, stores | Whole entities | Groups |
| Next week, next quarter | The future | Time order |
Random folds answer the first question even when you're asking the second or third.
Why random folds flatter
ExploreWith Group k-fold, a customer's rows all stay on one side. Read The shortcut (plain random folds) against True new-data error and This scheme.
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.
Switch Scheme to Forward chaining (time series). Yesterday's row sits in training, and today's sits in validation, and they're nearly the same row. The model looks prescient because it's reading its neighbor's answer.
Which split?
PredictPredict first
You have 40,000 scans from 900 patients, several per patient. The model will be used on scans from patients it has never seen. Which split matches?
Reveal
Grouped by patient. Scans of one patient look alike, so a random split lets the model recognize the person. Time order would matter too if scanners or practice change over the years, and then you'd want the later patients held out.
Grouped data, measured
ReadChick weights have 12 repeated weighings on average per chick. With Groups set to the chick, whole birds stay on one side, and the locked test rows are whole birds too.
XGBoost reaches an RMSE of 40.7 on birds it hasn't seen, across 432 training rows and 146 test rows. Extra trees gets 38.8 on its own grouped plan.
Those scores are honest for the question "a new chick". A random split would answer another one: a new weighing of a chick already seen.
Time has a price
ReadTime-ordered folds validate only on later rows, and a gap drops rows between training and validation when features use lags.
Ozone, predicted from wind and temperature with a gap of 3 days: mean absolute error is 25.1 across folds, yet 7.66 on the locked test rows. The test set is the last readings of the series, all from September, when ozone runs much lower than in midsummer and errors are small.
A time-ordered score describes one period. Report which, and don't treat it as an average over all seasons.
Check your understanding
CheckpointCheck yourself
You're predicting next month's demand per store for stores you already have. What should the split respect?
Show the answer
Answer: a
The model will meet known stores in future months, so hold out the future. Grouping by store would answer a different question: how it does on a store it has never seen.
Check yourself
A feature is the average of the last 7 days. What should you set Gap between train and validation to, at least?
Show the answer
Answer: c
Without a gap, the first validation rows' features are built from days also in training. The app's help text states the same rule.
Check yourself
With random folds AUC is 0.91. Grouped by customer, it's 0.71. Which should you report for predicting new customers?
Show the answer
Answer: b
The gap measures how much the model leaned on recognizing customers. A large gap is the alarm the Fold scheme check raises.
Set the plan
In the appIn Setup, set Groups (optional) or Time order (optional), and Automatic in Validation follows. To compare after training, open the run's Robustness tab and set Group folds by or Order folds by time. The Fold scheme table shows Random folds better by, in red when the gap raises an alarm.
import numpy as np
from sklearn.model_selection import GroupKFold, TimeSeriesSplit
rows = np.arange(24).reshape(-1, 1)
customer = np.repeat(np.arange(6), 4)
for train, valid in GroupKFold(3).split(rows, groups=customer):
print("held out customers", sorted({int(c) for c in customer[valid]}))
for train, valid in TimeSeriesSplit(n_splits=3, gap=2).split(rows):
print("train up to", train.max(), "validate", valid.min(), "to", valid.max())customer <- rep(1:6, each = 4)
set.seed(1)
groups <- sample(unique(customer))
fold_of <- setNames(rep(1:3, length.out = length(groups)), groups)
for (k in 1:3) cat("held out customers", sort(groups[fold_of == k]), "\n")
n <- 24; splits <- 3; gap <- 2
size <- floor(n / (splits + 1))
for (f in 1:splits) {
start <- f * size + 1
cat("train up to", start - gap - 1, "validate", start, "to", start + size - 1, "\n")
}Recap
Recap- Ask what will be new in use, and hold that out.
- Random folds on dependent rows flatter the model, because neighbors share answers.
- Use a gap for lagged features, and report which period a time-ordered score describes.
Concepts in this lesson