Confusion Matrix

A confusion matrix counts how a classifier’s predicted labels match the true labels on a test set. In a binary problem one class is called positive (spam, fraud, disease) and the other negative. A model that outputs a score becomes a classifier once we pick a threshold . It predicts positive when . Each test point then falls into one of four cells. Rows give the true class and columns give the prediction:

Predicted positivePredicted negative
Actual positivetrue positive (TP)false negative (FN)
Actual negativefalse positive (FP)true negative (TN)

A false positive is a false alarm, or type I error. A false negative is a miss, or type II error. The test set has actual positives, actual negatives and prevalence . The standard metrics are ratios of the four counts:

  • Accuracy: the fraction of all predictions that are correct, .
  • Precision (positive predictive value): the fraction of predicted positives that are positive, .
  • Recall (sensitivity, true positive rate, TPR): the fraction of actual positives that the model catches, .
  • Specificity (true negative rate): the fraction of actual negatives labeled negative, .
  • False positive rate (FPR): the fraction of actual negatives flagged as positive, .

The score combines precision and recall through their harmonic mean:

The second form shows that never uses . The score treats recall as times as important as precision:

Here is chosen by the user. favors recall, favors precision, and gives .

The accuracy paradox names a common failure. A model that always predicts the majority class can score a high accuracy and catch no positives. Accuracy is therefore a poor guide when is far from .

With classes the matrix is , and entry counts points of true class predicted as class . Each class gets its own precision, recall and by treating it as positive and every other class as negative. Two averages summarize the values:

  • Macro average: compute the metric for each class, then take the unweighted mean over the classes.
  • Micro average: add the TP, FP and FN counts over all classes, then compute the metric once from the totals.

The confusion matrix describes one threshold. The ROC curve and the precision-recall curve sweep the threshold, and log loss scores the probabilities directly.

Intuition

The name comes from the off-diagonal cells. They show which classes the model confuses with which. A single number such as accuracy adds the two kinds of error together. In most applications the two errors have very different costs. A spam filter that hides a job offer does more harm than one that lets an advertisement through. A cancer screen that misses a tumor does more harm than one that orders an extra biopsy. The matrix keeps the errors apart so that each can be weighed.

Precision and recall serve two different people. Precision matters to the analyst who reviews the flagged cases. It is the fraction of their alerts that are real. Recall matters to the person responsible for the positives. It is the fraction of all frauds or tumors that the model catches. The rows of the matrix come from the labels and stay fixed. The threshold decides only how each row splits between the two columns.

Properties

(Accuracy Is a Weighted Average) Accuracy equals , so the larger class controls it.

Let be the test set size. Recall divides by the positives, so . In the same way, . Adding them and dividing by gives the formula. With 0.5% fraud, accuracy is , so recall barely moves it. A model that always answers “legitimate” has specificity 1 and recall 0, and it scores 99.5%. Any useful fraud model flags some legitimate transactions, so it can easily score below this trivial model. Report the majority-class rate next to accuracy as its chance level.

(Precision Depends on Prevalence) Recall and specificity are rates within one true class, while precision mixes both classes and changes with .

Bayes’ rule gives precision as the probability of a positive label given a positive prediction:

A screening test with sensitivity 0.99 and specificity 0.95 has the same TPR and FPR in every population. In a clinic where 10% of patients are sick, 68.8% of its positive results are correct. In general screening with 1% prevalence, only 16.7% are correct, so five of every six positive results are false alarms. A precision measured at one prevalence therefore does not carry over to a deployment with another. Recall and specificity do carry over, as long as each class keeps its distribution of scores.

(F1 Ignores True Negatives) stays the same when correctly rejected negatives are added, which suits retrieval and detection.

In a search or detection task the negatives are “everything else”. Adding a million correctly ignored web pages leaves unchanged, while accuracy would approach 1. For the same reason, depends on which class is called positive. Swapping the labels swaps the roles of TP and TN. The harmonic mean stays close to the smaller of its two inputs. A model with precision 1 and recall 0.01 has , while the arithmetic mean would give 0.505. weights precision and recall equally. When a miss costs more than a false alarm, states that preference directly.

(Moving the Threshold) Changing the threshold moves points between the columns of the matrix and never between its rows.

