How to split duplicate samples to train test with no overlapping?
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
If duplicate samples can appear in both train and test sets, evaluation becomes overly optimistic because the model is effectively seeing the same example twice. The correct fix is not a normal row-wise split. It is a group-based split where all duplicates are assigned to the same side.
Why Ordinary train_test_split Fails
A row-wise split treats every row as independent. That breaks down when multiple rows represent the same underlying sample.
Example problem:
If you split this row by row, one copy of sample 1 might go to training and another to testing. That leaks information.
Split by Group Instead of by Row
The right abstraction is a group identifier that marks all duplicates belonging to the same underlying sample. If you already have a sample ID, use that. If not, build one from the duplicated feature columns.
With scikit-learn, GroupShuffleSplit is often the simplest solution.
This guarantees that all rows with the same sample_id stay together.
Build a Group Key When No ID Exists
Sometimes the dataset has duplicates but no explicit identifier. In that case, construct a group key from the columns that define duplication.
If duplicates are defined only by features and not by label, leave the label out of the key. The point is to capture the real overlap rule in one grouping column.
Then split on that key the same way.
Use Grouped Cross-Validation Too
If you are doing model selection rather than a single holdout split, use grouped cross-validation instead of ordinary K-fold.
The intersection should be empty for each fold. That is the guarantee you actually want.
Verify the Split Explicitly
Do not assume the grouping logic is correct. Check it.
If the overlap is non-empty, the split is still leaking duplicates.
This verification step is especially important when the grouping key was built manually from several columns.
Stratification and Grouping Can Conflict
A common follow-up requirement is preserving class balance while also respecting duplicate groups. That is harder than plain group splitting because perfect class proportions may not be possible once groups are indivisible.
If both matter, consider:
- '
StratifiedGroupKFoldif your sklearn version provides it' - custom group-aware splitting logic
- accepting approximate rather than perfect label balance
The higher priority is usually no leakage. A slightly imperfect class ratio is often better than a perfectly balanced but contaminated test set.
Common Pitfalls
- Using ordinary
train_test_spliton duplicated data and assuming randomization is enough. - Defining duplicate groups incorrectly, so near-identical rows still leak across the split.
- Forgetting to use grouped cross-validation after fixing the holdout split.
- Prioritizing exact class balance over eliminating overlap leakage.
- Skipping explicit overlap checks after the split.
Summary
- Duplicate samples should be split by group, not by individual row.
- '
GroupShuffleSplitis a strong default for train-test splitting without overlap.' - Build a group key manually if no sample ID exists.
- Use grouped cross-validation for model selection as well.
- Always verify that no group appears in both train and test sets.
Related reading
- How to Split the Input into different channels in Keras
- How to stack multiple lstm in keras?
- How to stop training when it hits a specific validation accuracy?
- How to store best models checkpoints, not only newest 5, in Tensorflow Object Detection API?
- How to store neural network knowledge data?
- how to store numpy arrays as tfrecord?
- How to stratify the training and testing data in Scikit-Learn?
- How to structure Machine Learning projects using Object Oriented programming in Python?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.