Machine Learning Tutorial 0/98 lessons ~6 min read Lesson 17
Confusion Matrix
A confusion matrix counts prediction outcomes in four buckets: true positive, false positive, true negative, and false negative.
Course progress0%
Focus
9 guided sections
Practice signal
Examples included
Career prep
Foundation builder
Introduction
A confusion matrix counts prediction outcomes in four buckets: true positive, false positive, true negative, and false negative. It is the foundation for precision, recall, and many other classification metrics.
Understanding the topic
Read the grid Rows are actual classes; columns are predicted classes (sklearn default).
Imbalanced data Accuracy alone can hide poor minority-class performance — always inspect the matrix.
- Read the grid — Rows are actual classes; columns are predicted classes (sklearn default).
- Imbalanced data — Accuracy alone can hide poor minority-class performance — always inspect the matrix.
Step-by-step explanation
- Read the grid — Rows are actual classes; columns are predicted classes (sklearn default).
- Imbalanced data — Accuracy alone can hide poor minority-class performance — always inspect the matrix.
Informative example
Python starter:
python
from sklearn.metrics import confusion_matriximport numpy as npy_true = np.array([1, 0, 1, 1, 0, 0])y_pred = np.array([1, 0, 0, 1, 0, 1])print(confusion_matrix(y_true, y_pred).tolist())
Output
[[2, 1], [1, 2]]
Execution workflow
1Confusion Matrix — workflow
1 / 2Read the grid
Rows are actual classes; columns are predicted classes (sklearn default).
Best practices
- Hold out a test set before hyperparameter tuning.
- Scale numeric columns for distance-based models.
- Track multiple metrics — not accuracy alone on skewed labels.
Common mistakes
- Leaking test statistics into preprocessing fit on full data.
- Training on the same rows you report as test performance.
- Chasing complex models before a simple baseline.
Hands-on exercise
Practice:
- Compute TN/FP/FN/TP manually for a tiny example
- Plot with seaborn.heatmap
Summary
Confusion Matrix — TP/FP/TN/FN grid for classifiers.
Ready to mark this lesson complete?Track your journey across the entire course.