Multilabel Precision

In a multilabel problem a sample can carry several labels at once โ€” an image tagged {dog, outdoor, animal}. Precision asks the same question it always does, now over label assignments rather than samples: of the labels the model predicted, how many were right?

With true labels {dog, animal} and predicted {dog, outdoor}, one prediction is correct and one is not, so the precision for that sample is \(1/2\).

Four ways to average, and what separates them

import numpy as np
from sklearn.metrics import precision_score, recall_score

truth = np.array([[1, 1, 0], [0, 1, 0], [1, 0, 1], [0, 0, 1]])
pred = np.array([[1, 1, 1], [0, 1, 1], [1, 1, 1], [0, 0, 0]])
"""Four images, three possible tags. The model is generous on the first three
   and predicts nothing at all for the fourth."""

for scheme in ("micro", "macro", "weighted", "samples"):
    p = precision_score(truth, pred, average=scheme, zero_division=0)
    r = recall_score(truth, pred, average=scheme, zero_division=0)
    print(f"{scheme:9} precision {p:.4f}  recall {r:.4f}")
print(f"true labels {int(truth.sum())}  predicted labels {int(pred.sum())}  correct {int((truth & pred).sum())}")
for zero_division in (0, 1):
    v = precision_score(truth, pred, average="samples", zero_division=zero_division)
    print(f"samples average, zero_division={zero_division}  {v:.4f}")
# micro     precision 0.6250  recall 0.8333
# macro     precision 0.6667  recall 0.8333
# weighted  precision 0.6667  recall 0.8333
# samples   precision 0.4583  recall 0.7500
# true labels 6  predicted labels 8  correct 5
# samples average, zero_division=0  0.4583
# samples average, zero_division=1  0.7083
SchemeWhat it averages over
MicroPools TP and predicted-positive counts. Equivalently, sample precisions with predictions are weighted by the number of labels predicted for that sample, not its true-label count
MacroPrecision per label, then an unweighted mean. A rare tag counts as much as a common one
WeightedPrecision per label, weighted by how often that label occurs โ€” which means it follows the common tags rather than accounting for the rare ones
SamplesPrecision within each sample, then a mean over samples. Only defined for multilabel problems, and the one that matches “how good are the tags on a typical image”

Four images and three possible tags give 0.6250, 0.6667, 0.6667 and 0.4583. The samples average is the outlier, and that is the scheme working as intended: it is the only one that treats each image as one unit rather than counting label decisions.

Micro precision and micro recall come apart here

This is the substantive difference from multiclass precision. In a single-label problem every mistake is a false positive for one class and a false negative for another, so \(\sum FP = \sum FN\) and micro precision, micro recall and micro F1 are all the same number โ€” and all equal accuracy.

That balance is an artifact of one prediction per sample. Here the model predicts 8 labels where 6 are true, getting 5 right, so the two totals differ: micro precision is 0.6250 and micro recall is 0.8333. Both numbers now carry information, and the gap is the direction of the error โ€” this model over-tags.

So micro averaging earns its place in multilabel work in a way it does not in single-label work, where it is accuracy under another name.

The sample with no predictions

The fourth image in the example receives no predicted labels at all, which leaves its precision as \(0/0\). The last two lines show what that costs: the samples average is 0.4583 or 0.7083 depending on whether zero_division is 0 or 1, a gap of a quarter driven by one image out of four.

Neither convention is obviously right. Scoring it 0 punishes silence as if it were a wrong tag; scoring it 1 says a model that tags nothing is perfect on that image. Which to use depends on whether an untagged sample is a failure in your system, and the choice has to be stated because it is not recoverable from the number.

Report multilabel precision with its averaging scheme, its zero_division convention, the matching recall, and โ€” for the samples average โ€” how many samples received no prediction at all.

References


Discover more from Insightful Data Lab

Subscribe to get the latest posts sent to your email.

Similar Posts

Questions, corrections, or additional insights?

This site uses Akismet to reduce spam. Learn how your comment data is processed.