Grouped K-Fold Cross-Validation
What you'll learn: How to prevent data leakage when your dataset contains groups of related samples that must stay together during validation.
The Problem with Regular K-Fold and Groups
Sometimes your data has a group structure where multiple samples belong to the same entity. For example:
- Medical data: Multiple measurements from the same patient
- Image data: Multiple photos of the same person
- Time series: Multiple transactions from the same customer
- Sensor data: Multiple readings from the same device
If you use regular K-Fold Cross-Validation here, samples from the same group might appear in both training and validation sets. This creates data leakage — your model learns patterns from Group A's training samples and gets tested on Group A's validation samples. It appears to perform well, but only because it's essentially "seeing" the same entity twice!
How Grouped K-Fold Works
Grouped K-Fold ensures that all samples from the same group stay together in either the training fold or the validation fold, but never split across both.
Think of it like organizing students into teams. If siblings must be on the same team, you can't put one sibling on Team A and another on Team B — the whole family stays together.
The algorithm assigns entire groups (not individual samples) to folds, preventing any group from appearing in multiple folds simultaneously.
When to Use It
Use Grouped K-Fold whenever your samples have inherent grouping and you want to test how well your model generalizes to completely new groups it has never seen before.
Key Takeaway: Grouped K-Fold prevents data leakage by keeping all samples from the same group together in the same fold, ensuring your validation tests true generalization to unseen groups.