← Blog

Why shuffled cross-validation can lie: row order, duplicates and leakage in experimental data

You train a model on your lab data, run 5-fold cross-validation, and get R² = 0.91. It goes into the draft. Six months later, new specimens come in and the model is far worse.

Nothing was wrong with the code. The validation answered a different question from the one you asked. In experimental and engineering data, three quiet habits cause this more often than any modelling choice: how the rows are ordered, whether some rows repeat, and whether the folds respect how the data was collected.

What cross-validation assumes

Shuffled k-fold cross-validation assumes every row is an independent draw. Each fold is a fair stand-in for new data the model has never seen.

Lab data rarely behaves like that. Measurements are logged in time, sensors drift, specimens come in batches, and the same mix or setting is often tested more than once. When rows are related, a shuffled split lets the model meet a near-copy of each test row during training. The score then measures recall, not prediction.

Trap 1: rows that are truly ordered in time

If your data is a time series (a sensor log, a test rig recording, a monitoring campaign), neighbouring rows are similar. Shuffle them, and the model learns from yesterday and tomorrow to predict today. That is interpolation, and it looks excellent.

In a simple simulated example (an autocorrelated signal plus one real input), shuffled 5-fold cross-validation reports R² = 0.87. Validating the honest way, training on the past and testing on the future, gives a negative R²: the model is worse than predicting the mean. Same data, same model, opposite conclusions.

The fix is a time-ordered split: train on earlier rows, test on later ones, ideally with a small gap between them so near-identical neighbours cannot leak across the boundary.

Trap 2: rows that only look ordered

The reverse mistake is just as real. Many datasets are sorted by something other than time: by mix design, by specimen, by test campaign. Sorted rows also look autocorrelated, even when there is no time in them at all.

The UCI Concrete Compressive Strength dataset (1,030 mixes) is a good example. In file order, strength has a lag-1 autocorrelation of 0.57, because rows with similar mixes sit next to each other. Treat it as a time series and the model is forced to extrapolate to mixes it has never seen: the time-ordered split scores R² = 0.25, while shuffled 5-fold scores 0.91.

Here the shuffled score is the honest one. The sort order is explained by the inputs themselves: once a model accounts for the mix, the leftover errors have a lag-1 autocorrelation of only 0.17.

Same model, opposite verdicts: the honest split depends on the data

Blue = the honest split for that data

The same model gives opposite verdicts depending on the split. Which split is honest depends on how the data was collected, not on which score is higher. Computed 6 Dec 2026: UCI Concrete (1,030 rows, CC BY 4.0) and a simulated autocorrelated series (1,000 rows); scikit-learn random forest, 300 trees.

How to tell the two apart

Do not decide from the raw target. Check the errors instead:

  1. Fit a model with shuffled folds and keep its out-of-fold errors.
  2. Measure the autocorrelation of those errors in file order (lag-1, plus a Ljung–Box test).
  3. If the errors are still strongly autocorrelated, the order carries information the inputs do not explain: use time-ordered folds. If not, the order was only sorting: shuffled folds are fine.

When it is close, report both scores. A reviewer will trust a model more when you show that you checked.

Trap 3: duplicate rows and grouped data

If the same row appears twice and one copy lands in training while the other lands in testing, the model is tested on an answer it has already seen. Repeated specimens, replicated test settings and copied rows all do this.

How much it matters depends on how many there are. The Concrete data has 25 exact duplicate rows; keeping each group of copies in one fold changes the score very little here (0.91 either way). In datasets where replicates are common, the effect can be large. Either way, the check costs nothing and belongs in the methods section.

The same logic applies to groups: specimens from one batch, readings from one turbine, tests on one machine. If the question is "how well does this predict a new batch?", every row of a batch must stay in the same fold.

A checklist before you report a score

The takeaway

A cross-validation score is only as honest as the split behind it. The right split depends on how your data was collected, not on which number looks best. Checking takes minutes, and it is often the difference between a model that holds up and one that does not.

BlinkDA runs these checks automatically on every dataset and states the result in the report.

Get early access, with 10% off

BlinkDA is launching soon. Join the early-access list and you will hear first when it opens. Everyone who confirms their email before launch gets 10% off their first 12 months of any BlinkDA subscription, monthly or annual, applied to the account that uses the same email.

Join the early-access list

The discount applies to subscriptions only. One-time reports are not included. Offer terms. We send a confirmation link, a welcome note, one email on launch day and occasional launch updates. Nothing else.

UCI Concrete Compressive Strength data: I-Cheng Yeh, UCI Machine Learning Repository, CC BY 4.0.