spectrochempy.GroupKFold

class GroupKFold(n_splits=5)[source]

Split observations while keeping each group in a single validation fold.

This is a thin SpectroChemPy adaptation of sklearn.model_selection.GroupKFold. It uses scikit-learn’s partitioning algorithm unchanged. Each distinct group appears in exactly one validation fold and is never shared between the calibration and validation subsets of a fold.

Parameters:

n_splits (int, optional, default: 5) – Number of folds. Must be at least 2 and no greater than the number of distinct groups.

See also

cross_validate

Execute supervised cross-validation.

KFold

Split observations without group constraints.

sklearn.model_selection.GroupKFold

Underlying implementation.

Notes

The public signature intentionally contains only parameters supported across the scikit-learn versions used by SpectroChemPy. Group assignment is deterministic for a fixed input order.

The splitter produces integer positions. scp.cross_validate resolves sample_dim, validates coordinates and group identities, slices the NDDataset inputs, and fits each fold. Calling split directly does not make the splitter interpret named dimensions or coordinates automatically.

Examples

>>> X = scp.NDDataset([[0.0, 1.0], [1.0, 0.0], [2.0, 1.0], [3.0, 2.0]])
>>> y = scp.NDDataset([[0.0], [1.0], [2.0], [3.0]])
>>> model = scp.PLSRegression(n_components=1)
>>> sample_groups = [0, 0, 1, 1]
>>> splitter = scp.GroupKFold(n_splits=2)
>>> result = scp.cross_validate(
...     model, X, y, cv=splitter, groups=sample_groups
... )

Methods Summary

get_metadata_routing()

Get metadata routing of this object.

get_n_splits([X, y, groups])

Returns the number of splitting iterations as set with the n_splits param when instantiating the cross-validator.

set_split_request(*args, **kw)

Configure whether metadata should be requested to be passed to the split method.

split(X[, y, groups])

Generate indices to split data into training and test set.

Methods Documentation

get_metadata_routing()[source]

Get metadata routing of this object.

Please check User Guide on how the routing mechanism works.

Returns:

routing (MetadataRequest) – A MetadataRequest encapsulating routing information.

get_n_splits(X=None, y=None, groups=None)[source]

Returns the number of splitting iterations as set with the n_splits param when instantiating the cross-validator.

Parameters:
  • X (array-like of shape (n_samples, n_features), default=None) – Always ignored, exists for API compatibility.

  • y (array-like of shape (n_samples,), default=None) – Always ignored, exists for API compatibility.

  • groups (array-like of shape (n_samples,), default=None) – Always ignored, exists for API compatibility.

Returns:

n_splits (int) – Returns the number of splitting iterations in the cross-validator.

set_split_request(*args, **kw)[source]

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.

Added in version 1.3.

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)[source]

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,)) – Group labels for the samples used while splitting the dataset into train/test set.

Yields:
  • train (ndarray) – The training set indices for that split.

  • test (ndarray) – The testing set indices for that split.

Examples using spectrochempy.GroupKFold