🧷  Stratified Group K-Fold

Stratified Group K-Fold#

K-fold CV preserving class balance while keeping groups intact across folds.

Important

✨ AI-generated content. This page was written with the assistance of an AI language model and is provided as a learning aid. Despite careful review, it may still contain mistakes, omissions, or out-of-date information. Whether you are new to the topic, a team lead, or a senior practitioner, treat it as a starting point rather than an authoritative reference: read it critically and independently verify anything you act on (code, commands, figures, and factual claims) against official documentation and primary sources before relying on it.

What it is#

Stratified Group K-Fold is a cross-validation scheme that fuses three requirements at once: k-fold splitting, stratification (preserve the class balance in every fold), and grouping (keep every group — same patient, user, session — entirely on one side of each split). It is the right tool for grouped *and* imbalanced classification.

Why it’s needed#

Each simpler scheme covers only part of the problem. Stratified k-fold balances classes but can let one group’s rows fall into both train and validation, leaking information. Group k-fold prevents that overlap but can wreck the class balance. Stratified group k-fold does both — class proportions held and group boundaries respected.

How it works and an example#

Identify the group key, then build folds that are simultaneously class-balanced and group-clean. For 1,000 samples from 100 patients with a 20/80 disease split and k = 5, each fold holds about 20 patients, preserves the ~20/80 ratio, and shares no patient between train and validation.

In scikit-learn#

from sklearn.model_selection import StratifiedGroupKFold

cv = StratifiedGroupKFold(n_splits=5)
for train_idx, test_idx in cv.split(X, y, groups):  # groups = patient IDs
    ...

The comparison is clean: plain k-fold is neither stratified nor group-aware, stratified k-fold adds class balance, group k-fold adds group safety, and stratified group k-fold is the only one with both.


Theme: Validation & Cross-Validation  ·  All terminology



See also

Source article Adapted (context, re-expressed) in our own words from: Stratified Group K-Fold (insightful-data-lab.com).

Tags: purpose: reference topic: terminology level: intermediate