Module 2 — Evaluating Classifiers and Tuning Parameters

Sources: Lec 5 — Evaluation Criteria for Data Classification; Lec 6 — Parameter Tuning (Cross-Validation).


C11. The Confusion Matrix, Accuracy, Precision, Recall, F-measure

Intuition

A classifier can be wrong in two different ways: crying wolf (false alarm) and missing the wolf (miss). The confusion matrix counts all four outcomes. Accuracy lumps everything together; precision asks “when I say positive, am I right?”; recall asks “of all real positives, how many did I find?”; F-measure combines the two.

Formal definition

Predicted +Predicted −
Actual +TPFN
Actual −FPTN

Mathematical properties

  • is the harmonic mean of precision and recall : .
  • ; iff ; if or .
  • Equivalent count form: .
  • .

Derivation of the count form

, . Then

Key insights

  • TN does not appear in precision, recall, or — they ignore the (often huge) negative class.
  • Closer and ⇒ higher for a given sum.
  • Convention: if precision is taken as (undefined ).

Worked examples

Example 1 — Very Easy

  • Problem: . Compute accuracy.
  • Asked: Accuracy.
  • Concept: Accuracy formula.
  • Approach: Correct / total.
  • Solution: Correct ; total .
  • Final answer: .
  • Key lesson: Accuracy uses the diagonal.

Example 2 — Easy

  • Problem: Same matrix. Compute precision and recall.
  • Asked: .
  • Concept: Definitions.
  • Approach: Row/column totals of the positive class.
  • Solution: . .
  • Final answer: , .
  • Key lesson: Precision divides by the predicted-positive column; recall by the actual-positive row.

Example 3 — Moderate

  • Problem: Same matrix. Compute two ways and verify .
  • Asked: .
  • Concept: Harmonic mean.
  • Approach: Formula and count form.
  • Solution: . Count form: ✓. Bounds: ✓.
  • Final answer: .
  • Key lesson: Harmonic mean sits below the arithmetic mean, pulled toward the smaller value.

Example 4 — Hard

  • Problem: 1000 samples, 20 positive. Classifier A predicts everything negative. Classifier B has . Compare accuracy, , , .
  • Asked: Which is better.
  • Concept: Accuracy paradox.
  • Approach: Fill matrices.
  • Solution: A: . Accuracy . ; → by convention; . B: accuracy ; ; ; .
  • Final answer: A has higher accuracy (0.98 vs 0.965) but is useless (F=0); B is far better (F=0.46).
  • Key lesson: On imbalanced data, accuracy hides total failure on the rare class.

Example 5 — Very Hard

  • Problem: Ten test items ranked by classifier score (highest first) have true labels P,P,N,P,N,N,P,N,N,N (4 positives). Predicting “positive” for the top items, compute for each and choose the best .
  • Asked: .
  • Concept: ; threshold selection.
  • Approach: Track cumulatively; .
  • Solution: : → . : → . : → . : → . : → . : → . : → . : . : . : .
  • Final answer: Best , (not where recall is 1).
  • Key lesson: is not monotone in the threshold; best balances precision and recall.

Common mistakes

  • Mixing the roles of FP and FN in precision/recall.
  • Quoting accuracy alone on imbalanced data.

Exam traps

  • “Precision = TP/(TP+FN)” is wrong — that is recall.
  • undefined/0 when .

Connections

Sensitivity/specificity and averaging (C12), clustering F-measure (C41), cross-validation scoring (C13).

Mastery checklist

  • Draw the confusion matrix and fill it.
  • Compute and the count form.
  • Explain the accuracy paradox.

C12. Macro/Micro Averaging, Sensitivity & Specificity, Class Imbalance

Intuition

With classes there is one per class (treat class as “positive”, the rest “negative”). Two ways to summarise: average the per-class scores (macro: every class counts equally, rare classes matter) or pool the counts then compute (micro: every data point counts equally, big classes dominate).

Formal definitions

Mathematical result (derived)

