Practice in Lab: Split identities before splitting examples →
Split identities before splitting examples
Randomly splitting rows leaks information when several rows belong to the same person, device or document. Validation must separate the unit that will actually be new at deployment. Keeping a group intact can produce uneven fold sizes; equal row counts are less important than avoiding leakage.
This deterministic allocator sorts group names and assigns them round-robin to folds. It is a simple reproducible policy rather than an optimal balance algorithm. Evaluate whether labels and groups are confounded before interpreting a fold score. A grouped split cannot repair a target that already contains future information.
groups=["a","a","b","c"]
assigned={g:i%2 for i,g in enumerate(sorted(set(groups)))}
print([assigned[g] for g in groups])Review the design
The split unit should match the deployment question. Group separation estimates new-group performance; it does not estimate future drift within groups, so a time-aware design may also be required.
Your task
Implement group_folds(groups,k) returning k lists of row indices. Sort distinct group names and assign group number modulo k. Require 2<=k<=number of distinct groups.
The explanation opens here once every check passes.