Skip to main content

Multiclass and Choosing Metrics

Topic — Real-world classification problems frequently involve more than two categories: diagnosing dozens of medical conditions, recognizing hand-written digits, or categorizing customer inquiries. This guide covers how to extend binary metrics to multiclass settings using One-vs-Rest (OvR) decomposition, compares Macro, Micro, and Weighted averaging, and provides an Interactive Metric Selection Wizard to determine the optimal evaluation metric for any machine learning problem.


1. The Core Concept​

In a multiclass problem with K>2K > 2 categories, the confusion matrix expands into an K×KK \times K grid where:

  • Rows represent the Actual Ground Truth Class (yy).
  • Columns represent the Predicted Class (y^\hat{y}).
  • The main diagonal entries represent correct classifications.
  • All off-diagonal entries represent misclassification confusions.

One-vs-Rest (OvR) Metric Extraction​

To compute Precision, Recall, or F1F_1 for any individual class kk, we treat category kk as the Positive Class (11) and the union of all other K−1K-1 categories as the Negative Class (00):

  • TPk\text{TP}_k: The diagonal cell for class kk (Matrix[k,k]\text{Matrix}[k, k]).
  • FPk\text{FP}_k: Column sum for class kk minus TPk\text{TP}_k (other classes falsely predicted as kk).
  • FNk\text{FN}_k: Row sum for class kk minus TPk\text{TP}_k (actual instances of kk misclassified elsewhere).
tip

If you only take one thing from this page

  • Macro-averaging treats every class equally, making it essential when performance on rare minority classes matters.
  • Weighted-averaging weights each class by its support, reflecting population distribution.
  • Micro-averaging pools samples globally and is mathematically identical to standard Overall Accuracy.

2. Interactive Averaging Calculator: Macro vs. Weighted​

Observe how performance on a rare category (1%1\% prevalence) influences Macro vs. Weighted metrics:

Interactive Multiclass Averaging Calculator

Dataset Support: Class A (1,000 samples, 90.1%), Class B (100 samples, 9.0%), Class C (10 samples, 0.9%).

Class A (Majority - 1,000):95%
Class B (Medium - 100):85%
Class C (Rare - 10):10%
Macro F1 (Unweighted Average)
63.3%
(F₁_A + F₁_B + F₁_C) / 3
Weighted F1 (Support-Weighted)
93.3%
Σ (n_k / N) · F₁_k
Masking Alert: Minority Class Failure Hidden

Class C is failing miserably (F1 = 10%). Yet, Weighted F1 reports a stellar 93.3% because Class A dominates 90% of samples! Macro F1 plummets to 63.3%, exposing that the system cannot detect the rare category.


3. Comparing Averaging Schemes​


  • The Math: Unweighted arithmetic mean across all KK classes: Macro F1=1K∑k=0K−1F1,k\text{Macro } F_1 = \frac{1}{K} \sum_{k=0}^{K-1} F_{1, k}
  • Equal Weighting: A class with 10 samples has the exact same voting power as a class with 100,000 samples.
  • Best For: When performance on every class is vital (e.g. rare disease diagnosis, multi-language translation).

4. Interactive Metric Selection Wizard​

Select your project parameters to receive the exact recommended metric, scikit-learn API snippet, and production rationale:

Interactive Metric Decision Wizard

Configure your production constraints below to discover the optimal evaluation strategy:

RECOMMENDED PRIMARY METRIC:
PR-AUC or Recall at Fixed Precision
average_precision_score(y_true, y_scores)

Why: On severe binary imbalance, PR-AUC avoids ROC inflation and exposes false alarm floods.


5. Metric Selection Architecture​


6. Implementation Lab​

tip

Lab Exercise: Multiclass Averaging & Metric Benchmarking

An interactive, fully functional Google Colab / Jupyter Notebook is available to extract One-vs-Rest counts, benchmark Macro vs. Weighted F1 on imbalanced cohorts, and evaluate multiclass Log Loss live in your browser.

Open In Colab

How to run the lab:

  1. Click the "Open In Colab" badge above to launch the interactive notebook.
  2. Run cells sequentially to extract per-class metrics and calculate averaging divergence.

Key Experiments to Run:

  • Experiment 1 (Extracting OvR from 3x3 Matrix): Extract TP,FP,FNTP, FP, FN for Iris Setosa, Versicolor, and Virginica, computing individual F1F_1 scores (98.0%,95.0%,94.0%98.0\%, 95.0\%, 94.0\%).
  • Experiment 2 (The Minority Collapse Test): Misclassify 100% of minority Class 2 on an imbalanced cohort to watch Weighted F1 stay high (91.1%91.1\%) while Macro F1 plunges (60.6%60.6\%).
  • Experiment 3 (Multiclass Log Loss): Compute cross-entropy across predicted probability matrices using log_loss to evaluate confidence calibration.

7. Common Mistakes​


  • The Misconception: "Our 10-class document classifier achieved 93% accuracy, so it is ready for deployment."
  • Wrong Thinking: Quoting accuracy without checking per-class support.
  • The Right Principle: If two majority classes account for 90% of documents, an accuracy of 93% can coexist with 0% accuracy on the remaining 8 rare document types. Always inspect Macro F1 and the full confusion matrix.

8. Summary​

Averaging Schemes

  • Macro: Arithmetic mean of per-class metrics. Treats all classes equally. Exposes rare class collapse.
  • Weighted: Support-weighted mean. Reflects natural sample frequency.
  • Micro: Globally pooled sample counts. Identical to overall accuracy.
  • One-vs-Rest: Treats class kk as positive and all others as negative to extract per-class TP, FP, FN.

