Course map
Stage 1: Foundations
Stage 2: Machine Learning
Stage 3: Deep Learning
Stage 4: LLMs & Generative AI
Stage 5: Production & Specialization

Part 06 / Topic 1 of 7

The confusion matrix

Accuracy hides which mistakes a classifier makes. The confusion matrix shows all four outcomes:

# examples/01_confusion_matrix_precision_recall_and_f1.py (abridged)
print(confusion_matrix(y_te, pred))
print(classification_report(y_te, pred, digits=3))
confusion matrix [[TN FP] [FN TP]]:
[[247  33]
 [ 63  57]]
              precision    recall  f1-score   support

           0      0.797     0.882     0.837       280
           1      0.633     0.475     0.543       120

    accuracy                          0.760       400

Read row by row: of 120 real churners, the model caught 57 (true positives) and missed 63 (false negatives). Accuracy says 0.760; the matrix says the model misses most churners — a very different headline.

🧪 Quick quiz

  1. 8 true positives, 2 false positives, 4 false negatives. What is precision?
Show the answer

Precision = TP / (TP + FP) = 8/10 = 0.80. (Recall = TP / (TP + FN) = 8/12 ≈ 0.67.)

Done here? The practice set for Part 6 — 4 exercises with solutions — is on the part page.

Search the guide