Train–Test Data Leakage in Bioinformatics: Split by Source, Not by Row

A classifier can score 100% on held-out measurements without learning anything that transfers to a new biological source. The clue is in what you held out: a row is not necessarily an independent sample.

Direct answer

Split on the unit you expect to be new at deployment, such as donor, cell line, experiment, plate, or site. If the same source appears in training and testing, a model may recognise the source instead of learning a signal that generalises.

How can 100% balanced accuracy be misleading?

We simulated 60 source groups with five measurements per source (300 rows total). Each source had a distinct fingerprint across eight numeric features and a randomly assigned class. A one-nearest-neighbour classifier can match very similar measurements. There is no designed rule connecting fingerprints to class on unseen sources.

Scatter plot of ten source groups, with five nearby simulated rows per group
Figure 1. Ten of the 60 simulated sources. The plot shows two of eight input features; colours identify sources, not classes.

Using the same data and classifier, we held out 75 rows two ways: a stratified random row split and a group-aware split holding out 15 entire sources. We calculated balanced accuracy as the mean of recall for class 0 and class 1. No dataset was downloaded; no clinical or biological claim follows from this example.

Test designShared sourcesBalanced accuracy
Random rows44100%
Unseen sources033%
Bar chart: 44 source groups appear in both partitions of the random row split, compared with zero in the group split
Figure 2. Both splits use 225 training and 75 test rows, but only the group-aware split keeps every source on one side.
Balanced accuracy is 100% with the random row split and 33% with the group split
Figure 3. Balanced accuracy averages recall over the two classes. The 50% reference is not a guarantee for one finite split.

Every random-split test row had its nearest training neighbour from the same source. In the group split, none did. The 33% result came from one seed and one particular set of groups; it illustrates unstable performance on unseen sources, not a general benchmark or evidence of a reliably below-chance model.

Nearest training-row distance is much smaller for the random row split than for the group split
Figure 4. Median nearest-training-row distance rose from 0.16 to 2.08 when sources were held out. Distances are in arbitrary simulation units.
Confusion matrices: random split makes zero mistakes; group split predicts 9 of 25 class-zero and 15 of 50 class-one rows correctly
Figure 5. The apparent perfect result vanishes on new sources. These are simulated row counts, not patient outcomes.

Which column should be the group?

  • Predict a new donor: group every aliquot, visit and technical replicate by donor.
  • Predict a new lab or site: hold out entire sites or use an independent site. Donor isolation alone does not test site transfer.
  • Predict a later time period: respect time order. Grouping alone does not make a random split prospective.

If multiple relationships overlap, no single grouping column may satisfy every requirement. Document the intended prediction task and audit the split for the relationships that matter before choosing an evaluation design.

What should you check before training?

  1. Keep a source ID in the sample sheet and calculate the train/test intersection. It should be empty for the isolation criterion you chose.
  2. Count both groups and rows per class in each partition. A group-aware split can still produce uneven classes. Don’t silently select the split with the best score.
  3. Fit scaling, imputation and feature selection only on each training fold. Keep your final test set out of tuning.
  4. Save the assigned source IDs, software version and scoring rule. If your claim is about new sources, report performance and uncertainty at that level where appropriate.
from sklearn.model_selection import GroupShuffleSplit
splitter = GroupShuffleSplit(n_splits=1, test_size=0.25, random_state=17)
train, test = next(splitter.split(X, y, groups=source_id))
assert set(source_id[train]).isdisjoint(source_id[test])

source_id is a NumPy array aligned with the rows of X. The quarter in this example refers to groups, not rows; the row counts match only because all groups are equal-sized.

FAQs

Is a stratified row split enough? No. Stratification can balance labels while still spreading repeated measurements from the same source across partitions.

Is one group split enough for a paper? Not necessarily. Depending on the claim and the number of groups, use group-aware cross-validation and, where feasible, a truly independent test set. Report how many independent sources were available.

References and related reading

Reviewed 4 December 2026. All figures are original Python-generated outputs from the described, seeded simulation. Model: scikit-learn 1.8.0.

Post a Comment

0 Comments