spectrochempy.LeaveOneOut

class LeaveOneOut[source]

Use each observation once as a one-observation validation fold.

This is a thin SpectroChemPy adaptation of sklearn.model_selection.LeaveOneOut. It uses scikit-learn’s partitioning algorithm unchanged and creates as many fits as there are observations.

See also

cross_validate

Execute supervised cross-validation.

KFold

Select a fixed number of folds.

sklearn.model_selection.LeaveOneOut

Underlying implementation.

Notes

Leave-one-out validation can be expensive because it fits the estimator once per observation. R² is undefined for each one-observation fold; scp.cross_validate records that limitation while its global out-of-fold R² may still be defined when enough valid observations are available.

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.LeaveOneOut()
>>> 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 in 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, y=None, groups=None)[source]

Returns the number of splitting iterations in the cross-validator.

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) – 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.LeaveOneOut