In single-label multiclass classification, every misclassified item is a false positive for exactly one class and a false negative for exactly one class, so . Hence .

Derivation

Let = count with actual , predicted . , . , and is the same off-diagonal sum. ∎

Key insights

  • Macro: “per-class average”, insensitive to class frequency. Micro: “per-data-point average”.
  • Sensitivity and specificity are the standard medical pair: how many sick found, how many healthy cleared.

Worked examples

Example 1 — Very Easy

  • Problem: Binary matrix . Sensitivity and specificity?
  • Asked: Both.
  • Concept: Definitions.
  • Approach: Direct.
  • Solution: Sens . Spec .
  • Final answer: and .
  • Key lesson: High specificity can coexist with mediocre sensitivity.

Example 2 — Easy

  • Problem: Three-class confusion matrix (rows actual A,B,C; columns predicted A,B,C): . Compute and per-class precision/recall.
  • Asked: Per-class counts and scores.
  • Concept: One-vs-rest counts.
  • Approach: Diagonal; column sums minus diagonal; row sums minus diagonal.
  • Solution: Row totals: . Column totals: . . . . Precision: . Recall: .
  • Final answer: As computed.
  • Key lesson: Class C (rare, 10 items) is much worse than A.

Example 3 — Moderate

  • Problem: For the matrix of Example 2 compute macro and micro precision and recall.
  • Asked: Four numbers.
  • Concept: Averaging.
  • Approach: Macro: mean of per-class; micro: pooled.
  • Solution: Macro . Macro . Micro: , , → , .
  • Final answer: Macro ; micro .
  • Key lesson: Macro is lower because the weak rare class gets a full third of the weight.

Example 4 — Hard

  • Problem: 980 “common” and 20 “rare” items; a classifier labels everything common. Compute accuracy, micro , macro , sensitivity and specificity of the rare class.
  • Asked: Five numbers.
  • Concept: Which metric exposes imbalance.
  • Approach: Per-class recalls.
  • Solution: Common: → . Rare: → . Accuracy . Micro . Macro . Sensitivity (rare) ; specificity (rare) .
  • Final answer: Accuracy , micro , macro , sensitivity , specificity .
  • Key lesson: Macro averaging (and sensitivity) reveal failure on the rare class; accuracy and micro do not.

Example 5 — Very Hard

  • Problem: Starting from the matrix of Example 2, an improved model re-labels 3 class-C items that were predicted A as predicted C: the C row becomes (rows A,B unchanged). Recompute micro and macro recall and precision, and compare the improvement.
  • Asked: New metrics and change.
  • Concept: Sensitivity of macro vs micro to a rare class.
  • Approach: Recount.
  • Solution: New matrix . Column totals: . , , . Precision: → macro . Recall: → macro . Micro: → . Changes: micro ; macro ; macro .
  • Final answer: Micro (+3 pts); macro (+6 pts), macro (+10 pts).
  • Key lesson: Improving one small class barely moves micro scores but visibly moves macro scores.

Common mistakes

  • Averaging per-class accuracies and calling it macro precision.
  • Expecting micro micro in single-label problems.

Exam traps

  • Sensitivity ≡ recall ≡ TPR; specificity is not precision.
  • Micro = per-data-point average; macro = per-class average.

Connections

C11, class imbalance, cross-validation (C13), clustering evaluation (C41).

Mastery checklist

  • Extract from a multiclass matrix.
  • Compute macro vs micro.
  • Prove micro = micro = accuracy.

C13. Cross-Validation: Leave-One-Out and -Fold

Intuition

Evaluating on training data flatters the model; setting aside one test set wastes data and gives a lucky-or-unlucky estimate. Cross-validation (CV) rotates the roles: every point gets to be a test point exactly once, while the model is never trained on the point it is tested on. CV is also how hyper-parameters (like in kNN, in SVM, tree depth) are chosen.

Formal definition

