After using cross_validation.KFold(n, n_folds=folds) I would like to access the indexes for training and testing of single fold, instead of going through all the folds.
So let's take the example code:
from sklearn import cross_validation
X = np.array([[1, 2], [3, 4], [1, 2], [3, 4]])
y = np.array([1, 2, 3, 4])
kf = cross_validation.KFold(4, n_folds=2)
sklearn.cross_validation.KFold(n=4, n_folds=2, shuffle=False,
>>> for train_index, test_index in kf:
train_index, test_index in kf
>>> train_index, test_index in kf
>>> print("TRAIN:", train_index, "TEST:", test_index)
TRAIN: [2 3] TEST: [0 1]
You are on the right track. All you need to do now is:
kf = cross_validation.KFold(4, n_folds=2) mylist = list(kf) train, test = mylist
kf is actually a generator, which doesn't compute the train-test split until it is needed. This improves memory usage, as you are not storing items you don't need. Making a list of the
KFold object forces it to make all values available.