Confusion Matrix
A confusion matrix counts how predictions compare with known classes. It exposes errors that one accuracy number can hide.
Understand the idea
Specify label order so rows and columns have a stable meaning. In scikit-learn, rows are true classes and columns are predicted classes. Precision and recall answer different questions, and their importance depends on the cost of each error. The example has equally explicit binary labels to keep the four cells interpretable.
Where to try it
Run locally with scikit-learn and NumPy installed in your virtual environment (py -m pip install scikit-learn numpy). These examples are executed during course review, but the online runner does not currently preload scikit-learn. The small data is for learning the API, not evidence of production model quality.
A worked example
from sklearn.metrics import confusion_matrix
actual = [0, 0, 1, 1, 1]
predicted = [0, 1, 1, 0, 1]
print(confusion_matrix(actual, predicted, labels=[0, 1]).tolist())
Read the result
The matrix is [[1, 1], [1, 2]]: one true negative, one false positive, one false negative, and two true positives.
Try one change
Compute precision and recall for class 1 from the counts before using the metric functions.
For package options, see the scikit-learn documentation.