Stratified Train/Test Split From Scratch
Problem statement
You are building an evaluation pipeline for a fraud classifier where only a few percent of rows are positive. A plain random split can leave the test set with noticeably more or fewer positives than the full data, so you want a stratified split: every class keeps (as closely as integer counts allow) the same share in the test set as in the data. Implement it without scikit-learn.
To make the output unique, the shuffle is passed in: order is a random permutation of all row indices.
def stratified_split(labels: list[int], n_test: int, order: list[int]) -> list[list[int]]:
...
- How many test rows per class. Class $c$ has $n_c$ of the $n$ rows, so its exact quota is $q_c = n_{test} \cdot n_c / n$. Give each class $\lfloor q_c \rfloor$ test rows, then hand the remaining slots one each to the classes with the largest fractional parts of $q_c$, breaking ties by the smaller class label. The test set has exactly
n_testrows. - Which rows. Class $c$'s test rows are the first $k_c$ indices of class $c$ in the sequence
order. - Return
[train, test], both sorted ascending; together they partitionrange(n).
Constraints
- $2 \le n \le 1000$, $1 \le$
n_test$\le n - 1$. - Labels are integers (possibly negative); a class may have a single row.
orderis a permutation ofrange(n).
Examples
Example 1: labels = [0, 0, 1, 0, 1, 1], n_test = 3, order = [0, 1, 2, 3, 4, 5] returns [[3, 4, 5], [0, 1, 2]]. Both quotas are 1.5; the floors give 1 + 1 rows and the leftover slot goes to class 0 (tie, smaller label). Rounding each quota separately would produce 4 test rows, and flooring would produce 2.
Example 2: labels = [1, 0, 0, 0, 1, 0, 0, 0, 0, 0], n_test = 3, order = [9, 4, 2, 0, 7, 1, 5, 3, 8, 6] returns [[0, 1, 3, 5, 6, 7, 8], [2, 4, 9]]. Quotas are 2.4 for class 0 and 0.6 for class 1, so class 1 gets the leftover slot. Class 0 appears in the shuffle as 9, 2, ... and class 1 first appears as 4.
Example 3: labels = [5, 5, 5, 5], n_test = 1, order = [3, 1, 0, 2] returns [[0, 1, 2], [3]].
Show hint
What does it mean to sample inside each class rather than across the whole dataset, and how does the given shuffle fit in?
Relevant roles: ML Engineer, Data Scientist and Applied Scientist.
Related free problems
- Multi-Head AttentionMedium
- Masked Self-AttentionMedium