spectrochempy.KFold
- class KFold(n_splits=5, *, shuffle=False, random_state=None)[source]
Split observations into consecutive cross-validation folds.
This is a thin SpectroChemPy adaptation of
sklearn.model_selection.KFold. It uses scikit-learn’s partitioning algorithm unchanged while providing documentation and an explicit public signature for use withspectrochempy.cross_validate.- Parameters:
n_splits (int, optional, default: 5) – Number of folds. Must be at least 2 and no greater than the number of observations.
shuffle (bool, optional, default: False) – Shuffle observation positions before dividing them into folds. The observations within each resulting fold are not shuffled.
random_state (int, RandomState instance or None, optional, default: None) – Controls the ordering when shuffle is true. Pass an integer to obtain reproducible folds. It has no effect when shuffle is false, and scikit-learn rejects a non-None value in that case.
See also
cross_validateExecute supervised cross-validation.
GroupKFoldKeep groups separated between folds.
LeaveOneOutValidate one observation at a time.
sklearn.model_selection.KFoldUnderlying implementation.
Notes
The splitter produces integer positions.
scp.cross_validateresolvessample_dim, validates coordinates, 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) >>> splitter = scp.KFold(n_splits=2, shuffle=True, random_state=7) >>> result = scp.cross_validate(model, X, y, cv=splitter)
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.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.
- 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,), default=None) – Always ignored, exists for API compatibility.
- Yields:
train (ndarray) – The training set indices for that split.
test (ndarray) – The testing set indices for that split.
Examples using spectrochempy.KFold