Skip to content

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.

Applied ~12 min8 steps

What runs in CodeSplash

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

01

One question decides

Read

Ask: what will be new when this model is used?

In use, the model meets…So hold out…Set
New rows from the same mixRandom rowsStratified or k-fold
New customers, patients, storesWhole entitiesGroups
Next week, next quarterThe futureTime order

Random folds answer the first question even when you're asking the second or third.

02

Why random folds flatter

Explore

With 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?

How sure?

Scheme

5
40
Simulating…

Commit your prediction above to see the result

Make your prediction first; the figure appears when you commit.

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.

03

Which split?

Predict

Predict 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?

Random stratified foldsGrouped by patientTime-ordered
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.

04

Grouped data, measured

Read

Chick 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.

05

Time has a price

Read

Time-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.

06

Check your understanding

Checkpoint

Check 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.

07

Set the plan

In the app

In 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.

Python
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())
R
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")
}
08

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