Machine Learning Β· Chapter 25 of 40

Confusion Matrix

A CONFUSION MATRIX shows TRUE vs PREDICTED labels for each class. Four cells for binary classification: TP, FP, TN, FN.

From it you can compute accuracy, precision, recall and F1.

Example 1 (python)
from sklearn.metrics import confusion_matrix
print(confusion_matrix(y_test, y_pred))
Output
[[85  5]
 [ 8 42]]

Rows: true. Columns: predicted.

Example 2 (python)
from sklearn.metrics import classification_report
print(classification_report(y_test, y_pred))
Output
precision recall f1-score support...

Full metrics per class.

Key points

  • Shows true vs predicted labels.
  • TP/FP/TN/FN.
  • Foundation for precision, recall, F1.
  • Read rows as truth, columns as prediction.
πŸ’‘ Note: In imbalanced problems, accuracy alone lies. Always inspect the confusion matrix and per-class metrics.

πŸ“ Quick Quiz

1. Confusion matrix rows represent:

2. False Negative means:

3. In imbalanced problems, prefer: