Suppose 5 percent of your examples are positive. A random split can, by chance, put almost all of them in one side, and then your test set says little about how the model handles the rare class. A stratified split fixes the class proportions on both sides: each class is split separately, so the test set keeps the same mix as the full dataset.
Implement stratified_train_test_split(y, test_fraction=0.25, seed=0), which returns (train_indices, test_indices) as sorted integer arrays. Each class is shuffled with a seeded generator and round(test_fraction * class_count) of its members go to the test set.
round(test_fraction * class_count).test_fraction must lie in [0, 1], otherwise raise ValueError.seed gives the same split.Open one at a time. Each gives away a little more than the last.
np.flatnonzero(y == label) gives the indices of one class. Shuffle those indices with rng.shuffle.
The first n_test shuffled indices go to test, and the rest go to train. Concatenate the pieces across classes and sort at the end.
No results yet
Run your code to check it against the examples, or submit to run every test.