1) Definition
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$.
Interpretation: “When the model predicts class $c$, how often is it right?”
2) Why it matters
- Overall precision (macro/micro/weighted) hides class-specific behavior.
- Per-class precision shows which classes are performing well and which are problematic.
- Especially useful for:
- Imbalanced datasets (rare classes might have very poor precision).
- Error analysis (see which class is most confused with others).
3) Example
Suppose a 3-class problem (A, B, C):
| True | Pred |
|---|---|
| A | A |
| A | B |
| B | B |
| B | C |
| C | C |
| C | A |
- Class A:
- Predicted A = {A→A, C→A} = 2 predictions
- TP_A = 1 (A→A), FP_A = 1 (C→A)
- Precision_A = 1 / 2 = 0.5
- Class B:
- Predicted B = {A→B, B→B} = 2 predictions
- TP_B = 1, FP_B = 1
- Precision_B = 1 / 2 = 0.5
- Class C:
- Predicted C = {B→C, C→C} = 2 predictions
- TP_C = 1, FP_C = 1
- Precision_C = 1 / 2 = 0.5
Each class precision = 0.5.
4) 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"]
# Per-class precision
print(precision_score(y_true, y_pred, average=None, labels=["A","B","C"]))
Output:
[0.5 0.5 0.5]
Each value corresponds to precision for A, B, C.
5) Relationship to macro/micro
- Per-class precision gives you a list of values, one per class.
- Macro precision = arithmetic mean of those values.
- Weighted precision = weighted mean (by support).
- Micro precision = computed globally, not per class.
Summary
- Per-class precision = precision for each class individually.
- Highlights which classes are reliable vs. where the model struggles.
- Useful for imbalanced data and detailed error analysis.
