KFold#

class cuml.model_selection.KFold(n_splits=5, *, shuffle=False, random_state=None)[source]#

K-Folds cross-validator.

Provides train/test indices to split data in train/test sets. Split dataset into k consecutive folds (without shuffling by default).

Each fold is then used once as a validation set while the k - 1 remaining folds form the training set.

Parameters:
n_splitsint, default=5

Number of folds. Must be at least 2.

shufflebool, default=False

Whether to shuffle the samples before splitting. Note that the samples within each split will not be shuffled.

random_stateint, CuPy RandomState, NumPy RandomState, or None, default=None

When shuffle is True, random_state affects the ordering of the indices, which controls the randomness of each fold. Otherwise, this parameter has no effect. Pass an int for reproducible output across multiple function calls.

Examples

>>> import cupy as cp
>>> from cuml.model_selection import KFold
>>> X = cp.array([[1, 2], [3, 4], [1, 2], [3, 4]])
>>> y = cp.array([0, 0, 1, 1])
>>> kf = KFold(n_splits=2)
>>> kf.get_n_splits()
2
>>> for i, (train_index, test_index) in enumerate(kf.split(X, y)):
...     print(f"Fold{i}:")
...     print(f"  Train: index={train_index}")
...     print(f"  Test:  index={test_index}")
Fold 0:
  Train: index=[2 3]
  Test:  index=[0 1]
Fold 1:
  Train: index=[0 1]
  Test:  index=[2 3]