Stratified Group K-Fold cross-validator with binned continuous targets.
This splitter combines:
- KBinsDiscretizer to bin continuous y values into discrete categories
- StratifiedGroupKFold to ensure stratified splits while respecting groups
This is useful for regression tasks where you want stratified sampling
(balanced target distribution across folds) while ensuring samples from
the same group are never split across train and test sets.
Parameters:
n_splits (int, default=5) – Number of folds. Must be at least 2.
n_bins (int, default=10) – Number of bins for discretizing continuous y values.
More bins = finer stratification but may fail with small datasets.
Strategy used to define the widths of the bins:
- ‘uniform’: All bins have identical widths.
- ‘quantile’: All bins have the same number of points (recommended for
imbalanced distributions).
’kmeans’: Values in each bin have the same nearest center of a 1D
k-means cluster.
shuffle (bool, default=False) – Whether to shuffle each class’s samples before splitting.
random_state (int or None, default=None) – Random state for reproducibility when shuffle=True.
Implements stratified sampling using KBins discretization.
Every occupied bin needs at least two samples, and each split must have
enough samples for all occupied bins. Sparse targets may require fewer bins
or strategy=”quantile”. Defaults remain uniform bins and a 25% test split.
Assigns samples to folds using joint X-Y distance (SPXY algorithm) to ensure
each fold is spatially representative of the full feature and target space.
When y_metric is None, falls back to pure Kennard-Stone (X-only) partitioning.
This splitter does NOT handle groups. For group-aware SPXY splitting, use
SPXYGFold or combine SPXYFold with the GroupedSplitterWrapper via the
group_by pipeline syntax.
Parameters:
n_splits (int, default=5) – Number of folds for cross-validation. Must be at least 2.
metric (str, default="euclidean") – Distance metric for X-space. Any metric supported by scipy.spatial.distance.cdist.
y_metric (str or None, default="euclidean") – Distance metric for Y-space.
- “euclidean”: For regression (continuous y) - default SPXY behavior
- “hamming”: For classification (categorical y)
- None: Ignore Y (pure Kennard-Stone, X-only selection)
pca_components (int or None, default=None) – If provided, apply PCA to reduce X dimensionality before distance computation.
random_state (int or None, default=None) – Random state for reproducibility (used only for PCA).
Combines:
- SPXY (joint X-Y distance) or Kennard-Stone (X-only) selection
- Group constraints (samples in same group stay together)
- K-fold cross-validation
This splitter extends the SPXY algorithm to support:
1. Classification tasks (using appropriate distance metrics for categorical y)
2. Group-aware splitting (treating groups as atomic units)
3. K-fold cross-validation (not just single train/test split)
The algorithm ensures uniform coverage of the feature space (and optionally
target space) across all folds, which is particularly useful for spectroscopy
data where sample distribution matters for model generalization.
Parameters:
n_splits (int, default=5) – Number of folds for cross-validation. Use 1 for single train/test split.
Must be at least 2 for cross-validation.
test_size (float, default=None) – Proportion of samples for test set. Only used when n_splits=1.
If None with n_splits=1, defaults to 0.25.
Distance metric for Y-space.
- “euclidean”: For regression (continuous y) - default SPXY behavior
- “hamming”: For classification (categorical y) - treats all class
differences equally
None: Ignore Y (pure Kennard-Stone, X-only selection)
aggregation (str, default="mean") – Method for group aggregation when groups are provided:
- “mean”: Use group centroid (mean of all samples in group)
- “median”: Use group median (robust to outliers)
pca_components (int or None, default=None) – If provided, apply PCA to reduce X dimensionality before distance
computation. Useful for high-dimensional spectral data.
random_state (int or None, default=None) – Random state for reproducibility. Only used for tie-breaking when
multiple samples have equal distances.