ML Foundations

Check missing classes before reporting balanced accuracy

Inspect class support before interpreting a balanced accuracy score on a test split.

Example results for Check missing classes before reporting balanced accuracy. labels: 0, 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.

A missing class cannot have measured recall

Balanced accuracy is the average of class recalls. Recall for a class is true positives divided by the number of actual examples of that class. When a test set contains no examples of a class, its recall has a zero denominator: that split supplies no evidence about performance on that class. [1, 2]

A reported score can look perfect even though one intended class was never tested. Record the support of every expected class and the label set with the score; a single number cannot reveal whether all intended classes were represented.

References: [1], [2]

Inspect the support before the score

Run python examples/balanced_accuracy_missing_class.py. The example expects classes 0 and 1, but its four true labels are all 0. All four predictions are correct, so the recall for class 0 is 1.0; class 1 has no support and is shown as None. The mean over observed classes is 1.0, while the explicit coverage check fails.

This is a small NumPy calculation, not a call to scikit-learn. A separate check with scikit-learn 1.8.0 on the same fixture returned 1.0. That result does not establish recall for the missing class. [1, 2]

Executed with local Python and NumPy; stdout and checksum are stored in private evidence.

import numpy as np

truth = np.array([0, 0, 0, 0])
pred = np.array([0, 0, 0, 0])
labels = [0, 1]
support = [int(np.sum(truth == label)) for label in labels]
recall = [float(np.mean(pred[truth == label] == label)) if count else None
          for label, count in zip(labels, support)]
observed_mean = sum(value for value in recall if value is not None) / sum(value is not None for value in recall)
assert support == [4, 0] and recall == [1.0, None]
assert observed_mean == 1.0
print("labels:", labels)
print("support:", support)
print("per-class recall:", recall)
print("mean over observed classes:", observed_mean)
print("complete class coverage:", all(count > 0 for count in support))

Recorded output

labels: [0, 1]
support: [4, 0]
per-class recall: [1.0, None]
mean over observed classes: 1.0
complete class coverage: False

References: [1], [2]

Choose a reporting policy

Report the support table and mark a missing class as unmeasured, rather than silently interpreting its recall as perfect or zero. If each class must be assessed, obtain a test set with examples of every required class and keep the test set separate from model fitting. [2]

This example concerns a single-label, unweighted classification split and the default unadjusted score. It does not establish model quality, confidence intervals, or behaviour for every scikit-learn version and metric option.

References: [2]

Sources

  1. scikit-learn balanced accuracy score
  2. scikit-learn recall score