
A document classifier can look excellent on a folder containing mainly invoices by predicting invoice for everything. An editor still needs reliable recall for the rarer receipts. Averaging recall per class grants the rare class a voice independent of its sample frequency. The change in score is driven by class-level success rates.
Audit the inputs first
The reference labels use zero and one. The example keeps each reference set beside the model output for those same records, in the same order. Shuffling one array independently quietly changes which examples are treated as mistakes. In each input the first list holds ground truth and the second holds predictions or scores.
from sklearn.metrics import balanced_accuracy_score
def evaluate(v):
return balanced_accuracy_score(*v)
for sample in [[[0, 0, 0, 1], [0, 0, 0, 0]], [[0, 0, 0, 1], [0, 1, 0, 1]], [[0, 1], [0, 1]]]:
print(evaluate(sample))Check the operating boundary
| Example input | Calculated output |
|---|---|
| [[0, 0, 0, 1], [0, 0, 0, 0]] | 0.5 |
| [[0, 0, 0, 1], [0, 1, 0, 1]] | 0.83333 |
| [[0, 1], [0, 1]] | 1.0 |
For [[0, 0, 0, 1], [0, 0, 0, 0]], the observed output is 0.5. With [[0, 0, 0, 1], [0, 1, 0, 1]], it becomes 0.83333. These are useful regression fixtures because an integration test can send both through the same entry point. A test of only the first case can stay green after a change that affects the other.
The value for [[0, 1], [0, 1]] is 1.0. Record the unit or indexing convention next to that expectation. A second implementation can return the same type and still disagree about where a boundary falls or which observations enter the calculation.
The second case changes the reported quantity by 0.33333 compared with the first. That is an absolute difference in the reported scale. Calling it a relative percent change would require dividing by an appropriate starting value and handling a baseline of zero.
Where this calculation stops
A small minority sample brings substantial uncertainty. Keep its sample count visible when comparing two nightly evaluations.
For a repeatable comparison, retain one counterexample as well as one ordinary success. In this fixture, [[0, 0, 0, 1], [0, 1, 0, 1]] makes a useful starting point: its reference result is 0.83333. A changed result is a prompt to inspect the inputs, dependency version and chosen policy before accepting a new baseline.
A reproduction of balanced accuracy should keep the label source with the case. Here the original first input is [[0, 0, 0, 1], [0, 0, 0, 0]]. In a production note, include the reviewer or instrument that supplied the reference labels. This makes it possible to tell a code regression apart from a revised annotation rubric when the same evaluation is run again.
scikit-learn balanced accuracy score · Python numeric types
Sources and further reading
Cover photograph: Usbkey internals edit by John Fader. CC BY-SA 3.0. Cropped and typeset for this cover. The photograph illustrates the topic. This image adaptation uses the same share-alike license.
