Middle
What metrics of multiclass classification do you know? Their pros and cons (macro, micro, weighted).
sobes.tech AI
Answer from AI
In multi-class classification tasks, the following aggregation metrics are often used:
-
Macro-average — calculates the metric (e.g., precision, recall, F1) for each class separately, then averages them without considering the number of examples in each class.
- Pros: gives equal weight to all classes, useful for imbalanced data to not ignore rare classes.
- Cons: can be too sensitive to classes with few examples.
-
Micro-average — combines all predictions and true labels across all classes and computes the metric on this combined set.
- Pros: reflects the overall performance of the model, considering the number of examples in each class.
- Cons: dominates classes with many examples, rare classes may be "dampened".
-
Weighted-average — computes the metric for each class and averages them, weighted by the number of examples in each class.
- Pros: balances between macro and micro, considering each class's contribution proportionally to its size.
- Cons: may still underestimate rare classes if they are very small.
The choice of metric depends on the task: if equal evaluation for all classes is important — macro, if overall balance — micro, if a compromise is needed — weighted.
Example in sklearn:
from sklearn.metrics import precision_recall_fscore_support
# y_true and y_pred are true and predicted labels
precision, recall, f1, _ = precision_recall_fscore_support(y_true, y_pred, average='macro')