Skip to content
Back to skills

Group Kfold Leak Prevention

ASecurity

Uses GroupKFold to prevent data leakage when multiple rows share a common entity (e.g., same user, question, or document).

  • 61 stars
  • 0 votes
  • 0 copies
  • 0 views
  • Added September 12, 2026
developmentpythongo

Security analysis

A100/100

Scanned September 12, 2026

npx -y skills add wenmin-wu/ds-skills --skill group-kfold-leak-prevention --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Group Kfold Leak Prevention?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Group Kfold Leak Prevention
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/wenmin-wu-group-kfold-leak-prevention/badge)](https://www.skillsdirectory.com/skills/wenmin-wu-group-kfold-leak-prevention)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: tabular-group-kfold-leak-prevention
description: >
  Uses GroupKFold to prevent data leakage when multiple rows share a common entity (e.g., same user, question, or document).
---
# 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

```python
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

- **Group key selection**: Choose the entity that would cause leakage if split (question_body, user_id)
- **Stratification**: `GroupKFold` doesn't stratify; for imbalanced targets, use `StratifiedGroupKFold` (sklearn 1.0+)
- **Number of folds**: Must have at least `n_splits` unique groups
- **Nested groups**: If multiple group levels exist, group by the coarsest level

## References

- Google QUEST Q&A Labeling competition (Kaggle)
- Source: [quest-bert-base-tf2-0](https://www.kaggle.com/code/akensert/quest-bert-base-tf2-0)

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.