Group shuffle split

From wenmin-wu/ds-skills · View interactive version

Group shuffle split is an AI agent skill from the GitHub repository wenmin-wu/ds-skills. Splits train/validation using GroupShuffleSplit so that related samples (forks, families, sessions) never span both sets.

How to use this skill

Skill file

Group Shuffle Split

Overview

When samples have group relationships (notebook forks sharing an ancestor, patients in the same hospital, users in the same household), random splits leak information across train/validation. GroupShuffleSplit ensures all samples from the same group land in the same split, preventing data leakage while still allowing a simple holdout split (unlike GroupKFold which requires K folds).

Quick Start

from sklearn.model_selection import GroupShuffleSplit

splitter = GroupShuffleSplit(n_splits=1, test_size=0.1, random_state=42)
groups = df["group_id"]  # e.g., ancestor_id, patient_id, session_id

train_idx, val_idx = next(splitter.split(df, groups=groups))
train_df = df.iloc[train_idx].reset_index(drop=True)
val_df = df.iloc[val_idx].reset_index(drop=True)

Workflow

  1. Identify the group column (ancestor, patient, user, session, etc.)
  2. Create a GroupShuffleSplit with desired test size
  3. Split once to get train/val indices where no group spans both sets
  4. Use the indices to create train and validation DataFrames

Key Decisions

References

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.