Start learning free

LESSON 88 OF 165 · MACHINE LEARNING: ML INTERVIEW QUESTIONS

Write k-Fold Cross-Validation

Practice

An interviewer asks: "Write k-fold cross-validation without scikit-learn. Given the number of rows n, the number of folds k and a seed, return the train and test row numbers of every fold." The idea in one sentence: shuffle the row numbers once, cut them into k parts, and let each part be the test set once while the other parts train the model.

Four numpy functions do the work. np.random.default_rng(seed) gives back a random generator; the same seed gives the same shuffle on every run. rng.permutation(n) gives back the numbers 0 to n - 1, each once, in a shuffled order. np.array_split(a, k) gives back a list of k arrays of nearly equal size, the first ones one item longer when they can't all be equal. np.concatenate(list_of_arrays) gives back one array with all their items. On other numbers:

pieces = np.array_split(np.array([10, 11, 12, 13, 14]), 2)
print(pieces[0], pieces[1])                    # [10 11 12] [13 14]
print(np.concatenate([pieces[1], pieces[0]]))  # [13 14 10 11 12]

To leave one item out of a list, add the slice before it to the slice after it: with letters = ["a", "b", "c", "d"], letters[:2] + letters[3:] gives back ["a", "b", "d"]. The starting code has kfold(n, k, seed) with the generator rng, an empty list folds and return folds, and prints the folds of kfold(10, 3, 0). When it's done, your code and the check print:

Train: [3 5 9 0 8 1] - test: [4 6 2 7]
Train: [4 6 2 7 0 8 1] - test: [3 5 9]
Train: [4 6 2 7 3 5 9] - test: [0 8 1]
23 rows, k = 5: test sizes [5, 5, 5, 4, 4] - scikit-learn's KFold: [5, 5, 5, 4, 4]
Every row is tested exactly once: True
Each fold's train and test share no row and hold them all: True
Same seed, same folds: True
Another seed, other folds: True
scikit-learn's cross_val_score with your 4 folds: [0.6 0.5 0.9 1. ]

The last line hands your folds to scikit-learn's cross_val_score, which trains a small tree on each fold's train rows and scores it on that fold's test rows.

Your task: fill in kfold. Shuffle the row numbers into order, cut them into k parts, and for each part number i set test to that part and train to all the other parts joined, then append the pair (train, test) to folds.


Assignment
Follow the Coding Tutorial to write kfold, which gives back one (train, test) pair of row numbers per fold.


Hint
10 rows in 3 parts give 4, 3 and 3 row numbers, the same sizes as scikit-learn's KFold. For train, leave part i out: a slice of parts before it plus a slice after it.


STEP 1 OF 6
    Ctrl + Enter to check