Raising turns some predicted positives into predicted negatives. Each true positive that crosses becomes a false negative, and each false positive becomes a true negative. The row totals and stay fixed, so recall and FPR can only fall as rises. Precision usually rises, but it falls at any step that removes a true positive and no false positive. The cost-minimizing threshold for calibrated probabilities follows from the misclassification costs, as 4.1. Bayes Classifier derives. The ROC and precision-recall curves record the split at every threshold at once.

The animation moves a threshold down 40 scored points. Points scoring at or above are predicted positive. The four counts, precision, recall and accuracy update at each step.

\usepackage{tikz}
\usetikzlibrary{animations, arrows.meta}
\begin{document}
\definecolor{hot}{HTML}{D9483B}
\definecolor{cold}{HTML}{2F80ED}
\newcommand{\pf}[1]{\filldraw[draw=black, fill=hot] (#1) circle (2.4pt);}
\newcommand{\ph}[1]{\draw[hot, thick] (#1) circle (2.2pt);}
\newcommand{\nf}[1]{\filldraw[draw=black, fill=cold] (#1) circle (2.4pt);}
\newcommand{\nh}[1]{\draw[cold, thick] (#1) circle (2.2pt);}
\begin{tikzpicture}[font=\small, >={Stealth[length=2.2mm]}]
\draw (-0.10,0) -- (13.10,0);
\draw (0.00,-0.07) -- (0.00,0.07) node[pos=0, below] {\footnotesize $0$};
\draw (3.25,-0.07) -- (3.25,0.07) node[pos=0, below] {\footnotesize $0.25$};
\draw (6.50,-0.07) -- (6.50,0.07) node[pos=0, below] {\footnotesize $0.5$};
\draw (9.75,-0.07) -- (9.75,0.07) node[pos=0, below] {\footnotesize $0.75$};
\draw (13.00,-0.07) -- (13.00,0.07) node[pos=0, below] {\footnotesize $1$};
\node at (6.50,-0.82) {classifier score $s$};
\node[anchor=east] at (-0.2,1.5) {$y = 1$};
\node[anchor=east] at (-0.2,0.6) {$y = 0$};
\ph{3.51,1.5} \ph{4.42,1.5} \ph{4.94,1.5} \ph{5.59,1.5} \ph{6.11,1.5} \ph{6.63,1.5} \ph{7.02,1.5} \ph{7.54,1.5} \ph{7.93,1.5} \ph{8.32,1.5} \ph{8.71,1.5} \ph{9.23,1.5} \ph{9.62,1.5} \ph{10.01,1.5} \ph{10.53,1.5} \ph{10.92,1.5} \ph{11.31,1.5} \ph{11.83,1.5} \ph{12.22,1.5} \ph{12.61,1.5}
\nh{0.26,0.6} \nh{0.78,0.6} \nh{1.17,0.6} \nh{1.56,0.6} \nh{2.08,0.6} \nh{2.47,0.6} \nh{2.86,0.6} \nh{3.38,0.6} \nh{3.77,0.6} \nh{4.16,0.6} \nh{4.68,0.6} \nh{5.07,0.6} \nh{5.46,0.6} \nh{5.98,0.6} \nh{6.37,0.6} \nh{6.89,0.6} \nh{7.28,0.6} \nh{8.06,0.6} \nh{8.84,0.6} \nh{9.49,0.6}
\node[anchor=west, align=left] at (7.0,-4.35) {\footnotesize filled: predicted positive ($s \ge t$) \\ \footnotesize hollow: predicted negative ($s < t$)};
\draw[thick] (2.5,-2.0) rectangle (5.10,-4.60);
\draw (3.80,-2.0) -- (3.80,-4.60); \draw (2.5,-3.30) -- (5.10,-3.30);
\node at (3.80,-1.28) {predicted};
\node at (3.15,-1.74) {\footnotesize $\hat y = 1$};
\node at (4.45,-1.74) {\footnotesize $\hat y = 0$};
\node[anchor=east] at (2.38,-1.74) {actual};
\node[anchor=east] at (2.38,-2.65) {\footnotesize $y = 1$};
\node[anchor=east] at (2.38,-3.95) {\footnotesize $y = 0$};
\pf{2.85,-2.29} \node[anchor=west] at (2.97,-2.29) {\footnotesize TP};
\ph{4.15,-2.29} \node[anchor=west] at (4.27,-2.29) {\footnotesize FN};
\nf{2.85,-3.59} \node[anchor=west] at (2.97,-3.59) {\footnotesize FP};
\nh{4.15,-3.59} \node[anchor=west] at (4.27,-3.59) {\footnotesize TN};
\node[anchor=west] at (7.0,-2.35) {precision $= \mathrm{TP}/(\mathrm{TP} + \mathrm{FP})$};
\node[anchor=west] at (7.0,-2.95) {recall $= \mathrm{TP}/(\mathrm{TP} + \mathrm{FN})$};
\node[anchor=west] at (7.0,-3.55) {accuracy $= (\mathrm{TP} + \mathrm{TN})/40$};
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 2.2s = "true", 18.36s = "true", repeats}}}]
\pf{12.61,1.5}
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 2.46s = "true", 18.36s = "true", repeats}}}]
\pf{11.83,1.5} \pf{12.22,1.5}
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 2.72s = "true", 18.36s = "true", repeats}}}]
\pf{11.31,1.5}
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 2.98s = "true", 18.36s = "true", repeats}}}]
\pf{10.53,1.5} \pf{10.92,1.5}
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 5.78s = "true", 18.36s = "true", repeats}}}]
\pf{10.01,1.5}
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 6.04s = "true", 18.36s = "true", repeats}}}]
\pf{9.23,1.5} \pf{9.62,1.5} \nf{9.49,0.6}
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 6.3s = "true", 18.36s = "true", repeats}}}]
\pf{8.71,1.5} \nf{8.84,0.6}
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 6.56s = "true", 18.36s = "true", repeats}}}]
\pf{7.93,1.5} \pf{8.32,1.5} \nf{8.06,0.6}
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 6.82s = "true", 18.36s = "true", repeats}}}]
\pf{7.54,1.5} \nf{7.28,0.6}
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 7.08s = "true", 18.36s = "true", repeats}}}]
\pf{6.63,1.5} \pf{7.02,1.5} \nf{6.89,0.6}
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 9.88s = "true", 18.36s = "true", repeats}}}]
\pf{6.11,1.5} \nf{5.98,0.6} \nf{6.37,0.6}
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 10.14s = "true", 18.36s = "true", repeats}}}]
\pf{5.59,1.5} \nf{5.46,0.6}
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 10.4s = "true", 18.36s = "true", repeats}}}]
\pf{4.94,1.5} \nf{4.68,0.6} \nf{5.07,0.6}
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 10.66s = "true", 18.36s = "true", repeats}}}]
\pf{4.42,1.5} \nf{4.16,0.6}
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 10.92s = "true", 18.36s = "true", repeats}}}]
\pf{3.51,1.5} \nf{3.38,0.6} \nf{3.77,0.6}
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 13.72s = "true", 18.36s = "true", repeats}}}]
\nf{2.86,0.6}
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 13.98s = "true", 18.36s = "true", repeats}}}]
\nf{2.08,0.6} \nf{2.47,0.6}
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 14.24s = "true", 18.36s = "true", repeats}}}]
\nf{1.56,0.6}
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 14.5s = "true", 18.36s = "true", repeats}}}]
\nf{0.78,0.6} \nf{1.17,0.6}
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 14.76s = "true", 18.36s = "true", repeats}}}]
\nf{0.26,0.6}
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "true", 2.2s = "false", 18.36s = "false", repeats}}}]
\draw[very thick] (13.00,-0.12) -- (13.00,2.2) node[above] {$t = 1.00$};
\node[anchor=east] at (12.94,1.98) {\footnotesize $\hat y = 0$};
\node[anchor=west] at (13.06,1.98) {\footnotesize $\hat y = 1$};
\node at (3.15,-2.82) {\Large $0$};
\node at (4.45,-2.82) {\Large $20$};
\node at (3.15,-4.12) {\Large $0$};
\node at (4.45,-4.12) {\Large $20$};
\node[anchor=west] at (11.1,-2.35) {$= 0/0$, undefined};
\node[anchor=west] at (11.1,-2.95) {$= 0/20 = 0.00$};
\node[anchor=west] at (11.1,-3.55) {$= 20/40 = 0.50$};
\node[anchor=west] at (-0.95,-5.25) {At $t = 1$ no point is predicted positive. Precision is $0/0$, so it is undefined.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 2.2s = "true", 2.46s = "false", 18.36s = "false", repeats}}}]
\draw[very thick] (12.35,-0.12) -- (12.35,2.2) node[above] {$t = 0.95$};
\node[anchor=east] at (12.29,1.98) {\footnotesize $\hat y = 0$};
\node[anchor=west] at (12.41,1.98) {\footnotesize $\hat y = 1$};
\node at (3.15,-2.82) {\Large $1$};
\node at (4.45,-2.82) {\Large $19$};
\node at (3.15,-4.12) {\Large $0$};
\node at (4.45,-4.12) {\Large $20$};
\node[anchor=west] at (11.1,-2.35) {$= 1/1 = 1.00$};
\node[anchor=west] at (11.1,-2.95) {$= 1/20 = 0.05$};
\node[anchor=west] at (11.1,-3.55) {$= 21/40 = 0.53$};
\node[anchor=west] at (-0.95,-5.25) {The threshold moves down.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 2.46s = "true", 2.72s = "false", 18.36s = "false", repeats}}}]
\draw[very thick] (11.70,-0.12) -- (11.70,2.2) node[above] {$t = 0.90$};
\node[anchor=east] at (11.64,1.98) {\footnotesize $\hat y = 0$};
\node[anchor=west] at (11.76,1.98) {\footnotesize $\hat y = 1$};
\node at (3.15,-2.82) {\Large $3$};
\node at (4.45,-2.82) {\Large $17$};
\node at (3.15,-4.12) {\Large $0$};
\node at (4.45,-4.12) {\Large $20$};
\node[anchor=west] at (11.1,-2.35) {$= 3/3 = 1.00$};
\node[anchor=west] at (11.1,-2.95) {$= 3/20 = 0.15$};
\node[anchor=west] at (11.1,-3.55) {$= 23/40 = 0.58$};
\node[anchor=west] at (-0.95,-5.25) {The threshold moves down.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 2.72s = "true", 2.98s = "false", 18.36s = "false", repeats}}}]
\draw[very thick] (11.05,-0.12) -- (11.05,2.2) node[above] {$t = 0.85$};
\node[anchor=east] at (10.99,1.98) {\footnotesize $\hat y = 0$};
\node[anchor=west] at (11.11,1.98) {\footnotesize $\hat y = 1$};
\node at (3.15,-2.82) {\Large $4$};
\node at (4.45,-2.82) {\Large $16$};
\node at (3.15,-4.12) {\Large $0$};
\node at (4.45,-4.12) {\Large $20$};
\node[anchor=west] at (11.1,-2.35) {$= 4/4 = 1.00$};
\node[anchor=west] at (11.1,-2.95) {$= 4/20 = 0.20$};
\node[anchor=west] at (11.1,-3.55) {$= 24/40 = 0.60$};
\node[anchor=west] at (-0.95,-5.25) {The threshold moves down.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 2.98s = "true", 5.78s = "false", 18.36s = "false", repeats}}}]
\draw[very thick] (10.40,-0.12) -- (10.40,2.2) node[above] {$t = 0.80$};
\node[anchor=east] at (10.34,1.98) {\footnotesize $\hat y = 0$};
\node[anchor=west] at (10.46,1.98) {\footnotesize $\hat y = 1$};
\node at (3.15,-2.82) {\Large $6$};
\node at (4.45,-2.82) {\Large $14$};
\node at (3.15,-4.12) {\Large $0$};
\node at (4.45,-4.12) {\Large $20$};
\node[anchor=west] at (11.1,-2.35) {$= 6/6 = 1.00$};
\node[anchor=west] at (11.1,-2.95) {$= 6/20 = 0.30$};
\node[anchor=west] at (11.1,-3.55) {$= 26/40 = 0.65$};
\node[anchor=west] at (-0.95,-5.25) {High threshold: every flagged point is positive, but $14$ of $20$ positives are missed.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 5.78s = "true", 6.04s = "false", 18.36s = "false", repeats}}}]
\draw[very thick] (9.75,-0.12) -- (9.75,2.2) node[above] {$t = 0.75$};
\node[anchor=east] at (9.69,1.98) {\footnotesize $\hat y = 0$};
\node[anchor=west] at (9.81,1.98) {\footnotesize $\hat y = 1$};
\node at (3.15,-2.82) {\Large $7$};
\node at (4.45,-2.82) {\Large $13$};
\node at (3.15,-4.12) {\Large $0$};
\node at (4.45,-4.12) {\Large $20$};
\node[anchor=west] at (11.1,-2.35) {$= 7/7 = 1.00$};
\node[anchor=west] at (11.1,-2.95) {$= 7/20 = 0.35$};
\node[anchor=west] at (11.1,-3.55) {$= 27/40 = 0.68$};
\node[anchor=west] at (-0.95,-5.25) {The threshold moves down.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 6.04s = "true", 6.3s = "false", 18.36s = "false", repeats}}}]
\draw[very thick] (9.10,-0.12) -- (9.10,2.2) node[above] {$t = 0.70$};
\node[anchor=east] at (9.04,1.98) {\footnotesize $\hat y = 0$};
\node[anchor=west] at (9.16,1.98) {\footnotesize $\hat y = 1$};
\node at (3.15,-2.82) {\Large $9$};
\node at (4.45,-2.82) {\Large $11$};
\node at (3.15,-4.12) {\Large $1$};
\node at (4.45,-4.12) {\Large $19$};
\node[anchor=west] at (11.1,-2.35) {$= 9/10 = 0.90$};
\node[anchor=west] at (11.1,-2.95) {$= 9/20 = 0.45$};
\node[anchor=west] at (11.1,-3.55) {$= 28/40 = 0.70$};
\node[anchor=west] at (-0.95,-5.25) {The threshold moves down.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 6.3s = "true", 6.56s = "false", 18.36s = "false", repeats}}}]
\draw[very thick] (8.45,-0.12) -- (8.45,2.2) node[above] {$t = 0.65$};
\node[anchor=east] at (8.39,1.98) {\footnotesize $\hat y = 0$};
\node[anchor=west] at (8.51,1.98) {\footnotesize $\hat y = 1$};
\node at (3.15,-2.82) {\Large $10$};
\node at (4.45,-2.82) {\Large $10$};
\node at (3.15,-4.12) {\Large $2$};
\node at (4.45,-4.12) {\Large $18$};
\node[anchor=west] at (11.1,-2.35) {$= 10/12 = 0.83$};
\node[anchor=west] at (11.1,-2.95) {$= 10/20 = 0.50$};
\node[anchor=west] at (11.1,-3.55) {$= 28/40 = 0.70$};
\node[anchor=west] at (-0.95,-5.25) {The threshold moves down.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 6.56s = "true", 6.82s = "false", 18.36s = "false", repeats}}}]
\draw[very thick] (7.80,-0.12) -- (7.80,2.2) node[above] {$t = 0.60$};
\node[anchor=east] at (7.74,1.98) {\footnotesize $\hat y = 0$};
\node[anchor=west] at (7.86,1.98) {\footnotesize $\hat y = 1$};
\node at (3.15,-2.82) {\Large $12$};
\node at (4.45,-2.82) {\Large $8$};
\node at (3.15,-4.12) {\Large $3$};
\node at (4.45,-4.12) {\Large $17$};
\node[anchor=west] at (11.1,-2.35) {$= 12/15 = 0.80$};
\node[anchor=west] at (11.1,-2.95) {$= 12/20 = 0.60$};
\node[anchor=west] at (11.1,-3.55) {$= 29/40 = 0.73$};
\node[anchor=west] at (-0.95,-5.25) {The threshold moves down.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 6.82s = "true", 7.08s = "false", 18.36s = "false", repeats}}}]
\draw[very thick] (7.15,-0.12) -- (7.15,2.2) node[above] {$t = 0.55$};
\node[anchor=east] at (7.09,1.98) {\footnotesize $\hat y = 0$};
\node[anchor=west] at (7.21,1.98) {\footnotesize $\hat y = 1$};
\node at (3.15,-2.82) {\Large $13$};
\node at (4.45,-2.82) {\Large $7$};
\node at (3.15,-4.12) {\Large $4$};
\node at (4.45,-4.12) {\Large $16$};
\node[anchor=west] at (11.1,-2.35) {$= 13/17 = 0.76$};
\node[anchor=west] at (11.1,-2.95) {$= 13/20 = 0.65$};
\node[anchor=west] at (11.1,-3.55) {$= 29/40 = 0.73$};
\node[anchor=west] at (-0.95,-5.25) {The threshold moves down.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 7.08s = "true", 9.88s = "false", 18.36s = "false", repeats}}}]
\draw[very thick] (6.50,-0.12) -- (6.50,2.2) node[above] {$t = 0.50$};
\node[anchor=east] at (6.44,1.98) {\footnotesize $\hat y = 0$};
\node[anchor=west] at (6.56,1.98) {\footnotesize $\hat y = 1$};
\node at (3.15,-2.82) {\Large $15$};
\node at (4.45,-2.82) {\Large $5$};
\node at (3.15,-4.12) {\Large $5$};
\node at (4.45,-4.12) {\Large $15$};
\node[anchor=west] at (11.1,-2.35) {$= 15/20 = 0.75$};
\node[anchor=west] at (11.1,-2.95) {$= 15/20 = 0.75$};
\node[anchor=west] at (11.1,-3.55) {$= 30/40 = 0.75$};
\node[anchor=west] at (-0.95,-5.25) {Middle threshold: $5$ false positives and $5$ false negatives, so precision equals recall.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 9.88s = "true", 10.14s = "false", 18.36s = "false", repeats}}}]
\draw[very thick] (5.85,-0.12) -- (5.85,2.2) node[above] {$t = 0.45$};
\node[anchor=east] at (5.79,1.98) {\footnotesize $\hat y = 0$};
\node[anchor=west] at (5.91,1.98) {\footnotesize $\hat y = 1$};
\node at (3.15,-2.82) {\Large $16$};
\node at (4.45,-2.82) {\Large $4$};
\node at (3.15,-4.12) {\Large $7$};
\node at (4.45,-4.12) {\Large $13$};
\node[anchor=west] at (11.1,-2.35) {$= 16/23 = 0.70$};
\node[anchor=west] at (11.1,-2.95) {$= 16/20 = 0.80$};
\node[anchor=west] at (11.1,-3.55) {$= 29/40 = 0.73$};
\node[anchor=west] at (-0.95,-5.25) {The threshold moves down.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 10.14s = "true", 10.4s = "false", 18.36s = "false", repeats}}}]
\draw[very thick] (5.20,-0.12) -- (5.20,2.2) node[above] {$t = 0.40$};
\node[anchor=east] at (5.14,1.98) {\footnotesize $\hat y = 0$};
\node[anchor=west] at (5.26,1.98) {\footnotesize $\hat y = 1$};
\node at (3.15,-2.82) {\Large $17$};
\node at (4.45,-2.82) {\Large $3$};
\node at (3.15,-4.12) {\Large $8$};
\node at (4.45,-4.12) {\Large $12$};
\node[anchor=west] at (11.1,-2.35) {$= 17/25 = 0.68$};
\node[anchor=west] at (11.1,-2.95) {$= 17/20 = 0.85$};
\node[anchor=west] at (11.1,-3.55) {$= 29/40 = 0.73$};
\node[anchor=west] at (-0.95,-5.25) {The threshold moves down.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 10.4s = "true", 10.66s = "false", 18.36s = "false", repeats}}}]
\draw[very thick] (4.55,-0.12) -- (4.55,2.2) node[above] {$t = 0.35$};
\node[anchor=east] at (4.49,1.98) {\footnotesize $\hat y = 0$};
\node[anchor=west] at (4.61,1.98) {\footnotesize $\hat y = 1$};
\node at (3.15,-2.82) {\Large $18$};
\node at (4.45,-2.82) {\Large $2$};
\node at (3.15,-4.12) {\Large $10$};
\node at (4.45,-4.12) {\Large $10$};
\node[anchor=west] at (11.1,-2.35) {$= 18/28 = 0.64$};
\node[anchor=west] at (11.1,-2.95) {$= 18/20 = 0.90$};
\node[anchor=west] at (11.1,-3.55) {$= 28/40 = 0.70$};
\node[anchor=west] at (-0.95,-5.25) {The threshold moves down.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 10.66s = "true", 10.92s = "false", 18.36s = "false", repeats}}}]
\draw[very thick] (3.90,-0.12) -- (3.90,2.2) node[above] {$t = 0.30$};
\node[anchor=east] at (3.84,1.98) {\footnotesize $\hat y = 0$};
\node[anchor=west] at (3.96,1.98) {\footnotesize $\hat y = 1$};
\node at (3.15,-2.82) {\Large $19$};
\node at (4.45,-2.82) {\Large $1$};
\node at (3.15,-4.12) {\Large $11$};
\node at (4.45,-4.12) {\Large $9$};
\node[anchor=west] at (11.1,-2.35) {$= 19/30 = 0.63$};
\node[anchor=west] at (11.1,-2.95) {$= 19/20 = 0.95$};
\node[anchor=west] at (11.1,-3.55) {$= 28/40 = 0.70$};
\node[anchor=west] at (-0.95,-5.25) {The threshold moves down.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 10.92s = "true", 13.72s = "false", 18.36s = "false", repeats}}}]
\draw[very thick] (3.25,-0.12) -- (3.25,2.2) node[above] {$t = 0.25$};
\node[anchor=east] at (3.19,1.98) {\footnotesize $\hat y = 0$};
\node[anchor=west] at (3.31,1.98) {\footnotesize $\hat y = 1$};
\node at (3.15,-2.82) {\Large $20$};
\node at (4.45,-2.82) {\Large $0$};
\node at (3.15,-4.12) {\Large $13$};
\node at (4.45,-4.12) {\Large $7$};
\node[anchor=west] at (11.1,-2.35) {$= 20/33 = 0.61$};
\node[anchor=west] at (11.1,-2.95) {$= 20/20 = 1.00$};
\node[anchor=west] at (11.1,-3.55) {$= 27/40 = 0.68$};
\node[anchor=west] at (-0.95,-5.25) {Low threshold: no positive is missed, but $13$ negatives are flagged too.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 13.72s = "true", 13.98s = "false", 18.36s = "false", repeats}}}]
\draw[very thick] (2.60,-0.12) -- (2.60,2.2) node[above] {$t = 0.20$};
\node[anchor=east] at (2.54,1.98) {\footnotesize $\hat y = 0$};
\node[anchor=west] at (2.66,1.98) {\footnotesize $\hat y = 1$};
\node at (3.15,-2.82) {\Large $20$};
\node at (4.45,-2.82) {\Large $0$};
\node at (3.15,-4.12) {\Large $14$};
\node at (4.45,-4.12) {\Large $6$};
\node[anchor=west] at (11.1,-2.35) {$= 20/34 = 0.59$};
\node[anchor=west] at (11.1,-2.95) {$= 20/20 = 1.00$};
\node[anchor=west] at (11.1,-3.55) {$= 26/40 = 0.65$};
\node[anchor=west] at (-0.95,-5.25) {The threshold moves down.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 13.98s = "true", 14.24s = "false", 18.36s = "false", repeats}}}]
\draw[very thick] (1.95,-0.12) -- (1.95,2.2) node[above] {$t = 0.15$};
\node[anchor=east] at (1.89,1.98) {\footnotesize $\hat y = 0$};
\node[anchor=west] at (2.01,1.98) {\footnotesize $\hat y = 1$};
\node at (3.15,-2.82) {\Large $20$};
\node at (4.45,-2.82) {\Large $0$};
\node at (3.15,-4.12) {\Large $16$};
\node at (4.45,-4.12) {\Large $4$};
\node[anchor=west] at (11.1,-2.35) {$= 20/36 = 0.56$};
\node[anchor=west] at (11.1,-2.95) {$= 20/20 = 1.00$};
\node[anchor=west] at (11.1,-3.55) {$= 24/40 = 0.60$};
\node[anchor=west] at (-0.95,-5.25) {The threshold moves down.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 14.24s = "true", 14.5s = "false", 18.36s = "false", repeats}}}]
\draw[very thick] (1.30,-0.12) -- (1.30,2.2) node[above] {$t = 0.10$};
\node[anchor=east] at (1.24,1.98) {\footnotesize $\hat y = 0$};
\node[anchor=west] at (1.36,1.98) {\footnotesize $\hat y = 1$};
\node at (3.15,-2.82) {\Large $20$};
\node at (4.45,-2.82) {\Large $0$};
\node at (3.15,-4.12) {\Large $17$};
\node at (4.45,-4.12) {\Large $3$};
\node[anchor=west] at (11.1,-2.35) {$= 20/37 = 0.54$};
\node[anchor=west] at (11.1,-2.95) {$= 20/20 = 1.00$};
\node[anchor=west] at (11.1,-3.55) {$= 23/40 = 0.58$};
\node[anchor=west] at (-0.95,-5.25) {The threshold moves down.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 14.5s = "true", 14.76s = "false", 18.36s = "false", repeats}}}]
\draw[very thick] (0.65,-0.12) -- (0.65,2.2) node[above] {$t = 0.05$};
\node[anchor=east] at (0.59,1.98) {\footnotesize $\hat y = 0$};
\node[anchor=west] at (0.71,1.98) {\footnotesize $\hat y = 1$};
\node at (3.15,-2.82) {\Large $20$};
\node at (4.45,-2.82) {\Large $0$};
\node at (3.15,-4.12) {\Large $19$};
\node at (4.45,-4.12) {\Large $1$};
\node[anchor=west] at (11.1,-2.35) {$= 20/39 = 0.51$};
\node[anchor=west] at (11.1,-2.95) {$= 20/20 = 1.00$};
\node[anchor=west] at (11.1,-3.55) {$= 21/40 = 0.53$};
\node[anchor=west] at (-0.95,-5.25) {The threshold moves down.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 14.76s = "true", 18.36s = "true", repeats}}}]
\draw[very thick] (0.00,-0.12) -- (0.00,2.2) node[above] {$t = 0.00$};
\node[anchor=east] at (-0.06,1.98) {\footnotesize $\hat y = 0$};
\node[anchor=west] at (0.06,1.98) {\footnotesize $\hat y = 1$};
\node at (3.15,-2.82) {\Large $20$};
\node at (4.45,-2.82) {\Large $0$};
\node at (3.15,-4.12) {\Large $20$};
\node at (4.45,-4.12) {\Large $0$};
\node[anchor=west] at (11.1,-2.35) {$= 20/40 = 0.50$};
\node[anchor=west] at (11.1,-2.95) {$= 20/20 = 1.00$};
\node[anchor=west] at (11.1,-3.55) {$= 20/40 = 0.50$};
\node[anchor=west] at (-0.95,-5.25) {As $t$ falls, recall rises and precision usually falls. Each threshold gives its own matrix.};
\end{scope}
\end{tikzpicture}
\end{document}

