ML Foundations

Keep negative count differences with Python Counter

Choose Counter subtraction or the subtract method according to whether signed differences must survive.

Example results for Keep negative count differences with Python Counter. minus operator: {'dog': 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.

Two subtraction operations have different contracts

A Counter is a dictionary subclass for counts of hashable items. Python supports signed counts, but its multiset subtraction operator returns only results with positive counts. A negative difference can therefore disappear from the result even when it is meaningful to your application. [1]

For example, suppose expected and observed label counts come from two batches. An observed count above the expected count produces a negative expected-minus-observed difference. If the goal is to inspect both excesses and shortfalls, a positive-only result is incomplete.

References: [1]

Copy and subtract to retain the signs

Run python examples/counter_signed_difference.py. The fixture compares expected counts for cats and dogs with observed counts for cats and birds. The minus operator returns only the positive dog difference; it drops the negative cat and bird differences. [1]

The example copies the expected Counter and calls its subtract method with the observed counts. Subtract updates that copy in place and permits negative and zero counts. The resulting signed differences are minus one for bird, minus one for cat and plus one for dog. The original expected Counter is left unchanged. [2]

The program sorts keys only to make printed output easy to inspect and asserts both results. A downstream reconciliation step can now identify the negative entries as labels observed more often than expected. The direction of subtraction determines what a negative sign means.

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

from collections import Counter

expected = Counter(cat=2, dog=1)
observed = Counter(cat=3, bird=1)
positive_only = expected - observed
signed = expected.copy()
signed.subtract(observed)
print('minus operator:', dict(sorted(positive_only.items())))
print('signed difference:', dict(sorted(signed.items())))
print('negative labels:', sorted(k for k, v in signed.items() if v < 0))
assert dict(positive_only) == {'dog': 1}
assert dict(signed) == {'cat': -1, 'dog': 1, 'bird': -1}

Recorded output

minus operator: {'dog': 1}
signed difference: {'bird': -1, 'cat': -1, 'dog': 1}
negative labels: ['bird', 'cat']

References: [1], [2]

Decide what zero and missing keys mean

The subtract method can retain zero counts as well as negative ones. Decide whether a zero key should remain in your report or be removed explicitly. Accessing a missing Counter key returns zero, but that does not establish that the key was present in either source. [1, 2]

The example uses small integer counts and string labels. It does not address concurrent updates, streaming reconciliation or arbitrary numeric values. Use the minus operator when positive multiset subtraction is what you intend; use a signed representation when deficits matter.

References: [1], [2]

Sources

  1. Python Counter objects
  2. Python Counter.subtract