Confusion matrix
A confusion matrix is a table that counts a classification model's correct and incorrect predictions for each class, showing exactly which kinds of mistake it makes.
A confusion matrix sets what actually happened against what the model predicted. For a yes-or-no task it is a two-by-two table with four counts: true positives (correctly flagged), false negatives (missed), false positives (false alarms) and true negatives (correctly cleared). Correct predictions sit on the diagonal. For tasks with more classes the table grows to one row and one column per class. Some sources put actual classes in the rows and predictions in the columns; others do the reverse, so it is worth checking the labels.
Take the example on Wikipedia: twelve people are tested for cancer, eight of whom have it. The model correctly identifies six of the eight, misses two, raises one false alarm among the four healthy people and correctly clears the other three. Accuracy is 9 out of 12, or 75%. But the matrix shows more: recall is 6 out of 8, and of the seven people flagged, six really had cancer. Missing two cancers and alarming one healthy person are very different errors, and only the matrix keeps them apart.
The name describes the purpose: the table shows at a glance whether a system is confusing two classes. The idea came from studies of human perception, and early machine learning researchers including Frank Rosenblatt used it to compare how people and machines classified stimuli. In a multi-class problem it reveals patterns, such as an image model that keeps mistaking wolves for huskies but never for cats.
Nearly every classification metric is calculated from the confusion matrix: accuracy, precision, recall, specificity, the F1 score and the Matthews correlation coefficient, which some researchers consider the most informative single number. Looking at the matrix itself, not just a summary score, is the quickest way to see whether a model fails in the ways that matter for a particular use.
Find which axis shows the actual classes and which shows the predictions. The diagonal cells count correct predictions; every cell off the diagonal counts a particular kind of mistake, telling you which actual class was predicted as which other class. Because it shows which classes the model confuses with each other. The off-diagonal cells count exactly how often one class was mistaken for another. Accuracy, precision, recall (also called sensitivity), specificity, the F1 score and the Matthews correlation coefficient are all calculated from its counts. Each emphasises different kinds of error. Yes. With N classes it becomes an N by N table, with one row and one column per class. It is especially useful for spotting which specific pairs of classes the model mixes up.Common questions
How do you read a confusion matrix?
Why is it called a confusion matrix?
What metrics come from a confusion matrix?
Can a confusion matrix be used for more than two classes?
Search for "confusion matrix explained", "true positive false positive table" or "multiclass confusion matrix". Machine learning courses such as Google's Machine Learning Crash Course, library documentation and statistics or medical testing textbooks explain it well. When a study reports only one score for a classifier, look for the full confusion matrix in its appendix or supplementary material.Finding more
Working out what this means for your business?
Aileron Shift builds the systems behind this site: AI newsrooms that research, write and publish on their own, marketing automation and WordPress hosting for companies that need to move faster than their competitors.