Topic 4 of 6 · 5 min read
Time-aware and group-aware splits
Goal: Choose a random, time-based or group-based split for a dataset and explain what a random split would hide.
Split the data the way the model will be used: the test set should be as far from the training set as real future data will be from what you trained on. A random split is correct only when rows are independent and the future looks like the past.
Check yourself · 1 of 3
A model will forecast next month's sales. Which split fits?
Why it matters
A random split puts neighbouring rows on both sides. For a time series, the model sees Tuesday and Thursday in training and is tested on Wednesday, which is interpolation, not forecasting. For grouped data, such as many visits per patient or many photos per user, the model recognises the person instead of learning the condition. Both give a score that production will not reproduce.
Choosing a split
- Ask how the model will be used. Predict next month from last month: split by time. Score new customers: split by customer. Score new documents from known authors: a random split may be fine.
- Time-based split. Train on everything before a cutoff, validate on the period after it. For cross-validation, use expanding windows such as
TimeSeriesSplit, where each test fold follows its training folds. - Group-based split. Keep every row of one entity (user, patient, device, document source) on the same side. In scikit-learn,
GroupKFoldandGroupShuffleSplittake agroupsarray. - Keep a final test set that you touch once, separate from the validation data used to choose settings.
- Add a gap when a feature looks back over a window. If you use a 30-day average, leave 30 days between train and test.
- Stratify by label when classes are rare, so each split contains them. Stratification does not fix time or group problems.
Before and after
Weak plan: "Predict weekly demand per store. Shuffle all rows and take 80 percent for training." Test weeks sit between training weeks, and a promotion in week 20 appears on both sides.
Fixed plan: train on weeks 1 to 40, validate on 41 to 46, keep weeks 47 to 52 as the final test. Features that use the last 4 weeks are computed using only data before each row. The reported error is higher and closer to what the planners will see.
from sklearn.model_selection import GroupKFold
cv = GroupKFold(n_splits=5)
for train_idx, test_idx in cv.split(X, y, groups=df["patient_id"]):
...
What goes wrong
- Shuffling time series. The model peeks at the future.
- Leaking the entity. Two scans of one patient sit in train and test.
- Reusing the test set. After many rounds of tuning, the test score is no longer an honest estimate.
Examples
A random split hides patient identity
A model detects pneumonia from chest scans, with about six scans per patient. A random split scores well because scans of the same patient resemble each other. A split by patient_id shows the real, lower score for new patients.
Time split for churn
A telecom predicts churn in the next 30 days. The plan trains on customers' history up to 1 March, validates on March, and tests on April. Each feature is computed using data before the row's date, so the test mirrors a monthly production run.
Key points
- Choose the split by asking how the model will be used, not by habit.
- Use a time-based split when the model predicts the future; every test row should come after training rows.
- Use a group-based split when rows share an entity such as a user or patient, so no entity sits on both sides.
- Leave a gap equal to the look-back window of your features.
- Keep a final test set separate from the validation data used for tuning, and use it once.
Common mistakes
- Using a random shuffle on time-ordered data because it is the default.
- Putting rows of the same user on both sides of the split.
- Tuning against the test set until its score stops being an unbiased estimate.
- Stratifying by label and assuming that also fixes time or group problems.
Check yourself · 2 of 3
Each patient has several scans in a dataset. Why use a group-based split?
Check yourself · 3 of 3
Features use a 14-day rolling average. What helps keep the time-based split honest?
Practice items, not counted towards your mastery.
Start here
- scikit-learn: common pitfalls and recommended practices (opens in a new tab)ArticleFree
Data leakage, preprocessing inside pipelines and inconsistent preprocessing, with working code.
- Cross-validation: evaluating estimator performance (scikit-learn) (opens in a new tab)DocsFree
Group-aware and time-series splitters, and why each exists.
- Datasheets for Datasets (opens in a new tab)PaperFree
The question list for documenting where a dataset came from, how it was labelled and what it should not be used for.
More for this topic
- scikit-learn: TimeSeriesSplit (opens in a new tab)DocsFree
A splitter where every test fold comes after its training folds.