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
shuffleis True,random_stateaffects 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]
- get_n_splits(X=None, y=None)[source]#
Returns the number of splitting iterations in the cross-validator.
- Parameters:
- Xobject
Always ignored, exists for compatibility.
- yobject
Always ignored, exists for compatibility.
- Returns:
- n_splitsint
Returns the number of splitting iterations in the cross-validator.
- split(X, y=None)[source]#
Generate indices to split data into training and test set.
- Parameters:
- Xarray-like of shape (n_samples, n_features)
Training data, where
n_samplesis the number of samples andn_featuresis the number of features.- yarray-like of shape (n_samples,), default=None
The target variable for supervised learning problems.
- Yields:
- trainCuPy ndarray
The training set indices for that split.
- testCuPy ndarray
The testing set indices for that split.