RepeatedKFold#
- class mvpy.crossvalidation.RepeatedKFold(n_splits: int = 5, n_repeats: int = 1, random_state: int | Generator | Generator | None = None)[source]#
Implements a repeated k-folds cross-validator.
In principle, this class is redundant with sklearn.model_selection.RepeatedKFold. However, for the torch backend, this class is useful because it automatically creates indices on the desired device.
- Parameters:
- n_splitsint, default=5
Number of splits to use.
- n_repeatsint, default=1
Number of repeats to use.
- random_stateOptional[Union[int, np.random._generator.Generator, torch._C.Generator]], default=None
Random state to use for shuffling (either integer seed or numpy/torch generator), if any.
- Attributes:
- n_splitsint, default=5
Number of splits to use.
- n_repeatsint, default=1
Number of repeats to use.
- random_stateOptional[Union[int, np.random._generator.Generator, torch._C.Generator]], default=None
Random state to use for shuffling (either integer seed or numpy/torch generator), if any.
Notes
For reproducability when using shuffling, you can set the random_state to an integer.
Note also that, when using shuffling, please make sure to instantiate and transform immediately to the backend you would like. Otherwise, each call to split will instantiate a new object with the same random seed.
Examples
>>> import torch >>> from mvpy.crossvalidation import RepeatedKFold >>> X = torch.arange(6) >>> kf = RepeatedKFold(n_splits = 2, n_repeats = 2).to_torch() >>> for f_i, (train, test) in enumerate(kf.split(X)): >>> print(f'Fold{f_i}: train={train} test={test}') Fold0: train=tensor([4, 1, 2]) test=tensor([0, 5, 3]) Fold1: train=tensor([0, 5, 3]) test=tensor([4, 1, 2]) Fold2: train=tensor([2, 1, 3]) test=tensor([5, 0, 4]) Fold3: train=tensor([5, 0, 4]) test=tensor([2, 1, 3])
- split(X: ndarray | Tensor, y: ndarray | Tensor | None = None) Generator[tuple[ndarray, ndarray], None, None] | Generator[tuple[Tensor, Tensor], None, None][source]#
Repeatedly split the dataset into iterable (train, test).
- Parameters:
- XUnion[np.ndarray, torch.Tensor]
Input data of shape (n_samples, …)
- yOptional[Union[np.ndarray, torch.Tensor]], default=None
Target data of shape (n_samples, …). Unused, but parameter available for consistency.
- Returns:
- kfUnion[collections.abc.Generator[tuple[np.ndarray, np.ndarray], None, None], collections.abc.Generator[tuple[torch.Tensor, torch.Tensor], None, None]]
Iterable generator of (train, test) pairs.