K-Fold Cross-Validation Splits
Problem statement
Implement a function that generates train and test index splits for K-fold cross-validation. Divide the dataset indices into k folds and return one (train, test) pair per fold.
Input
n_samples: total number of samples (integer).k: number of folds (default 5).shuffle: whether to shuffle the indices before splitting (defaultTrue).
Output
- A list of
ktuples(train_indices, test_indices), each a Python list of integers.
Requirements
- Split the indices into
kroughly equal folds. Ifn_samplesis not divisible byk, give the extra samples to the first folds (for example, 10 samples withk=3gives fold sizes[4, 3, 3]). - In iteration
i, foldiis the test set and all other folds, concatenated in fold order, form the training set. - When
shuffle=True, callnp.random.shuffle()once on the index array[0, 1, ..., n_samples-1]before splitting (the seed is set by the caller). - When
shuffle=False, keep the original order[0, 1, ..., n_samples-1].
Example
Input:
k_fold_cross_validation(n_samples=10, k=5, shuffle=False)
Output:
[([2, 3, 4, 5, 6, 7, 8, 9], [0, 1]), ([0, 1, 4, 5, 6, 7, 8, 9], [2, 3]), ([0, 1, 2, 3, 6, 7, 8, 9], [4, 5]), ([0, 1, 2, 3, 4, 5, 8, 9], [6, 7]), ([0, 1, 2, 3, 4, 5, 6, 7], [8, 9])]
Reasoning: 10 samples split into 5 folds of 2. Each fold is the test set exactly once while the other 4 folds (8 samples) train the model.
Show hint
What should the test set look like if every sample must be tested exactly once across the k iterations?
Relevant roles: ML Engineer, Data Scientist, Applied Scientist and AI Engineer.
Related free problems
- Multi-Head AttentionMedium
- Masked Self-AttentionMedium