(Micro Averaging Equals Accuracy) In a single-label multi-class problem, micro precision, micro recall and micro all equal accuracy.

Suppose a point of true class is predicted as class . It counts as a false negative for class and a false positive for class . Summed over classes, the total FP and the total FN both equal the number of errors. The total TP equals the number of correct predictions. Micro precision is then (correct)/(correct + errors), which is accuracy, and the same holds for recall and . Micro averaging therefore weights each class by its size, and a rare class barely affects it. Macro averaging gives every class the same weight, so a badly handled rare class pulls the score down. Report macro when rare classes matter as much as common ones. Libraries differ on its definition. Some average the per-class scores, and others take the of macro precision and macro recall.

Examples

(Easy) Scoring a Spam Filter

A spam filter screens 1,000 emails, of which 200 are spam. It flags 190 emails, and 170 of them are spam.

(Medium) A Fraud Model That Loses on Accuracy

A bank sees 100,000 card transactions, of which 500 are fraud. Model A labels every transaction legitimate. Model B flags 2,000 transactions, and 400 of them are fraud. Reviewing a flag is cheap, and a missed fraud is expensive.

(Hard) Macro and Micro Averages for Ticket Routing

A support system routes tickets into three queues: billing (100 tickets), technical (50) and cancellation (10). Rows give the true queue and columns the predicted queue, in that order:

See Also