Macro F1 Score for Multi-Class Classification
Problem statement
Write a function that computes the Macro F1 score for a multi-class classification problem given lists of true labels and predicted labels.
The Macro F1 score is the unweighted average of the per-class F1 scores. For each class $c$:
- Compute precision and recall using one-vs-rest counts (treat $c$ as positive, all other classes as negative).
- Compute the F1 score for that class.
- If precision and recall are both zero (or true positives and false positives are zero, etc.), define that class's F1 score as 0.
Finally, return the arithmetic mean of the per-class F1 scores. The set of classes should be the union of labels appearing in either y_true or y_pred.
Inputs:
y_true: list of integer class labels (length N).y_pred: list of integer predicted class labels (length N).
Return:
- A single float: the Macro F1 score. Do not round inside your solution.
Example
Input:
y_true = [0, 0, 1, 1, 2, 2], y_pred = [0, 1, 1, 1, 2, 0]
Output:
0.6556
Reasoning: For class 0: TP=1, FP=1, FN=1 -> precision=0.5, recall=0.5, F1=0.5. For class 1: TP=2, FP=1, FN=0 -> precision=2/3, recall=1.0, F1=0.8. For class 2: TP=1, FP=0, FN=1 -> precision=1.0, recall=0.5, F1=2/3. Macro F1 = (0.5 + 0.8 + 2/3) / 3 $\approx$ 0.6556.
Show hint
How can a multi-class problem be broken into several binary problems so the usual precision and recall apply?
Relevant roles: Data Scientist, ML Engineer, Applied Scientist and AI Engineer.
Related free problems
- Macro, Micro and Weighted F1 for a Ticket RouterClassical MLMedium
- Multi-Head AttentionMedium
- Masked Self-AttentionMedium