How it works

A confusion matrix is a table that cross-references the true label of each sample with the model's prediction. In a two-class problem, the table shows four cells: true positives, false positives, true negatives and false negatives. The diagonal contains the correct predictions and the off-diagonal contains the errors, which makes it easy to calculate metrics like precision, recall and F1. In multi-class problems, the matrix expands to one cell per class pair and each sample falls into a single cell, which lets you read the model's behavior class by class and identify where it confuses one label with another. This way the table centralizes all of the model's responses in a single view, instead of requiring several separate reports.

Practical example

A spam detection model receives emails and classifies each one as spam or not spam. For each email you compare the classification with the true label. The cases where the model was correct are in the main cells; the errors are outside them. With this view it is clear whether the model is marking too much as spam or letting spam through. Instead of looking only at the overall accuracy rate, you can see whether the main error is flagging legitimate email as spam or letting spam reach the inbox, which guides which direction to correct the system.

What it's for

The confusion matrix is used to diagnose the behavior of a classifier, especially when classes are imbalanced. High accuracy can mask poor performance on the minority class; the matrix reveals this immediately. By adjusting the decision threshold, for example, you can track how the error distribution changes and choose an operating point that balances the cost of each type of error, which makes it an essential tool before taking a model to production. It also helps explain to the business exactly where the model is making mistakes, which makes it easier to get buy-in for AI-based solutions.

Difference between X and Y

The confusion matrix applies to classification. For regression problems, where the output is a number, the equivalent tool is error metrics, such as mean absolute error. While the matrix shows where the model errs in categories, error metrics show the magnitude of the deviation of numeric values.