Grouped splits: what a random test set hides in geographic data
A random train/test split quietly inflates scores when rows share a location. Grouping by region changes what the number actually measures.
The split in Bhoomi Drishti is grouped by region: 16,880 rows for training, 3,560 for validation, 3,560 for test, and not one of the 74 regions appears in more than a single split. I did not start there. I started with a random shuffle, the way most tutorials do, and the first thing grouping did was lower my score. That was the correct outcome and the reason to keep it.
Why a random split flatters you here
Every row in the dataset is a region at a moment in time: 16 features covering rainfall, slope, soil and past incidence for one of 74 regions across 20 Indian states, labelled with whether a failure occurred inside the forecast horizon. Spread 24,000 rows over 74 regions and each region contributes a few hundred rows. Shuffle them and cut 70/15/15, and every region has rows in all three sets.
Now consider what the model can do with that. Slope, soil class, lithology, elevation and historical incidence barely move for a given region across its rows, so the region is effectively identifiable from its feature vector. Base rates also differ sharply between regions: some places fail repeatedly across the record, others almost never do. A tree ensemble does not have to learn anything about slope stability to score well on that arrangement. It can learn that a particular pattern of static features means region 41, that region 41 has a high positive rate, and then apply the memorised rate to region 41 rows in the test set, rows drawn from a place it has already studied.
This is not overfitting in the usual sense, and regularisation does not fix it. The pattern the model exploits is real inside the training distribution. It is leakage: information about test labels reaching the model along a path that will not exist when the system is asked for a forecast. The resulting score is not arithmetically wrong either. It answers a different question. A random split asks how well the model interpolates within regions it already has records for. A grouped split asks how well it generalises to a hillside it has never seen. For an early-warning platform, only the second question is worth asking, because the districts most in need of a warning are the ones with the thinnest history.
The routine itself
Grouping is not much code. Assign whole regions to splits rather than individual rows, and since regions differ in size, walk them largest first and drop each one into whichever split is furthest behind its quota. That keeps the proportions close to target without any randomness, so the same regions land in the same split on every run.
import numpy as np
def grouped_split(groups, fractions=(0.70, 0.15, 0.15)):
"""Assign whole groups to splits, so no group lands in two of them.
Deterministic: same input, same split, every run."""
names, counts = np.unique(groups, return_counts=True)
quota = np.array(fractions) * counts.sum()
filled = np.zeros(len(fractions))
assigned = {}
for g in np.argsort(-counts): # largest groups first
k = int(np.argmax(quota - filled)) # split furthest behind quota
assigned[names[g]] = k
filled[k] += counts[g]
split_of = np.array([assigned[g] for g in groups])
return [np.flatnonzero(split_of == k) for k in range(len(fractions))]
train, val, test = grouped_split(regions)
assert not set(regions[train]) & set(regions[test]) # the invariantThe assert on the last line is the part I would keep if I threw out everything else. Every leakage bug I have written stayed invisible until I wrote the invariant down and checked it in code.
What the numbers did
Grouping produced the 16,880 / 3,560 / 3,560 arrangement, and every result I report comes off that grouped test set: ROC AUC 0.8254, PR AUC 0.5245, Brier 0.0681. All of them are lower than what the random shuffle handed me. I am not going to quote the shuffled figure, because quoting it gives it a status it has not earned, and the smaller number is the one I trust. When the system is pointed at a village with no incident history, the grouped score describes what will actually happen. The random score describes a situation that never occurs.
The trigger for grouping is not geography as such. It is any structure that makes rows non-independent in a way that survives into deployment.
- Rows share a place: regions, sites, hillsides, stores, grid cells.
- Rows share a subject: patients, users, devices, accounts.
- Rows share a document or session: sentences from one article, events in one visit.
- Rows share a clock: any series where a later row can inform an earlier prediction.
The same mistake wearing a different hat
Leaking a region and leaking time are one error with two names. In a time series, shuffling rows lets the model see Thursday while it predicts Wednesday, and the fix is to cut the split at a timestamp instead of at random. In spatial data, shuffling lets the model see one hour from a hillside while it predicts another hour from that same hillside, and the fix is to cut on the region. Identical shape in both cases: the rows were not independent, the split assumed they were, and the score inherited the assumption without ever complaining.
A leaderboard number that went up when you tightened nothing is a number that answered an easier question.
Both axes are live in this dataset, since a row is a place and a time together, with horizons at 0, 6, 12, 24, 48 and 72 hours. Region grouping is the constraint I enforce, because the deliverable has to say something defensible about places with no record. If I pushed this toward an operational system, the next thing I would add is a temporal holdout stacked on top of the grouping: fit on earlier monsoon seasons, evaluate on a later one, still grouped by region. Blocked along both axes is stricter again, and I would expect the score to drop a third time.
The habit is small and it transfers. Before trusting any split, name the unit the model is supposed to generalise across, then assert that no unit sits on both sides of the line. If you cannot name the unit, you do not yet know what your score means.