Confusion Matrix

A confusion matrix is a table that summarizes a classification model's performance by showing how many predictions were correct and incorrect for each class.

Data & AI

For a yes/no problem it has four cells: true positives, true negatives, false positives, and false negatives. The diagonal holds correct predictions; off-diagonal cells reveal exactly where the model gets 'confused.'

It's more informative than a single accuracy number because it shows the type of mistakes being made — for instance, whether a medical test misses sick patients (false negatives) or alarms healthy ones (false positives). Precision, recall, and accuracy are all computed from its values.

Examples

Python

from sklearn.metrics import confusion_matrix

y_true = [1, 0, 1, 1, 0]
y_pred = [1, 0, 0, 1, 1]
print(confusion_matrix(y_true, y_pred))

Output:

[[1 1]
 [1 2]]

Learn it in a lesson

See also

Back to the Dictionary