6.12.4. scikits.learn.cross_val.StratifiedKFold¶
- class scikits.learn.cross_val.StratifiedKFold(y, k, indices=False)¶
Stratified K-Folds cross validation iterator
Provides train/test indices to split data in train test sets
This cross-validation object is a variation of KFold, which returns stratified folds. The folds are made by preserving the percentage of samples for each class.
- __init__(y, k, indices=False)¶
K-Folds cross validation iterator
Provides train/test indices to split data in train test sets
Parameters : y: array, [n_samples] :
Samples to split in K folds
k: int :
number of folds
indices: boolean, optional (default False) :
Return train/test split with integer indices or boolean mask. Integer indices are useful when dealing with sparse matrices that cannot be indexed by boolean masks.
Notes
All the folds have size trunc(n/k), the last one has the complementary
Examples
>>> from scikits.learn import cross_val >>> X = np.array([[1, 2], [3, 4], [1, 2], [3, 4]]) >>> y = np.array([0, 0, 1, 1]) >>> skf = cross_val.StratifiedKFold(y, k=2) >>> len(skf) 2 >>> print skf scikits.learn.cross_val.StratifiedKFold(labels=[0 0 1 1], k=2) >>> for train_index, test_index in skf: ... 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: [False True False True] TEST: [ True False True False] TRAIN: [ True False True False] TEST: [False True False True]