Data , labels .

  • Leave-One-Out (LOO): for : train on , predict for . Score using vs (e.g. -measure).
  • -fold CV: partition into with (i) , (ii) , (iii) , (iv) sizes (nearly) equal. For : train on , predict . Score on the pooled predictions.

Mathematical formulation

  • Number of trainings: LOO ; -fold . ⇒ -fold = LOO.
  • Each training set has samples.
  • LOO is deterministic (exhaustive); -fold with random folds varies between runs. Repeating (e.g. 50 times) and averaging makes the estimate independent of the particular partition.

Key insights

  • Bias–variance of the estimate: LOO training sets are nearly the full data (low bias) but cost trainings; -fold is cheaper.
  • The folds must be disjoint and cover everything.

Worked examples

Example 1 — Very Easy

  • Problem: points, 3-fold CV. Possible fold sizes? How many models are trained?
  • Asked: Sizes and count.
  • Concept: Near-equal folds.
  • Approach: .
  • Solution: Sizes (any order). One model per fold → 3 models.
  • Final answer: ; 3 models.
  • Key lesson: Folds differ by at most one element.

Example 2 — Easy

  • Problem: . For 5-fold CV and for LOO give training-set size and number of trainings.
  • Asked: Four numbers.
  • Concept: Counts.
  • Approach: Formulas.
  • Solution: 5-fold: test , train , 5 trainings. LOO: train , 100 trainings.
  • Final answer: and .
  • Key lesson: LOO is times more expensive.

Example 3 — Moderate

  • Problem: 1-D points with labels A,A,A,B,B. Run LOO with 1-NN and report accuracy. (Ties: both neighbours A.)
  • Asked: LOO accuracy of 1-NN.
  • Concept: LOO procedure.
  • Approach: Remove each point, classify by nearest remaining.
  • Solution: : nearest is 2 (A) ✓. : neighbours 1 and 3 equidistant (1), both A ✓. : nearest 2 (A) ✓. : nearest 11 (B) ✓. : nearest 10 (B) ✓. 5/5.
  • Final answer: .
  • Key lesson: Well-separated clusters give perfect LOO for 1-NN.

Example 4 — Hard

  • Problem: Same data. Use LOO to choose between and nearest neighbours (majority vote).
  • Asked: Best .
  • Concept: Parameter tuning by CV.
  • Approach: Compute LOO accuracy for .
  • Solution: : others 2(d1),3(d2),10(d9) → A,A,B → A ✓. : 1,3 (d1,d1), 10 (d8) → A,A,B → A ✓. : 2(d1),1(d2),10(d7) → A,A,B → A ✓. : 11(d1),3(d7),2(d8) → B,A,A → A ✗. : 10(d1),3(d8),2(d9) → B,A,A → A ✗. Accuracy . gave .
  • Final answer: (LOO accuracy 1.0 vs 0.6).
  • Key lesson: A larger than the size of the smaller class ( B’s) forces errors; tune by CV.

Example 5 — Very Hard

  • Problem: A 2-fold CV gives fold 1: ; fold 2: . Compute (a) per fold then averaged; (b) on the pooled predictions as the lecture prescribes (“using the actual labels of ”). Explain the difference.
  • Asked: Two scores.
  • Concept: Pooling vs averaging.
  • Approach: .
  • Solution: (a) ; ; mean . (b) Pooled: → .
  • Final answer: Average-of-folds ; pooled .
  • Key lesson: is nonlinear in counts; fold-averaging weights small/odd folds equally, pooling weights data points equally. Be explicit about which you use.

Common mistakes

  • Tuning on the test fold (data leakage) and reporting that score.
  • Overlapping folds.
  • Using stratification-free random folds with tiny classes.

Exam traps

  • LOO is exhaustive and deterministic; -fold with random partition is not.
  • ⇒ LOO.

Connections

Pruning (C8), in kNN (C23), in SVM (C32), parameters.

Mastery checklist

  • Write both algorithms.
  • State folds’ four properties.
  • Use CV to pick a hyper-parameter.