group split
**GroupKFold** is a **cross-validation strategy that prevents data leakage by ensuring all samples from the same "group" stay together in either the training set or the test set, never split across both** — where a "group" is any logical unit whose samples are not independent: all X-rays from the same patient, all frames from the same video, all transactions from the same user — because splitting a patient's images across train and test lets the model memorize that patient's unique characteristics rather than learning the actual task, producing inflated performance estimates that collapse in production.
**What Is GroupKFold?**
- **Definition**: A cross-validation splitter that takes a group label for each sample and guarantees that no group appears in both the training and test folds — all samples from Patient A are either entirely in training or entirely in testing.
- **The Problem (Data Leakage)**: If Patient A has 10 X-rays and 8 go to training and 2 to testing, the model learns Patient A's bone structure, skin tone, and imaging artifacts — then "recognizes" Patient A in the test set. This isn't medical diagnosis; it's patient memorization. Performance looks great in cross-validation but fails on new patients.
- **The Solution**: GroupKFold ensures the model is always evaluated on groups it has never seen during training — simulating real-world deployment where new patients/users/videos arrive.
**The Data Leakage Problem**
| Split Method | Patient A's X-rays | What Model Learns | Test Performance |
|-------------|--------------------|--------------------|-----------------|
| **Random Split** | 8 in Train, 2 in Test ⚠️ | Patient A's unique features | Inflated (memorization) |
| **GroupKFold** | All 10 in Train OR all 10 in Test ✓ | Disease features (generalizable) | Honest (generalization) |
**Common Scenarios Requiring GroupKFold**
| Domain | Group | Why Groups Matter |
|--------|-------|------------------|
| **Medical Imaging** | Patient ID | Same patient's scans share anatomy, artifacts |
| **Video Classification** | Video ID | Frames from same video are nearly identical |
| **User Behavior** | User ID | Same user's actions are correlated |
| **Geographic Data** | Location/Region | Nearby locations share environmental features |
| **Time Series per Entity** | Entity ID | Same sensor/device has device-specific drift |
| **Multi-turn Dialog** | Conversation ID | Utterances in same conversation share context |
**Python Implementation**
```python
from sklearn.model_selection import GroupKFold
groups = df['patient_id'].values # Group labels
gkf = GroupKFold(n_splits=5)
for train_idx, test_idx in gkf.split(X, y, groups=groups):
X_train, X_test = X[train_idx], X[test_idx]
y_train, y_test = y[train_idx], y[test_idx]
# All of Patient A's samples are in EITHER train OR test
```
**GroupKFold Variants**
| Variant | Behavior | Use Case |
|---------|----------|----------|
| **GroupKFold** | Groups distributed across K folds (no stratification) | Standard grouped CV |
| **StratifiedGroupKFold** | Groups kept together + class proportions preserved | Grouped + imbalanced |
| **LeaveOneGroupOut** | Each fold holds out exactly one group | Small number of groups |
| **GroupShuffleSplit** | Random group-based split (not exhaustive) | Large number of groups |
**Impact of Ignoring Groups**
| Metric | Random CV (Leaking) | GroupKFold (Honest) | Reality (Production) |
|--------|--------------------|--------------------|---------------------|
| Accuracy | 95% ⚠️ | 82% ✓ | ~80% |
| F1 Score | 0.93 ⚠️ | 0.78 ✓ | ~0.76 |
The honest GroupKFold estimate is much closer to actual production performance.
**GroupKFold is the essential cross-validation strategy for non-independent data** — preventing the data leakage that occurs when correlated samples from the same group appear in both training and testing, producing honest performance estimates that accurately predict how the model will perform on genuinely new groups in production.