Before you start
Produce the index splits for k-fold cross validation.
Divide range(n) into k contiguous folds without shuffling. Each fold takes a
turn as the validation set while the other k - 1 folds form the training set.
When n is not divisible by k the folds cannot all be the same size. Use the
standard convention: the first n % k folds get one extra element. For n = 10
and k = 3 that gives sizes 4, 3, 3.
Return a list of k tuples (train_indices, test_indices), each an integer
np.ndarray, with the folds in order and the indices inside each array
ascending.
Cross validation exists because a single train/test split is a noisy estimate of
performance. Averaging over k folds uses every sample for validation exactly
once.
def k_fold_indices(n, k):
Input
k_fold_indices(6, 3)
Output
[(array([2, 3, 4, 5]), array([0, 1])),
(array([0, 1, 4, 5]), array([2, 3])),
(array([0, 1, 2, 3]), array([4, 5]))]
Reasoning
6 splits evenly into 3 folds of 2. Each fold is the test set once, in order, and the other four indices are the training set.
n % k folds get one extra element.Check runs the visible cases against a fresh kernel. Submit runs the hidden ones too.
⇧↵run cell