← All posts

Why Random Train-Test Splits Flatter Flood Models

The usual way to test a machine-learning model is to shuffle the data, keep a fifth aside and score on it. For a fast flood model trained on gridded simulator runs, that habit measures the wrong thing. Neighbouring cells carry almost the same depth, and the same terrain appears under every storm, so a shuffled test set is mostly made of cells the model has already seen in all but name. The score it gives is real, but it answers a question nobody asked.

This post shows how much the choice of held-out data moves a flood model's score, why, and how to choose the split that matches the claim you want to make. It also covers a quieter problem: training sets that fill up with runs where nothing happens, which flatter some metrics and not others. The numbers come from a small synthetic lab; the habits come from building fast flood models, where holding out whole areas changed the picture.

This is the second post in a series on checking flood models. The first, on verification and validation, explains why a score against the simulator is not a score against reality. Everything here is about making the score against the simulator honest.

In short

  • In the lab, averaged over ten seeds, a nearest-neighbour model scores 0.751 with random cells held out and 0.611 with whole tiles of terrain held out; boosted trees score 0.693 and 0.591. Random cells beat whole tiles in all ten seeds, for both models. Only the tile split estimates how a model does on ground it has not seen.
  • Holding out one storm of four is a single draw: the nearest-neighbour score runs from 0.49 to 0.86 depending mostly on which storm is left out.
  • A feature that names the place, such as position, raises the random-split score and lowers the score on new terrain.
  • Neighbouring cells are nearly copies of each other, so a random split's test set has far fewer independent cases than it has cells.
  • Runs where almost nothing floods lift accuracy from 0.77 to 0.87 and leave the critical success index roughly where it was. Check the corpus before training.

Why cells leak

A flood depth map is smooth. Water at one cell is at almost the same level as water at the next, and the depth differs only by the change in ground height. Hold out a random quarter of the cells and almost every test cell has its neighbours in the training set:

A 20 by 20 grid of one lab run shaded by depth, with about a quarter of the cells outlined as held out. Text beside it gives the correlation between a wet cell's depth and its neighbour's One lab run: depth, with a random quarter of the cells held out (outlined) A held-out cell's neighbours are almost always in training. Correlation of a wet cell's depth with its neighbour's, over 7,112 pairs: r = 0.951. A model that copies its neighbours scores well on cells like these without learning how water moves over terrain it has not seen. Shading: depth; grey: dry.
Figure 1. A model can score well on such cells by interpolating between neighbours, which is not the skill a new place needs.

How much information does such a test set really hold? For a series whose neighbours are correlated with coefficient ρ, the effective number of independent values among n is roughly, by a rule borrowed from time-series practice (Bretherton et al. 1999),

neff ≈ n (1 − ρ) / (1 + ρ)

In the lab, ρ for neighbouring wet cells is 0.951, which by this formula makes a 400-cell tile worth about 10 independent values. Read that as an order of magnitude, not a count: the formula assumes the correlation shrinks by the same factor at every step, and the lab's falls faster, as the next figure shows. Strictly, a test score depends on how correlated the model's errors are, not the depths; the depths are used here because they need no model. Real terrain grids are finer than the lab's, which makes neighbours more alike, not less.

The resemblance fades with distance. In the lab, the correlation falls by half at about four cells and is close to zero by seven:

Correlation of wet-cell depth with the cell k steps away, falling from 0.96 at one step to 0.54 at four, 0.06 at seven and slightly negative beyond Lab: correlation of wet-cell depth with the cell k steps away 0.0 0.5 1.0 1 2 3 4 5 6 7 8 9 10 distance in cells 0.96 0.85 0.71 0.54 0.37 0.21 0.06 -0.05 -0.14 -0.21
Figure 2. A buffer wider than the distance where the correlation fades removes most of the leak.

The formula above turns that correlation into a count of independent cases, which collapses as neighbours become more alike:

A falling curve of the effective number of independent cells per 400 against the neighbour correlation, from 400 at zero correlation to about 10 at 0.95, with the lab's value marked Independent values among 400 cells, n(1 − ρ)/(1 + ρ), against the neighbour correlation ρ 0 100 200 300 400 0.0 0.2 0.4 0.6 0.8 1.0 neighbour correlation ρ lab: ρ = 0.951, about 10 independent values
Figure 3. A random split's thousands of test cells can hold only tens of independent values.

A test score built on millions of cells can rest on a much smaller number of genuinely different situations.

Three ways to hold out

A corpus for a fast flood model is a set of runs: each one a piece of terrain under one storm. Test cases can be held out by cell, by storm or by piece of terrain, and each choice answers a different question:

