Browse and solve 1,200+ machine learning coding challenges on Deep-ML. Filter by difficulty (beginner, intermediate, advanced) and category (linear algebra, machine learning, deep learning, NLP, computer vision). Write Python solutions in the browser and get instant feedback against test cases.
With 10 samples and k=5, we create 5 folds of 2 samples each. In each iteration, one fold becomes the test set (2 samples) and the remaining 4 folds become the training set (8 samples). Every sample appears in the test set exactly once across all iterations.
Implement a function to generate train and test index splits for K-Fold Cross-Validation. Your task is to divide dataset indices into k folds and return a list of train-test index pairs for each fold.
Input:
n_samples: The total number of samples in the dataset (integer)
k: The number of folds (default 5)
shuffle: Whether to shuffle indices before splitting (default True)
Output:
A list of k tuples, where each tuple contains (train_indices, test_indices) as Python lists of integers
Requirements:
Split the indices into k roughly equal folds. If n_samples is not divisible by k, distribute the extra samples to the first folds (e.g., with 10 samples and k=3, fold sizes would be [4, 3, 3]).
For each fold iteration, use that fold as the test set and combine all other folds as the training set.
When shuffle=True, use np.random.shuffle() to randomize indices before splitting (the random seed will be set externally before calling your function).
When shuffle=False, keep indices in their original order [0, 1, 2, ..., n_samples-1].