Understand the idea
F1 balances precision and recall for a class. Macro F1 averages class F1 values equally, regardless of frequency. Production uses macro F1 for classifier selection and reports accuracy alongside it.
What the syntax does
f1_score- Combines precision and recall for each class.
average='macro'- Averages class F1 values with equal class weight, rather than weighting by class size.
average:"binary","micro","macro","weighted",None
Outputs you create
answer- The requested result, with the values, labels and row order described by this task.
actual | predicted |
|---|---|
| A | A |
| A | A |
| A | A |
| A | A |
| A | A |
| A | A |
| A | A |
| A | B |
| B | A |
| B | A |
| B | A |
| B | B |
- Compute macro F1 for ERROR12.
answerHints
Think: Macro averaging gives each class equal weight after its F1 is calculated.
Tools: f1_score with average='macro'.
Approach: Score the supplied actual and predicted labels with the requested averaging rule.
Explained solution
from sklearn.metrics import f1_score
answer=f1_score(df.actual,df.predicted,average='macro')
The macro summary balances class contributions rather than weighting them by how many rows they contain.