37. How to Check If Your ML Model Is Good
37.3 Confusion Matrix
Why. A confusion matrix shows the four buckets: true/false × positive/negative — so nobody argues from a single %
What. Rows = true class, columns = predicted class (sklearn default). You see true positives, false positives, true negatives, false negatives in one grid.
How.
Read the grid
- Print confusion_matrix(y_true, y_pred).
- Circle false negatives (missed fraud) and false positives (false alarms).
- Decide which error hurts your shop more.
from sklearn.metrics import confusion_matrix, ConfusionMatrixDisplay
y_true = [1, 1, 1, 0, 0, 0, 0, 0]
y_pred = [1, 1, 0, 1, 0, 0, 0, 0]
print(confusion_matrix(y_true, y_pred))
# Optional plot in Jupyter:
# ConfusionMatrixDisplay.from_predictions(y_true, y_pred)
Ravindra Bagale's Tip
Draw the confusion matrix on a whiteboard or on paper once by yourself. When you draw the four boxes with your own hand, you will easily remember what each number means.
Ravindra Bagale's Tip – मराठी
Confusion matrix एकदा स्वतः whiteboard वर किंवा कागदावर काढा. चारही boxes स्वतःच्या हाताने काढल्यावर प्रत्येक number चा अर्थ सहज लक्षात राहतो.
Ravindra Bagale's Tip – हिंदी
Confusion matrix को एक बार खुद whiteboard या कागज़ पर बनाओ. चारों boxes अपने हाथ से बनाने पर हर number का मतलब आसानी से याद रहता है.