sklearn.cross_validation.KFold

Warning

DEPRECATED

class sklearn.cross_validation.KFold(n, n_folds=3, shuffle=False, random_state=None)[source]

K-Folds cross validation iterator.

Deprecated since version 0.18: This module will be removed in 0.20. Use sklearn.model_selection.KFold instead.

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 as a validation set once while the k - 1 remaining fold(s) form the training set.

Read more in the User Guide.

Parameters:

n : int

Total number of elements.

n_folds : int, default=3

Number of folds. Must be at least 2.

shuffle : boolean, optional

Whether to shuffle the data before splitting into batches.

random_state : None, int or RandomState

When shuffle=True, pseudo-random number generator state used for shuffling. If None, use default numpy RNG for shuffling.

See also

StratifiedKFold, folds, classification

LabelKFold
K-fold iterator variant with non-overlapping labels.

Notes

The first n % n_folds folds have size n // n_folds + 1, other folds have size n // n_folds.

Examples

>>> from sklearn.cross_validation import KFold
>>> X = np.array([[1, 2], [3, 4], [1, 2], [3, 4]])
>>> y = np.array([1, 2, 3, 4])
>>> kf = KFold(4, n_folds=2)
>>> len(kf)
2
>>> print(kf)  
sklearn.cross_validation.KFold(n=4, n_folds=2, shuffle=False,
                               random_state=None)
>>> for train_index, test_index in kf:
...    print("TRAIN:", train_index, "TEST:", test_index)
...    X_train, X_test = X[train_index], X[test_index]
...    y_train, y_test = y[train_index], y[test_index]
TRAIN: [2 3] TEST: [0 1]
TRAIN: [0 1] TEST: [2 3]
.. automethod:: __init__