Split data into train and test sets by hand
In machine learning you keep some rows back to test the model (Topic 14.7). Write split(X, y, test_fraction, seed) where X is a 2-D array (one example per row) and y a 1-D array with one label (the right answer) per row of X. Shuffle the row numbers with np.random.default_rng(seed).permutation(len(X)). Put the first round(len(X) * test_fraction) shuffled row numbers in the test set and the rest in the train set.
Return X_train, X_test, y_train, y_test. The same seed must always give the same split, and each row of X must stay paired with its own label. For 10 rows and test_fraction 0.3 the example prints (7, 2) (3, 2).
What Check tests
- The sizes are 7 train rows and 3 test rows
- No row is in both sets, and none is lost
- Each row stays with its own label
- The same seed gives the same split
- The test size is round(len(X) * test_fraction)
- X and y are not shuffled in place
- A different seed gives a different split
Stuck? One hint at a time
The solution unlocks after your first Check. Try first: that is where the learning happens.
Output
Press Run to see what your program prints.