ML Foundations

Check singleton classes before a stratified split

Count class labels before stratifying and distinguish a necessary count check from a valid evaluation plan.

Example results for Check singleton classes before a stratified split. class counts: (0, 6), (1, 1)
Illustration: Original illustration by Tensor Almanac
Evidence and testing

Review record: Tensor Almanac automated evidence gate (no independent human review). Example versions, recorded outputs and limitations appear below.

One observation cannot populate both partitions

A stratified train and test split uses supplied class labels to allocate observations across partitions. If a class has only one observation, both partitions cannot each contain an independent observation from that class. Counting labels before splitting exposes this constraint. [1, 2]

This guide checks the observed class counts. The example is a count check, and does not perform a split or prove that a chosen split will succeed. [3]

References: [1], [2], [3]

Count and identify singleton labels

Run python examples/stratified_class_counts.py. The fixture contains six labels of class zero and one of class one. Counter records the frequency of each observed label; the count check prints class one as a singleton. The empty-label case also makes the final check false. [3]

This count condition is necessary if every observed class must appear in both disjoint partitions, but it is not sufficient for every test size or allocation. Check the resulting partitions and their class counts for the chosen split. [1, 2]

Executed with Python 3.12.14; stdout and checksum recorded in private evidence.

from collections import Counter

labels = [0, 0, 0, 0, 0, 0, 1]
counts = Counter(labels)
singletons = sorted(label for label, count in counts.items() if count == 1)
print('class counts:', sorted(counts.items()))
print('singleton classes:', singletons)
print('every observed class has at least two:', bool(counts) and not singletons)
assert singletons == [1]

Recorded output

class counts: [(0, 6), (1, 1)]
singleton classes: [1]
every observed class has at least two: False

References: [1], [2], [3]

Address the constraint in the data

If a class cannot be adequately represented, collect more independent observations where practical or report the evaluation limitation. Duplicating the singleton into both partitions creates repeated evidence and can distort evaluation.

Split design may also need to respect entity groups or time order. This count check does not check those constraints, missing labels, absent expected classes or whether the requested train and test sizes are feasible. Stratification alone does not settle those choices. [1, 2]

The check uses the labels actually supplied to it. A desired class that is absent altogether will not appear as a singleton in the counter, so compare against an independently specified class list if that distinction matters.

References: [1], [2]

Sources

  1. scikit-learn train_test_split
  2. scikit-learn StratifiedShuffleSplit
  3. Python Counter