Duplicate entities and split membership
Quick answer
Unique row IDs do not guarantee independent observations. Several messages may belong to one synthetic support case, and related rows can reveal each other’s labels. If deployment must generalize to new cases, assign all rows of a case to the same split. An entity-aware split groups related records by a stable case identifier; temporal evaluation also requires chronological availability checks. Examples use local Python 3.13.5 and scikit-learn 1.8.0 where imported. All fixtures are synthetic; ByteMind library execution is unverified.
Find cross-split entities
Three distinct row IDs represent only two cases. Compare group membership across train and test.
train = [('R1','C1'), ('R2','C2')]test = [('R3','C1')]print(sorted({g for _,g in train} & {g for _,g in test}))Expected output:
['C1']C1 appears in both splits even though row IDs differ. Reassign all C1 rows together before training; do not remove evidence of the overlap from the audit.
Use grouped cross-validation
GroupKFold keeps a group out of both training and test in each fold. The toy fixture has four groups and two folds. Check properties rather than relying on the exact order of folds.
from sklearn.model_selection import GroupKFoldgroups = ['A','A','B','B','C','C','D','D']X = [[i] for i in range(len(groups))]for train,test in GroupKFold(n_splits=2).split(X, groups=groups): overlap = {groups[i] for i in train} & {groups[i] for i in test} print(len(train),len(test),len(overlap))Expected output:
4 4 04 4 0Each fold has disjoint group IDs. GroupKFold does not enforce chronology or guarantee balanced target classes. It is suitable for this tiny group demonstration only.
Common mistakes
Exact duplicates are identical on a chosen representation; near duplicates may require a reviewed normalization or similarity method. Repeated observations are not always erroneous duplicates: preserve valid history and group it when appropriate. Choose the grouping key to match deployment (case, device, session or another independent unit). Grouping does not fix temporal leakage; use an appropriate time-aware policy when predicting future events. Never tune on the final holdout. Do not infer population performance from the tiny example.
Practice and next steps
Audit the chapter fixture by case and row ID, verify no case crosses the chosen splits, and preserve the original observations. Explain when a chronological split is more appropriate. The next chapter studies preprocessing and missing data. The separate pure-Python exercise assesses the underlying calculation, not library execution.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Duplicate entities and split membership — knowledge check
Apply data collection, labeling and provenance reasoning to synthetic examples.
Unique row IDs but a shared case across train and test indicate what risk?
What is a suitable grouping key for predicting unseen support cases?
Does GroupKFold enforce chronological order?
Two rows of the same group in train only are what?
When should group overlap be resolved?