1) What multiclass means
- In multiclass classification, each sample belongs to exactly one class out of several.
- Example: MNIST digit classification (class = 0,1,…,9).
- Unlike multilabel (where a sample can have multiple labels), here only one label is correct per sample.
2) Precision per class (one-vs-rest view)
For a given class $c$:
$\text{Precision}_c = \frac{TP_c}{TP_c + FP_c}$
- $TP_c$: number of samples correctly predicted as class $c$.
- $FP_c$: number of samples incorrectly predicted as class $c$.
Intuition: “When the model predicts class $c$, how often is it right?”
3) Averaging methods (to combine across classes)
Because we want a single precision score across all classes, we use different averaging schemes:
- Macro Precision
- Compute precision for each class, then take an unweighted mean.Each class has equal importance, even if rare.
- $\text{Precision}_{macro} = \frac{1}{C}\sum_{c=1}^C \text{Precision}_c$
- Weighted Precision
- Same as macro, but each class weighted by its support (number of true samples).
- Majority classes have more influence.
- Micro Precision
- Pool all $TP$ and $FP$ across classes, then compute precision once.Equivalent to accuracy in single-label classification.
- $\text{Precision}_{micro} = \frac{\sum_c TP_c}{\sum_c (TP_c + FP_c)}$
4) Example
Suppose we have 3 classes (A, B, C):
| True | Pred |
|---|---|
| A | A |
| A | B |
| B | B |
| B | C |
| C | C |
| C | A |
- Class A:
- TP = 1 (A→A), FP = 1 (C→A) → Precision_A = 1/2 = 0.5
- Class B:
- TP = 1 (B→B), FP = 1 (A→B) → Precision_B = 1/2 = 0.5
- Class C:
- TP = 1 (C→C), FP = 1 (B→C) → Precision_C = 1/2 = 0.5
- Macro Precision = (0.5 + 0.5 + 0.5)/3 = 0.5
- Weighted Precision = same here (balanced dataset) = 0.5
- Micro Precision = total TP / (TP+FP) = 3 / (3+3) = 0.5
5) In scikit-learn
from sklearn.metrics import precision_score
y_true = ["A","A","B","B","C","C"]
y_pred = ["A","B","B","C","C","A"]
print("Micro :", precision_score(y_true, y_pred, average="micro"))
print("Macro :", precision_score(y_true, y_pred, average="macro"))
print("Weighted:", precision_score(y_true, y_pred, average="weighted"))
Summary
- Multiclass Precision = precision extended to multiple mutually exclusive classes.
- Computed per class (one-vs-rest), then combined via averaging.
- Macro: equal class weight → checks fairness.
- Micro: equal sample weight → overall effectiveness.
- Weighted: balances according to class frequencies.
