API Reference / evaluation / splitting /

leave_one_out.py

Leave-One-Out and Leave-P-Out cross-validation splitters.

Classes

LeaveOneOut

class evaluation.splitting.leave_one_out.LeaveOneOut(BaseSplitter)

Leave-One-Out cross-validation.

Each sample is used once as test while remaining samples form training set.

Notes

This can be very slow for large datasets as it creates n splits.
python
>>> from tuiml.evaluation.splitting import LeaveOneOut
>>> import numpy as np
>>> X = np.arange(5).reshape(-1, 1)
>>> loo = LeaveOneOut()
>>> for train_idx, test_idx in loo.split(X):
...     print(f"Train: {train_idx}, Test: {test_idx}")
Train: [1 2 3 4], Test: [0]
Train: [0 2 3 4], Test: [1]
Train: [0 1 3 4], Test: [2]
Train: [0 1 2 4], Test: [3]
Train: [0 1 2 3], Test: [4]

Methods

get_parameter_schema (cls) -> dict

Return JSON Schema for parameters.

split (self, X: np.ndarray, y: Optional[np.ndarray]=None, groups: Optional[np.ndarray]=None) -> Iterator[Tuple[np.ndarray, np.ndarray]]

Generate LOO indices.

get_n_splits (self, X: Optional[np.ndarray]=None, y: Optional[np.ndarray]=None, groups: Optional[np.ndarray]=None) -> int

Get number of splits (equals n_samples).

__repr__ (self) -> str

Return a reproducible string form of the splitter.

Returns
repr_str
str
Constructor-style representation, "LeaveOneOut()".

LeavePOut

class evaluation.splitting.leave_one_out.LeavePOut(BaseSplitter)

Leave-P-Out cross-validation.

P samples are used as test while remaining samples form training set. Generates all possible combinations.
Constructor
__init__(
    self,
    p: int = 2,
)

Parameters

p
int
Size of test set.

Notes

Number of splits is C(n, p) = n! / (p! * (n-p)!) This grows very fast and can be impractical for large n or p.
python
>>> from tuiml.evaluation.splitting import LeavePOut
>>> import numpy as np
>>> X = np.arange(5).reshape(-1, 1)
>>> lpo = LeavePOut(p=2)
>>> for train_idx, test_idx in lpo.split(X):
...     print(f"Train: {train_idx}, Test: {test_idx}")
Train: [2 3 4], Test: [0 1]
Train: [1 3 4], Test: [0 2]
Train: [1 2 4], Test: [0 3]
Train: [1 2 3], Test: [0 4]
Train: [0 3 4], Test: [1 2]
Train: [0 2 4], Test: [1 3]
Train: [0 2 3], Test: [1 4]
Train: [0 1 4], Test: [2 3]
Train: [0 1 3], Test: [2 4]
Train: [0 1 2], Test: [3 4]

Methods

get_parameter_schema (cls) -> dict

Return JSON Schema for parameters.

split (self, X: np.ndarray, y: Optional[np.ndarray]=None, groups: Optional[np.ndarray]=None) -> Iterator[Tuple[np.ndarray, np.ndarray]]

Generate LPO indices.

get_n_splits (self, X: Optional[np.ndarray]=None, y: Optional[np.ndarray]=None, groups: Optional[np.ndarray]=None) -> int

Get number of splits C(n, p).

__repr__ (self) -> str

Return a reproducible string form of the splitter.

Returns
repr_str
str
Constructor-style representation, e.g. LeavePOut(p=2).