
A small labelled corpus must yield a test sample that contains examples of both categories. Random selection without attention to the class mix can produce a nearly useless tiny test fold. The output is the class count in each split. Stratification addresses proportions; it does not make related examples independent.
Audit the inputs first
The small fixtures expose what reaches an estimator after preparation. In deployment, version the transformer, its learned parameters and the feature-column order with the trained model. Recreating these settings from memory can produce a numerically valid but incompatible input.
import numpy as np
from sklearn.model_selection import train_test_split
def evaluate(v):
return [np.bincount(np.array(v)[ix],minlength=2).tolist() for ix in train_test_split(np.arange(len(v)),test_size=0.5,random_state=7,stratify=v)]
for sample in [[0, 0, 0, 0, 1, 1, 1, 1], [0, 0, 0, 0, 1, 1], [0, 0, 0, 1, 1, 1]]:
print(evaluate(sample))Check the operating boundary
| Example input | Calculated output |
|---|---|
| [0, 0, 0, 0, 1, 1, 1, 1] | [[2, 2], [2, 2]] |
| [0, 0, 0, 0, 1, 1] | [[2, 1], [2, 1]] |
| [0, 0, 0, 1, 1, 1] | [[2, 1], [1, 2]] |
For [0, 0, 0, 0, 1, 1, 1, 1], the observed output is [[2, 2], [2, 2]]. With [0, 0, 0, 0, 1, 1], it becomes [[2, 1], [2, 1]]. The useful comparison is at the application boundary. Run the pair against the input as it reaches the program after decoding or parsing, then inspect what the next component receives.
For [0, 0, 0, 1, 1, 1], the operation returns 2 top-level items. An assertion on that count would miss a change in their contents. Capture the expected sequence or compare membership explicitly if order has no meaning in the product.
Keep the shape of these outputs with the recorded result. A list used as a feature vector needs its column order, while a pair of measurements needs a name for each component. A downstream dashboard cannot reconstruct that meaning from a sequence of unlabelled numbers.
Where this calculation stops
If several inputs belong to a shared person or recording, group separation may be more important than a near-perfect match in class proportions.
For a repeatable comparison, retain one counterexample as well as one ordinary success. In this fixture, [0, 0, 0, 0, 1, 1] makes a useful starting point: its reference result is [[2, 1], [2, 1]]. A changed result is a prompt to inspect the inputs, dependency version and chosen policy before accepting a new baseline.
A reproduction of stratified holdout should keep the fitted transformation with the case. Here the original first input is [0, 0, 0, 0, 1, 1, 1, 1]. In a production note, include the exact training rows that established the transformation or split. This makes it possible to tell a code regression apart from a new category or measurement range in the incoming data when the same evaluation is run again.
scikit-learn cross-validation guide · Python numeric types
Sources and further reading
Cover photograph: Cherry MX 3850 USB - case removed-3537 by Raimond Spekking. CC BY-SA 4.0. Cropped and typeset for this cover. The photograph illustrates the topic. This image adaptation uses the same share-alike license.
