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_validateExecute supervised cross-validation.
KFoldSplit observations without group constraints.
sklearn.model_selection.GroupKFoldUnderlying 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_validateresolvessample_dim, validates coordinates and group identities, slices theNDDatasetinputs, and fits each fold. Callingsplitdirectly 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 of this object.
get_n_splits([X, y, groups])Returns the number of splitting iterations as set with the
n_splitsparam when instantiating the cross-validator.set_split_request(*args, **kw)Configure whether metadata should be requested to be passed to the
splitmethod.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
MetadataRequestencapsulating routing information.
- get_n_splits(X=None, y=None, groups=None)[source]
Returns the number of splitting iterations as set with the
n_splitsparam 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
splitmethod.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(seesklearn.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 tosplitif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it tosplit.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
groupsparameter insplit.- 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_samplesis the number of samples andn_featuresis 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