F1 score computation when target labels contain only a subset of the allowed labels

Viewed 37

I have a 3-class classification problem.
My predicted labels consist of all three classes.
However, my target labels have only 2 of the three classes present.
For example:

predicted = [1,1,2,3,2,1]
target = [1,1,2,2,2,1]

How should I go about computing the F1 score in this case?
I'm currently using sklearn's f1_score function with macro average.
But this leads to a low F1 score value for cases like above.

1 Answers

f1_score offers a parameter called labels that let's you define a set of labels to include in case that average != 'binary'.

For example, if you are only interested in the performance of your classifier for the classes 1 and 2, you can do:

from sklearn.metrics import f1_score


predicted = [1, 1, 2, 3, 2, 1]
target = [1, 1, 2, 2, 2, 1]

print(f1_score(target, predicted, average='macro', labels=[1, 2]))
# 0.9
Related