Group kfold leak prevention

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

Group kfold leak prevention is an AI agent skill from the GitHub repository wenmin-wu/ds-skills. Uses GroupKFold to prevent data leakage when multiple rows share a common entity (e.g., same user, question, or document).

How to use this skill

Skill file

GroupKFold Leak Prevention

Overview

When rows in a dataset share a group identity (same question, user, session, etc.), standard KFold can place related rows in both train and validation, causing leakage. GroupKFold ensures all rows from a group land in the same fold, giving honest validation scores.

Quick Start

from sklearn.model_selection import GroupKFold

def group_kfold_split(X, y, groups, n_splits=5):
    """Generate leak-proof train/val splits by group.

    Args:
        X: features array
        y: target array
        groups: array of group IDs (e.g., question_id, user_id)
        n_splits: number of folds

    Yields:
        (train_idx, val_idx) tuples
    """
    gkf = GroupKFold(n_splits=n_splits)
    for train_idx, val_idx in gkf.split(X, y, groups=groups):
        yield train_idx, val_idx

Workflow

  1. Identify the group column (entity that links related rows)
  2. Replace KFold / StratifiedKFold with GroupKFold
  3. Pass group IDs via the groups parameter
  4. Train and evaluate per fold as usual
  5. Average fold scores for final CV estimate

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.