Class-wise stratified K-Fold iterator variant with non-overlapping groups.
This cross-validation object is a variation of that
attempts to return stratified folds with non-overlapping groups. The folds are made
by preserving the percentage of samples for each class in in a binary or
multiclass classification setting.
StratifiedKFold
y
Each group will appear exactly once in the test set across all folds (the
number of distinct groups has to be at least equal to the number of folds).
The difference between GroupKFold
and StratifiedGroupKFold is that
the former attempts to create balanced folds such that the number of
distinct groups is approximately the same in each fold, whereas
StratifiedGroupKFold attempts to create folds which preserve the
percentage of samples from each class as much as possible given the
constraint of non-overlapping groups between splits.
Whether to shuffle each class’s samples before splitting into batches.
Note that the samples within each split will not be shuffled.
This implementation can only shuffle groups that have approximately the
same y class distribution, no global shuffle will be performed.
random_state : int or RandomState instance, default=None
When shuffle is True, random_state affects the ordering of the
indices, which controls the randomness of each fold for each class.
Otherwise, leave random_state as None.
Pass an int for reproducible output across multiple function calls.
See Glossary.
Notes
The implementation is designed to:
Mimic the behavior of StratifiedKFold as much as possible for trivial
groups (e.g. when each group contains only one sample).
Be invariant to class label: relabelling y=["Happy","Sad"] to
y=[1,0] should not change the indices generated.
Stratify based on samples as much as possible while keeping
non-overlapping groups constraint. That means that in some cases when
there is a small number of groups containing a large number of samples
the stratification will not be possible and the behavior will be close
to GroupKFold.
Configure whether metadata should be requested to be passed to the split method.
Note that this method is only relevant when this estimator is used as a
sub-estimator within a meta-estimator and metadata routing is enabled
with enable_metadata_routing=True (see sklearn.set_config).
Please check the User Guide on how the routing
mechanism works.
The options for each parameter are:
True: metadata is requested, and passed to split if provided. The request is ignored if metadata is not provided.
False: metadata is not requested and the meta-estimator will not pass it to split.
None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.
str: metadata should be passed to the meta-estimator with this given alias instead of the original name.
The default (sklearn.utils.metadata_routing.UNCHANGED) retains the
existing request. This allows you to change the request for some
parameters and not others.
Parameters:
groups : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for groups parameter in split.
Returns:
self : object
The updated object.
split(X, y=None, groups=None)
Generate indices to split data into training and test set.
Parameters:
X : array-like of shape (n_samples, n_features)
Training data, where n_samples is the number of samples
and n_features is the number of features.
y : array-like of shape (n_samples,), default=None
The target variable for supervised learning problems.
groups : array-like of shape (n_samples,), default=None
Group labels for the samples used while splitting the dataset into
train/test set.