After using cross_validation.KFold (n, n_folds = folds), I would like to access the indices for training and testing a single fold, instead of going through all the folds.
So let's take an 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) >>> print(kf) sklearn.cross_validation.KFold(n=4, n_folds=2, shuffle=False, random_state=None) >>> for train_index, test_index in kf:
I would like to access the first time in kf like this (instead of a loop):
train_index, test_index in kf[0]
This should only return the first fold, but instead I get an error: "TypeError: object" KFold "does not support indexing"
What I want as output:
>>> train_index, test_index in kf[0] >>> print("TRAIN:", train_index, "TEST:", test_index) TRAIN: [2 3] TEST: [0 1]
Link: http://scikit-learn.org/stable/modules/generated/sklearn.cross_validation.KFold.html
Question
How to get indices for training and test for only one fold without going through the entire cycle of the cycle?
python scikit-learn cross-validation
NumesSanguis
source share