A single train/test split can be lucky or unlucky. K-fold cross-validation splits the data into folds and evaluates times, each time holding out a different fold.
Implement kfold_indices(n, k, shuffle=False, seed=0) returning a list of k (train_idx, val_idx) pairs of NumPy integer arrays.
Rules (these match scikit-learn's KFold)
0 … n-1. If shuffle is true, use np.random.default_rng(seed).permutation(n) instead.n // k, with the first n % k folds getting one extra element.Example
kfold_indices(7, 3)
# [([3, 4, 5, 6], [0, 1, 2]), ([0, 1, 2, 5, 6], [3, 4]), ([0, 1, 2, 3, 4], [5, 6])]