Macro vs weighted metrics in classification for machine learning
| Metric Type | Meaning |
|---|---|
| Macro | Calculates metric individually per class, then takes the unweighted average. Treats all classes equally, regardless of size. |
| Weighted | Calculates metric per class, then takes the average weighted by class support (number of true samples per class). Large classes have more influence. |
Example Dataset:
| Class | True samples (support) |
Precision | Recall | F1-score |
|---|---|---|---|---|
| Cat | 50 | 0.9 | 0.8 | 0.85 |
| Dog | 40 | 0.6 | 0.7 | 0.65 |
| Tiger | 10 | 0.5 | 0.6 | 0.55 |
Final Result:
| Metric | Macro Average | Weighted Average |
|---|---|---|
| F1-score | 0.683 | 0.74 |
When to use and What?
| Scenario | Recommended Metric |
|---|---|
| Class imbalance exists | Weighted |
| You care equally about all classes | Macro |
| You want to compare per-class fairness | Macro |
| You want an overall accuracy-type proxy | Weighted |
Coding:
from sklearn.metrics import f1_score
# Example:
f1_macro = f1_score(y_true, y_pred, average='macro')
f1_weighted = f1_score(y_true, y_pred, average='weighted')
Md. Alamgir Hossain
MSc in ICT, IICT, BUET
No comments:
Post a Comment