Confusion matrix
A grid that counts every prediction by what the thing really was and what the model called it. Built from 50 animal photos, read cell by cell, then grown to three classes.
In 30 seconds
A confusion matrix is a table of counts. Each row is a true class, each column is an answer the model gave, and every example lands in exactly one cell.
Right answers sit on the diagonal. Everything off it is a mistake, and the cell tells you which one: a false positive (said yes, wrongly) or a false negative (said no, wrongly).
Accuracy, precision and recall are all ratios of these cells. With more classes the grid just gets bigger, and shows exactly which classes get mixed up.
Confusion matrix, step by step
Rows are the true class and columns the predicted class here, as in scikit-learn. Some books and tools swap the two, so check the axis labels before reading any confusion matrix.
- Step 1: Answers against the truth. A model looks at 50 photos and answers "cat" or "not cat" for each. We also know what each photo really shows. Sometimes the two agree, sometimes not, and there is more than one way to be wrong: missing a cat is a different mistake from seeing a cat that is not there.
- Step 2: Sort every photo into a grid. Draw a grid with a row for each true class and a column for each answer the model can give. Every photo drops into exactly one cell: its row is what it is, its column is what the model said. Counting each cell gives the confusion matrix.
- Step 3: Name the four cells. Call "cat" the positive class. Positive or negative is what the model said; true or false is whether it was right. So a false negative is a cat the model said no to, and a false positive is a non-cat it said yes to. The two true cells lie on the diagonal.
- Step 4: Read the rows and columns. Each row adds up to what is really there: 20 cats, 30 others. Each column adds up to what the model said: 24 "cat", 26 "not cat". The diagonal holds the 42 correct answers; the 8 mistakes sit off it. Here most are false alarms, 6 against 2 misses.
- Step 5: Metrics from the grid. Every common score is a ratio of cells. Accuracy is the diagonal over everything: 84%. Recall reads along the cat row: of 20 real cats, 18 were found, 90%. Precision reads down the "cat" column: of 24 times it said cat, 18 were right, 75%.
- Step 6: More than two classes. Let the model name the animal: cat, dog or rabbit. The grid grows to 3 by 3, one row and one column per class, and still counts the same 50 photos. The diagonal is still the right answers, 40 of 50. Each off-diagonal cell is one specific mix-up: 5 dogs were called cats.
- Step 7: One class at a time. With many classes, read one at a time. For dog, the row gives recall: 9 of 15 real dogs were found, 60%. The column gives precision: 9 of the 12 "dog" answers were right, 75%. Treating dog as positive and the rest as negative gives it its own TP, FN, FP and TN.
Try it yourself
Learn this first
Nothing. This concept starts from scratch.
Spotted a mistake on this page?