Skip to main content

ensemble.kfold_bagging_groups

ensemble.kfold_bagging_groups(groups, n_folds=5, n_reps=20, seed=0)

Generate train and test indices for k-fold bagging with group constraints.

Like kfold_bagging, but operates at the group level so that members of the same group are never split across training and test sets. Groups are assigned to folds while balancing by sample count: larger groups are placed first, and each group is assigned to a randomly chosen fold that still has room.

The total number of train-test pairs is n_reps * n_folds.

Notes

The fold assignment assumes that there are enough small groups (e.g., single-member groups) to fill folds to their expected size. If all groups are large relative to the expected fold size, a group may not fit into any fold, causing an infinite loop. For example, with two groups of sizes 1 and 5 and n_folds=2, the expected fold size is 3, but the group of size 5 cannot be placed.

Parameters

Name Type Description Default
groups list of arrays Each array contains the sample indices of members of one group. required
n_folds int Number of folds. 5
n_reps int Number of repetitions of the k-fold procedure. 20
seed int Random seed for the random number generator. 0

Returns

Name Type Description
indices_li list of tuples List of (train_idx, test_groups) tuples, where train_idx is an array of observation indices used for training and test_groups is an array of group indices that have no overlap with train_idx.