Three panels of four terrain tiles by three storms. Random cells: scattered squares held out everywhere. Whole storms: one row of runs held out across all tiles. Whole tiles: one column held out under all storms Four terrain tiles by three storms; filled squares are held out for testing Random cells neighbours are in training Whole storms the terrain is in training Whole tiles nothing from these tiles Columns: tiles of terrain. Rows: storms. Each small square: one cell of one run.
Figure 4. The same corpus, split three ways.
Held outWhat still leaks into trainingWhat the score estimates
Random cellsNeighbouring cells of the same runFilling gaps inside runs already simulated
Whole stormsThe same terrain under other stormsNew storms on terrain already in the corpus
Whole tilesNothing from those tilesNew terrain under storms like those in the corpus

In the lab, two models are trained and scored on the same synthetic corpus, split each of the three ways: a k-nearest-neighbour model, which predicts from the most similar training cells, and gradient-boosted trees, a standard choice for tabular data. Each split is repeated over ten seeds, with new terrain every time:

Paired bars of critical success index, means of ten seeds with whiskers from lowest to highest. Nearest neighbours: random cells 0.751, whole storms 0.705, whole tiles 0.611. Boosted trees: 0.693, 0.664 and 0.591. The whole-storm whiskers are the widest, from about 0.47 to 0.86 Lab: two models, three ways of holding out tests, ten seeds each 0 1.0 random cells 0.751 0.693 whole storms 0.705 0.664 whole tiles 0.611 0.591 nearest neighbours boosted trees Critical success index of wet cells. Bars: mean of ten seeds; whiskers: lowest and highest seed.
Figure 5. Only the tile bars say how a model does on terrain it has never seen; the storm bars swing with which storm was drawn.

The same results as numbers, with accuracy for comparison, which falls less because it also counts dry cells:

Held outModelCSI: mean (range)Depth errorAccuracy
Random cellsNearest neighbours0.751 (0.69 to 0.78)0.32 m0.87
Random cellsBoosted trees0.693 (0.61 to 0.76)0.28 m0.82
Whole stormsNearest neighbours0.705 (0.49 to 0.86)0.42 m0.85
Whole stormsBoosted trees0.664 (0.47 to 0.83)0.41 m0.81
Whole tilesNearest neighbours0.611 (0.48 to 0.67)0.48 m0.77
Whole tilesBoosted trees0.591 (0.45 to 0.70)0.38 m0.74

Nothing about either model changed between the bars. The random split credits a model with knowing what it was shown; the storm split, with knowing the terrain; only the tile split asks it to handle ground it has never seen. Random cells scored above whole tiles in every seed, by 0.14 on average for nearest neighbours (0.08 to 0.25) and by 0.10 for the trees (0.01 to 0.24). A model meant for new places should be scored on new places.

The storm split is the unstable one. With four storms, holding out a quarter means holding out one, and the score depends on which. When the lightest storm is left out, its thin, patchy wet area is hard to predict from heavier storms and the index falls to about 0.49 for nearest neighbours and 0.47 for the trees; for each of the other three it lies between 0.68 and 0.86. Over ten seeds the random split beat the storm split only five times. A split with few groups is a single draw, and the honest report is one score per held-out group, which here would show the light storm plainly.

The second model does not settle which is better, because that depends on the metric. The trees have lower depth error in most seeds and a lower critical success index in nearly all: they miss fewer wet cells but raise more false alarms, mostly shallow ones, at a median of about 0.16 m against about 0.3 m for nearest neighbours in two seeds checked. What both models agree on is the direction of the gap between random cells and new terrain.

The same lesson has been learned in other fields that map from gridded data. Ploton et al. (2020) reproduced the method behind large-scale maps of forest biomass and showed that tests ignoring the similarity of nearby points overstated its predictive power. Roberts et al. (2017) review the remedies for data with spatial, temporal or grouped structure; the core of them is to split by group, not by row.

Features that name the place

Leakage also runs through the inputs. A feature that identifies where a cell is (its coordinates, a tile identifier, a distance to a fixed landmark) lets a model recall nearby training cells instead of learning how water moves. Under a random split that looks like skill. Under a tile split it misleads:

Paired bars. With random cells held out, the critical success index is 0.823 with position features and 0.791 without. With whole tiles held out, it is 0.716 with position and 0.770 without Lab: the same model with and without each cell's position as a feature random cells held out with position 0.823 without 0.791 whole tiles held out with position 0.716 without 0.770 Critical success index. Position helps when neighbours are in training and hurts on new terrain.
Figure 6. The same feature raises one score and lowers the other.

Dropping position costs the random split 0.03 and gains the tile split 0.05. A team that chose features by the random-split score would have kept the feature that makes the model worse where it will be used. Features should be chosen on the split that matches the claim, for the same reason the headline score should be.

One score per area

A pooled score over several held-out areas is an average, and averages hide the hard places. Leaving out one tile at a time in the lab gives ten scores:

Ten dots for the critical success index on each held-out tile, from 0.632 to 0.784, with the mean, 0.72, marked Lab: leave one tile out at a time, ten scores instead of one 0.6 0.7 0.8 mean 0.72 Critical success index per held-out tile: 0.632 to 0.784. The mean hides a spread of 0.15.
Figure 7. Each dot is the score on a tile the model never saw.

