Confusion matrixRight and wrong predictions in a grid.
What really happened down the side, what the model said across the top. Right answers on the diagonal, the two kinds of mistakes in the other corners.
- Answers Is the model any good?
- Your data counts of actual against predicted classes
- Classes 2, sometimes up to about 10
- Gives accuracy, precision, recall
- Excel and Power BI a matrix or PivotTable
What is a confusion matrix?
A confusion matrix counts a model's predictions against what really happened. For two classes it is a 2 by 2 grid: the real class down the side, the predicted class across the top. The two cells on the diagonal are right answers; the two off it are the two kinds of mistakes, and they rarely cost the same. Missing a customer who leaves is not the same as calling one who would have stayed.
A churn model on 1,000 customers, 200 of whom really left. It caught 160 of them, missed 40, and wrongly flagged 54 who stayed.
Why use a confusion matrix?
Because one accuracy number hides how a model is wrong.
- Both mistakes in view. Missed cases and false alarms, side by side.
- Real counts. 40 customers missed, not "4% error".
- Many measures from one grid. Accuracy, precision and recall all come from it.
- Easy to explain. Four boxes, four kinds of outcome.
How does a confusion matrix work?
Four steps build a confusion matrix.
- Step 1 of 4Real and predictedWhat really happened down the side, what the model said across the top.
- Step 2 of 4Count every customerEach customer falls in one cell: 1,000 in all.
- Step 3 of 4Right and wrongThe diagonal is right (green), the other corners are mistakes (red).
- Step 4 of 4Shares of each rowOf the 200 who left, 80% were caught and 20% missed.
What are the parts of a confusion matrix?
Four parts, and each one has a job.
- True positiveleavers the model caught
- False negativeleavers the model missed
- False positivestayers flagged by mistake
- True negativestayers rightly left alone
How do you read a confusion matrix?
Three looks, in this order.
- Read the real leavers200 left: 160 caught, 40 missed. Recall: 80%.
- Read the flagged onesOf 214 flagged, 160 really left: precision 75%.
- Weigh the mistakes54 false alarms cost a discount each; 40 misses cost a customer each.
What kinds of confusion matrix are there?
The same model, three ways.
- Kind 1CountsCustomers in each cell.
- Kind 2Shares of each rowRecall and its opposite, per real class.
- Kind 3One colourDarker for more, without right or wrong colours.
With more than two classes (say, which of five products a customer buys), the grid grows to 5 by 5; the diagonal is still right, and the biggest off-diagonal cells show which classes the model mixes up.
When should you use a confusion matrix?
When a model sorts cases into classes at a fixed cut-off.
- a model predicts classes
- the cut-off is chosen
- the two mistakes cost different amounts
- people need counts, not rates
- the cut-off is still open (an ROC curve)
- the question is whom to contact first (a lift chart)
- the model predicts numbers (a residual plot)
Confusion matrix or accuracy alone?
Four counts, or one share.
Accuracy is the diagonal over the total: (160 + 746) / 1,000 = 91%. When one class is much bigger, accuracy is mostly about that class. The matrix keeps the small class in view. Moving the cut-off along the ROC curve trades one mistake for the other.
How do you make a confusion matrix?
Actual and predicted class per case, in Excel, Power BI or Python.
In Excel, a PivotTable with actual class in Rows, predicted class in Columns and a count in Values makes the grid. In Power BI, the matrix visual does the same, with conditional formatting for colours. In Python, sklearn.metrics.confusion_matrix counts it and ConfusionMatrixDisplay draws it.
What goes wrong with confusion matrices?
The most common mistake, and its fix: swapped axes.
- tools disagree on whether real classes go down or across
- readers swap missed cases and false alarms
| Mistake | Why it hurts | Fix |
|---|---|---|
| Axes not labelled | Mistakes get swapped | Write Actual and Predicted |
| Accuracy only | Small classes vanish | Precision and recall too |
| Rates without counts | 80% of how many? | Show the counts |
| Same colour for right and wrong | The diagonal does not stand out | Right and wrong in two colours |
| Scored on training data | Too good to be true | Held-out data |
Every chart for “Is the model any good?”
Read on, step by step.
Does it fit your data? Try it.
Paste your table into Chart Compass, say what the chart should show, and see the charts that fit, drawn with your own numbers. It runs in your browser, so the data stays with you.