Solution 30. Two worth asking:
What is the recall? Precision of 99% is trivial to achieve by flagging one message you are certain about and nothing else.
What is the prevalence in the data it was measured on? If the test set was half spam, 99% precision is a real achievement. If it was tuned and measured on the same messages, or if the test set was built by collecting spam until there was a lot of it, the number does not transfer to a real inbox.
A third question: on which messages? A detector measured on last year’s spam is being asked an easier question than the one it will face.
Multi-class metrics
Precision, recall and \(F_\recallweight\) are all defined for a detection task, so for \(C > 2\) we reduce to detection first. The standard reduction is one-vs-rest. We take each class \(c\) in turn, relabel every point as \(c\) or \(\lnot c\), and read the resulting \(2 \times 2\) matrix directly from \(M\),
\[T_p^{(c)} = M_{cc}, \quad
F_n^{(c)} = \sum_{j \neq c} M_{cj}, \quad
F_p^{(c)} = \sum_{i \neq c} M_{ic}, \quad
T_n^{(c)} = N - T_p^{(c)} - F_n^{(c)} - F_p^{(c)}\]
The diagonal entry counts the true positives. The rest of row \(c\) counts the class-\(c\) points the model mislabeled, and the rest of column \(c\) counts the other points it mislabeled as \(c\).
That leaves \(C\) binary problems and \(C\) values of whichever metric you wanted. Two conventions collapse them to one number. They differ only in when the averaging happens.
Macro-averaging computes the metric per class, then averages: \[P_{macro} = \frac{1}{C}\sumto{c}{C} \frac{T_p^{(c)}}{T_p^{(c)} + F_p^{(c)}},
\qquad
R_{macro} = \frac{1}{C}\sumto{c}{C} \frac{T_p^{(c)}}{T_p^{(c)} + F_n^{(c)}}\] Every class contributes equally, however rare it is.
Micro-averaging pools the counts, then computes the metric once: \[P_{micro} = \frac{\sum_c T_p^{(c)}}{\sum_c T_p^{(c)} + \sum_c F_p^{(c)}},
\qquad
R_{micro} = \frac{\sum_c T_p^{(c)}}{\sum_c T_p^{(c)} + \sum_c F_n^{(c)}}\] Every point contributes equally, so large classes dominate.
When every point gets exactly one predicted label, each mistake is a false positive for exactly one class and a false negative for exactly one other, so \(\sum_c F_p^{(c)} = \sum_c F_n^{(c)}\) and the two micro quantities coincide with each other and with accuracy: \(P_{micro} = R_{micro} = \mathrm{Acc}\).
The same two conventions apply to \(F_\recallweight\), and the order of operations matters more there than it does for \(P\) and \(R\). We compute micro-\(F_1\) from the pooled counts, so it is the \(F_1\) formula applied to \(P_{micro}\) and \(R_{micro}\) — which makes it accuracy again when every point gets exactly one predicted label, since then \(P_{micro} = R_{micro} = \mathrm{Acc}\). Macro-\(F_1\) is the mean of the \(C\) per-class \(F_1\) scores, and that is not the \(F_1\) formula applied to \(P_{macro}\) and \(R_{macro}\): the formula is not linear in \(P\) and \(R\), so averaging before combining and combining before averaging give different numbers.
- Discussion: under what class proportions and per-class scores do macro- and micro-averaging agree? What has to be true of the errors for the two to diverge sharply?
Balanced accuracy (higher is better)
\(R_{macro}\) has its own name, balanced accuracy (\(\mathrm{BAcc}\)), because it fixes the most common failure of accuracy: \[\mathrm{BAcc} = \frac{1}{C}\sumto{c}{C} \frac{T_p^{(c)}}{T_p^{(c)} + F_n^{(c)}}\]
Averaging the per-class recalls gives each class equal weight regardless of size, so a model that ignores a rare class scores \(0\) on that class’s term however few points it contains [10]. A classifier guessing uniformly at random scores \(1/C\) whatever the class proportions are, which accuracy does not.
Take \(C = 3\) and \(N = 90\), with \(M_{ij}\) the number of class-\(i\) points predicted as class \(j\) — rows are the true class, columns the prediction:
| 1 |
3 |
7 |
0 |
10 |
| 2 |
0 |
50 |
0 |
50 |
| 3 |
0 |
12 |
18 |
30 |
The diagonal sums to \(71\), so \(\mathrm{Acc} = 71/90 \approx 0.789\), which looks fairly high. The per-class recalls are \(3/10 = 0.3\), \(50/50 = 1.0\) and \(18/30 = 0.6\), giving \(\mathrm{BAcc} = 1.9/3 \approx 0.633\). Class 2 accounts for the whole difference: it holds \(50\) of the \(90\) points and is classified perfectly, so under accuracy it dominates the score, while class 1 — right less than a third of the time — can only ever move the total by \(10\) points.