The spread, 0.15, is about as large as the average gap between the random and tile splits above, 0.14. For a model meant for new places, the weakest held-out area is as informative as the mean, and the number of areas tested is part of the result.

Storms leak too

Holding out storms is weaker than it looks when storms are variations on one event. A corpus built by scaling one recorded storm up and down, or shifting it by an hour, contains many near-copies. Holding out one of them tests the model on a storm it has seen in all but scale. The split has to follow the real unit of independence: distinct weather events, not distinct runs.

Terrain has the same trap at a larger scale. Two tiles cut from the same valley share its slope, soils and drainage pattern. Holding out one of them tests less than holding out a different valley. When there are few independent places, the honest statement is the number of places the model has been tested on, beside the score.

Choosing the split

The right split follows from the claim:

If the claim isHold outReport beside the score
Fast answers for new storms on a place already simulatedWhole storms, as distinct weather eventsThe number of distinct events
It works in a new placeWhole areas, with a buffer between training and testA score per area, and the number of areas
It works in a new kind of placeWhole regions or terrain typesThe number of regions, and how they differ

Three practical details matter as much as the choice itself. Leave a buffer: if test tiles touch training tiles, the cells along the shared edge still leak, so drop a strip at the boundary at least as wide as the distance over which neighbours stop resembling each other, four to seven cells in the lab, or pick tiles apart from each other.

Two 16 by 16 grids with a 4 by 4 test area in the middle. Left: no buffer, test cells touch training cells. Right: a three-cell ring around the test area is dropped from training Holding out an area with a buffer: the ring of cells around it is used for neither No buffer: edge cells leak With a buffer Blue: held-out test area. Faded: buffer cells dropped from training. White: training. Without the buffer, every test cell on the edge has a training neighbour one cell away.
Figure 8. A ring as wide as the correlation range costs training data and keeps the test's nearest neighbours out of training.

Report scores per held-out area, not only pooled; one easy area can hide two hard ones. And keep one area untouched until the end. Every decision made while looking at a test score (a threshold, a feature, a stopping point) leaks a little of that test into the model.

Runs where nothing happens

A corpus fills up with degenerate runs without anyone choosing them. A light storm on high ground leaves every cell dry; a huge storm on a basin floods all of it. Such runs are cheap to predict and inflate any metric that rewards getting dry cells right. In the lab, adding runs where almost nothing floods, on the same terrain, moves accuracy and leaves the critical success index roughly where it was, between 0.62 and 0.66:

Paired bars for three corpora with 0%, 20% and 50% of runs nearly dry: accuracy 0.77, 0.80 and 0.87; critical success index 0.66, 0.62 and 0.65 Lab: adding runs where almost nothing floods, held-out tiles 0% of runs dry accuracy 0.77 CSI 0.66 20% of runs dry accuracy 0.80 CSI 0.62 50% of runs dry accuracy 0.87 CSI 0.65 Accuracy counts every correctly dry cell; the critical success index ignores cells both sides call dry.
Figure 9. Accuracy rewards the easy dry cells; the critical success index does not count them.

Depth error falls in the same way, from 0.44 m to 0.31 m, because a dry cell predicted dry has no error at all. A metric that improves when the corpus gets easier is measuring the corpus, not the model.

Two checks catch most of this before any training, at almost no cost:

CheckRuleFlagged in the lab
Nearly dry or nearly full runsWet share below 5% or above 95%20 of 70 runs
Near-duplicate runsSame terrain, wet areas overlapping by more than 95%9 pairs, storms of 60 and 62 units

Flagged runs are not always wrong to keep. Dry runs teach a model where water does not go. But they should be there by choice, counted, and kept out of the headline score or reported beside a score that ignores them.

A small lab you can run

The lab is synthetic: square tiles of random terrain, water that fills from the lowest cell, and two models with six features per cell, both in plain Python. Download the lab (zip), then run python run_all.py and python -m pytest -q. Its seven experiments produce the other figures, and python seeds.py produces the two-model comparison over ten seeds. The boosted trees are a short implementation in the lab; scikit-learn's histogram gradient boosting with the same settings gave scores within 0.02 on one seed. Each experiment draws its own terrain and test sets, so the same split can score a few hundredths differently from one figure to another: compare within a figure, not across figures. Its eight tests check each claim against the alternative it rejects, for example that a random split shares terrain between training and test while a tile split shares none.

Limits of this post

  • The lab's water fills by volume on small tiles. Real floods flow and drain, and real grids are larger and finer.
  • Two models are a small sample. Both put random cells above new terrain in every seed, with gaps of different size; other models will differ again in size, and the lab has not tested them.
  • Ten seeds give a range, not a confidence interval. The ranges quoted are the lowest and highest seed.
  • The effective-sample formula assumes one correlation between neighbours. Real depth fields are correlated over many distances at once.

Sources