Confusion Matrix

Machine Learning
Introduced by
Statistical foundations
Year
20th century
Type
Evaluation visualization
Family
Diagnostic Tool

What does it do?

A confusion matrix is a table that counts how a classifier's predictions fall against the true labels.

In the binary case it has four cells: true positives, false negatives, false positives and true negatives. Accuracy, precision and recall are all computed from these counts, but metrics that depend on predicted scores or on the decision threshold cannot be recovered from a single matrix.

The intuition behind the method

A single summary such as accuracy combines the two kinds of error into one number. The matrix keeps them apart: a false positive is a negative case predicted as positive, and a false negative is a positive case that was missed. When these two cells differ greatly in size or in real-world cost, the matrix shows which error the classifier favours. Because the table is built at one decision threshold, it describes the classifier only at that threshold.

History

The confusion matrix is the standard tabular summary in the classification literature. It shows where a classifier's predictions agree with the true labels and where they do not, and machine learning and data mining textbooks use it as the basis for accuracy, precision and recall. ROC analysis, described by Fawcett (2006), builds on the same four counts, computed across many decision thresholds rather than at one.

When to use it

Build a confusion matrix whenever you evaluate a classifier that outputs class labels at a fixed decision threshold, and before you choose any summary metric. It is especially informative when classes are imbalanced, because it shows whether errors fall on the minority class. For multi-class problems, the full N x N table shows which specific classes are confused with one another. Do not rely on it alone when the question concerns ranking quality or performance across all thresholds; a matrix at one threshold cannot answer that, and threshold-sweeping curves built from the same counts are needed. It does not apply to regression outputs, which have no class labels.

Limitations and considerations

  • Every cell depends on the decision threshold; changing the threshold changes the counts, so one matrix describes one operating point only.
  • Raw counts scale with test set size, so matrices from differently sized test sets are not directly comparable without normalization.
  • For many classes the N x N table becomes difficult to read, and it does not yield a single number for ranking models.
  • A matrix built from hard labels does not show how confident the classifier was, so metrics that need predicted probabilities cannot be recovered from it.

Source conditions do not replace independent scientific review or validation by an analysis engine.

Strengths

  • It keeps false positives and false negatives separate, so the cost of each error type can be read directly rather than inferred from a blended score.
  • Accuracy, precision, recall and specificity are each a ratio of its cells, so one table supports all of them.
  • With imbalanced data, the minority-class row shows how many minority cases are missed, which an overall accuracy figure can hide.
  • In the N x N layout for multi-class problems, the off-diagonal cells show which specific class pairs are confused.

Applications

  • Medical diagnosis evaluation, where examining false negatives (missed diseases) and false positives (incorrect diagnoses) separately is critical.
  • Security and intrusion detection, where understanding missed attacks (false negatives) and false alarms (false positives) informs system tuning.
  • Information retrieval, where the confusion matrix decomposes relevant and non-relevant document classifications.
  • Any classification task where understanding error types is more important than a single number.

How it works

How it works

1

For a binary problem, arrange the counts in a 2x2 table with actual classes as rows and predicted classes as columns. Each cell holds the number of test cases with that combination: true positives (actual positive, predicted positive), false negatives (actual positive, predicted negative), false positives (actual negative, predicted positive) and true negatives (actual negative, predicted negative).

Formula
CM_{i,j}=\#\{\text{tuples of actual class } i \text{ labeled by the classifier as class } j\}
2

Read the off-diagonal cells to see how errors are distributed. A large false positive count means many negative cases are flagged as positive (false alarms). A large false negative count means many positive cases are missed. Comparing the two shows which error type dominates.

Formula
P' = TP + FP,\quad N' = TN + FN
3

The standard metrics follow directly from the four counts: accuracy = (TP + TN) / all cases, precision = TP / (TP + FP), recall = TP / (TP + FN). Each of these is defined at the single threshold that produced the matrix.

Formula
\text{Accuracy} = \frac{TP+TN}{P+N}

Common pitfalls

  • Judging a classifier by accuracy alone when the off-diagonal cells show that most errors fall on one class.
  • Reading an unnormalized multi-class matrix on imbalanced data: large classes dominate the counts and hide poor recall on small classes. Normalizing each row to proportions addresses this.
  • Misreading multi-class cells: precision and recall for a given class are computed by treating that class as positive and all other classes together as negative, not by reading single cells in isolation.
  • Confusing the layout convention. Precision uses the predicted-positive column total and recall uses the actual-positive row total, so transposing the table swaps the meaning of these metrics.
  • Comparing raw matrices from test sets of different sizes without normalization.

Frequently asked questions

What is the relationship between the confusion matrix and ROC curves?

The confusion matrix provides the raw data (TP, FP, FN, TN) from which ROC curves are constructed. An ROC curve plots the true positive rate (computed from the confusion matrix) against the false positive rate (also from the matrix) across different decision thresholds. The confusion matrix is for a single threshold; the ROC curve shows performance across all thresholds.

How do I interpret a confusion matrix for multi-class problems?

For multi-class problems, the confusion matrix is NxN where N is the number of classes. Diagonal elements are correct predictions for each class. Off-diagonal elements show which classes are confused with each other. To compute metrics like precision or recall for class i, treat class i as positive and all others as negative.

Should I normalize the confusion matrix?

Yes, for visual inspection and comparison. Normalize by dividing each row by the row sum to show the proportion of actual samples from each class that were predicted in each way. This makes imbalanced datasets easier to interpret visually.

Related methods

Sources

  1. Everitt, B. S., & Hothorn, T. (2005). A Handbook of Statistical Analyses Using R. Chapman and Hall/CRC.

    Open publication reference
  2. Fawcett, T. (2006). An introduction to ROC analysis. Pattern Recognition Letters, 27(8), 861-874.

    Open DOI reference
  3. [John_D._Kelleher_,_Brian_Mac_Namee_,_Aoife_D'Arcy(z-lib.org), p. 464

  4. The-Morgan-Kaufmann-Series-in-Data-Management-Systems-Jiawei-Han-Micheline-Kamber-Jian-Pei-Data-Mining.-Concepts-and-Techniques-3rd-Edition-Morgan-Kaufmann-2011, p. 41