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 with spectrochempy.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_validate

Execute supervised cross-validation.

GroupKFold

Keep groups separated between folds.

LeaveOneOut

Validate one observation at a time.

sklearn.model_selection.KFold

Underlying implementation.

Notes

The splitter produces integer positions. scp.cross_validate resolves sample_dim, validates coordinates, 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)
>>> 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()

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.

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.

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,), 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

Cross-validation of corn moisture models

Cross-validation of corn moisture models