AI engineering glossary · Evaluation metrics
What is confusion matrix?
Confusion matrix: A table that counts how a classifier's predictions line up with the true labels. For two classes it has four cells: true positives, false positives, false negatives and true negatives. Nearly every classification metric is computed from these counts, and the table shows which kinds of mistakes a model makes, which a single accuracy number hides.
Confusion matrix: an example
A fraud model reviews 1,000 UPI transactions, 20 of them fraud. It catches 15 (TP), misses 5 (FN), wrongly flags 10 genuine ones (FP) and correctly clears 970 (TN).
Confusion matrix in Python
A short example you can run (Python 3.11, scikit-learn). The output below is what it really prints.
from sklearn.metrics import confusion_matrix, precision_score, recall_score, f1_score
y_true = [1, 0, 1, 1, 0, 0, 1, 0, 1, 0] # 1 = fraud
y_pred = [1, 0, 0, 1, 0, 1, 1, 0, 1, 0]
print(confusion_matrix(y_true, y_pred)) # rows: actual 0, 1; columns: predicted 0, 1
print("precision", precision_score(y_true, y_pred), "recall", recall_score(y_true, y_pred),
"f1", round(f1_score(y_true, y_pred), 3))Output
[[4 1] [1 4]] precision 0.8 recall 0.8 f1 0.8
Related terms
More Evaluation metrics terms
- Continuous ranked probability score
- Cost matrix
- Cranfield paradigm
- Cumulative gains chart
- Davies-Bouldin index
- Diagnostic odds ratio
- Discounted cumulative gain
- Dunn index
Where you learn it
Confusion matrix is part of the AI Engineering Program, which teaches it with notes, a hands-on lab and an exam. The first lesson is free.