# confusion matrix

*Entry of the [Dictionary of Applied Machine Learning](https://dictionaryofml.org) (course edition), by Alexander Jung, Aalto University. CC BY 4.0. Updated 2026-11-04. [HTML](https://dictionaryofml.org/terms/cm.html) · [typeset PDF](https://dictionaryofml.org/terms/cm.pdf) · [LaTeX](https://dictionaryofml.org/terms/cm.tex) · [Python demo](https://dictionaryofml.org/terms/cm.py) · [notebook](https://dictionaryofml.org/terms/cm.ipynb)*

Math is LaTeX with the book's macros expanded, so no notation package is needed to read it.

## Abstract

The confusion matrix of a hypothesis on a finite dataset with $K$ label values is the $K \times K$ matrix whose entry in row $y$ and column $\widehat{y}$ counts the data points with true label $y$ and prediction $\widehat{y}$. Its diagonal counts the correct predictions, and each off-diagonal entry counts one kind of misclassification. The accuracy, the precision, and the recall are obtained from the matrix by normalizing the diagonal, a column, or a row. On imbalanced data, the confusion matrix exposes failure on the rare class that the accuracy alone hides.

## Definition

The weather station in Krems an der Donau (Austria) records the minimum and the maximum air temperature of each day. A linear classifier is learned by logistic regression from a training set $\mathcal{D}^{(\rm train)}$ of $40$ such data points from February and April 2024, and its decision boundary, a straight line, splits the plane of the two temperatures (Fig. 1). The features of a data point are the two temperatures of one day; its label indicates whether the minimum temperature of the following day stays above $0\,^{\circ}\mathrm{C}$ or falls below it, a frost warning.

![Figure 1 of the entry cm](https://dictionaryofml.org/terms/fig/cm-fig1.svg)

*Figure 1: The $40$ data points of the Krems an der Donau frost warning with the linear decision boundary learned by logistic regression. Each data point is a day's minimum and maximum temperature, marked by whether the following day stays above $0\,^{\circ}\mathrm{C}$ (filled circles) or brings frost (open triangles). Data generated by pythondemos/cm.py*

Most days pose no risk: only $6$ of the $40$ following days bring frost. The constant prediction ``above zero'', a baseline that ignores the features, is therefore correct on $34$ of the $40$ days, an accuracy of $34/40 = 0.85$, and the learned classifier reaches the same accuracy of $0.85$. The single number cannot tell the two apart, nor reveal which mistakes either makes. A classifier with that accuracy might miss the frost days or raise false alarms. The confusion matrix breaks the count of predictions down by what was true and what was predicted (Fig. 2). The learned classifier detects one of the six frost days at the cost of one false alarm, while the baseline detects none and raises none.

![Figure 2 of the entry cm](https://dictionaryofml.org/terms/fig/cm-fig2.svg)

*Figure 2: The confusion matrices of the learned classifier (a) and of the always-above-zero baseline (b) of Fig. 1. Both reach an accuracy of $0.85$; only the confusion matrices show that the learned classifier detects one frost day and raises one false alarm, while the baseline detects none and raises none. The shaded diagonal cells hold the correct predictions. Data generated by pythondemos/cm.py*

Consider a finite dataset with $m$ data points, each characterized by a feature vector ${\mathbf x}$ and a label $y$ from a finite label space $\mathcal{Y} = \{1, \,\ldots, \,K\}$. For a given hypothesis $h$, the confusion matrix is a $K \times K$ matrix with one row per true label value and one column per predicted value (Hastie et al., 2009, Sect. 9.1.2). The entry in row $y$ and column $\widehat{y}$ is the number of data points with true label $y$ and prediction $h({\mathbf x}) = \widehat{y}$. A diagonal entry is one with $\widehat{y} = y$, where the prediction agrees with the label, so the diagonal counts the correctly classified data points, and the off-diagonal entries count each kind of misclassification separately.

The standard classification metrics are ratios of entries of the confusion matrix. The accuracy is the sum of the diagonal divided by $m$. Normalizing a row by its sum yields the fraction of data points of that class that are detected (for the positive class, the recall), while normalizing a column yields the fraction of predictions of that class that are right, the precision. For the learned classifier of Fig. 2, with frost as the positive class, the recall is $1/6$ and the precision is $1/2$. The baseline has a recall of $0$, and its precision is not defined at all, since it never predicts frost (pythondemos/cm.py). On imbalanced data, the confusion matrix exposes what a large accuracy can hide. A classifier that never predicts the rare class fills the row of that class with misclassifications while keeping most predictions correct. The always-above-zero baseline in Fig. 2 does exactly that (see baseline).

All numbers above are computed on the training set $\mathcal{D}^{(\rm train)}$, the $40$ days the classifier was learned from. A frost warning is judged on days that did not belong to $\mathcal{D}^{(\rm train)}$, so the confusion matrix, and every ratio taken from it, must be computed again on a validation set before one classifier is preferred over the other.

**See also:** [classification](https://dictionaryofml.org/terms/classification.html), [accuracy](https://dictionaryofml.org/terms/accuracy.html), precision, recall, imbalanced data, [baseline](https://dictionaryofml.org/terms/baseline.html), [training set](https://dictionaryofml.org/terms/trainset.html), [validation set](https://dictionaryofml.org/terms/valset.html), metric, [label](https://dictionaryofml.org/terms/label.html), label space, [matrix](https://dictionaryofml.org/terms/matrix.html).

## References

- Hastie et al. (2009). The Elements of Statistical Learning: Data Mining, Inference, and Prediction. *Springer Science+Business Media*. <https://doi.org/10.1007/978-0-387-84858-7>

## Cite this entry

```bibtex
@misc{dictml_cm,
  author = {Jung, Alexander},
  editor = {Olioumtsevits, Konstantina and Schnoor, Ekkehard},
  title = {confusion matrix},
  howpublished = {Dictionary of Applied Machine Learning (course edition)},
  year = {2026},
  doi = {10.5281/zenodo.21569296},
  note = {ISBN 978-952-64-3013-3, CC BY 4.0, retrieved 2026-11-11},
  url = {https://dictionaryofml.org/terms/cm.html}
}
```