Selection Guide

  • Binary Balanced: Accuracy or F1F_1-Score.
  • Binary Imbalanced: PR-AUC, MCC, or Balanced Accuracy.
  • High Recall Focus (FN Critical): F2F_2-Score or threshold tuning.
  • High Precision Focus (FP Costly): F0.5F_{0.5}-Score or threshold tuning.
  • Multiclass Imbalanced: Macro F1F_1 (rare class vital) or Weighted F1F_1 (volume focus).

info

Key Takeaways

  • Macro protects the rare: Whenever catching minority classes is critical, Macro F1 prevents majority classes from masking failures.
  • Micro is just Accuracy: Never report Micro F1 as if it were a new balanced metric; it is mathematically identical to accuracy.
  • Anchor against baseline: Always compare model metrics to a zero-rule dummy classifier to quantify genuine diagnostic lift.

Next Module: Generalization & Model Control - diagnosing bias vs. variance, mastering L1 and L2 regularization, and defeating the curse of dimensionality.


9. Active Recall​

Test your understanding by answering first, then clicking to reveal the underlying principles.

Interactive Checkpoint Quiz​

Interactive Checkpoint: Averaging Scheme for Rare Tumors

A pathology model classifies 5 tumor types. Benign cysts represent 85% of cases, while an aggressive malignant lymphoma represents only 1% of cases. Which averaging method should the clinical evaluation committee prioritize to ensure the rare lymphoma is not overlooked?

Review Flashcards​

1. [THEORY] Why is Micro F1 mathematically identical to Overall Accuracy in mutually exclusive multiclass classification?

  • Every Error is Dual: If sample ii belonging to Class A is misclassified as Class B:
    • It is simultaneously a False Positive for Class B (FPB↑\text{FP}_B \uparrow).
    • And a False Negative for Class A (FNA↑\text{FN}_A \uparrow).
  • Summing across all classes: ∑k=0K−1FPk=∑k=0K−1FNk=Total Misclassifications\sum_{k=0}^{K-1} \text{FP}_k = \sum_{k=0}^{K-1} \text{FN}_k = \text{Total Misclassifications}
  • Therefore: Micro Precision=∑TPk∑TPk+∑FPk=CorrectTotal≡Accuracy\text{Micro Precision} = \frac{\sum \text{TP}_k}{\sum \text{TP}_k + \sum \text{FP}_k} = \frac{\text{Correct}}{\text{Total}} \equiv \text{Accuracy}
2. [MATH] A 3-class model achieves per-class F1 scores of [0.90, 0.70, 0.20] on classes with support [700, 200, 100]. Compute Macro F1 and Weighted F1.

  • Macro F1: Macro F1=0.90+0.70+0.203=1.803=0.60(60.0%)\text{Macro } F_1 = \frac{0.90 + 0.70 + 0.20}{3} = \frac{1.80}{3} = 0.60 \quad (60.0\%)
  • Weighted F1: Weighted F1=700(0.90)+200(0.70)+100(0.20)1000=630+140+201000=7901000=0.79(79.0%)\text{Weighted } F_1 = \frac{700(0.90) + 200(0.70) + 100(0.20)}{1000} = \frac{630 + 140 + 20}{1000} = \frac{790}{1000} = 0.79 \quad (79.0\%)
  • Notice the huge 19 percentage point gap! Weighted F1 looks great (79%79\%), but Macro F1 exposes the severe struggle on Class 2 (20%20\%).
3. [ANALYZE] In scikit-learn, what error occurs if you call precision_score(y_true, y_pred) on multiclass targets without parameters?

  • ValueError: Target is multiclass but average='binary'. Please choose another average setting, one of [None, 'micro', 'macro', 'weighted'].
  • Scikit-learn defaults to binary averaging (average='binary'), which expects labels in {0,1}\{0, 1\}.
  • You must pass average='macro', average='weighted', or average=None (to get per-class scores).
4. [PRACTICE] Given a 3x3 confusion matrix: Row 0: [45, 3, 2], Row 1: [2, 40, 8], Row 2: [1, 4, 45]. Extract TP, FP, FN for Class 1.

  • TP1\text{TP}_1: Diagonal element Matrix[1,1]=40\text{Matrix}[1, 1] = 40.
  • FP1\text{FP}_1: Column 1 sum minus diagonal =(3+40+4)−40=7= (3 + 40 + 4) - 40 = 7.
  • FN1\text{FN}_1: Row 1 sum minus diagonal =(2+40+8)−40=10= (2 + 40 + 8) - 40 = 10.
  • Precision: 4040+7=4047≈85.1%\frac{40}{40 + 7} = \frac{40}{47} \approx 85.1\%.
  • Recall: 4040+10=4050=80.0%\frac{40}{40 + 10} = \frac{40}{50} = 80.0\%.
5. [THEORY] Why is Log Loss considered a superior evaluation metric to F1 when outputs are used for probabilistic decision making?

  • Calibration & Uncertainty: F1F_1 only evaluates discrete hard predictions after applying a threshold cutoff, ignoring confidence entirely.
  • A prediction of 51%51\% and a prediction of 99%99\% receive identical treatment in F1F_1.
  • Log Loss directly penalizes overconfident wrong predictions and rewards well-calibrated posterior probabilities, making it essential for financial risk, medical staging, and downstream expected value modeling.