Model Evaluation: lesson 3 of 7
Model Evaluation
Reading a Confusion Matrix
Interpret true and false class outcomes before relying on summary metrics.
Concept
A confusion matrix organizes binary predictions by actual and predicted class. With fraud as positive, a true positive is detected fraud; a false negative is missed fraud; a false positive is a legitimate transaction flagged as fraud; and a true negative is correctly cleared.
| Actual / Predicted | Positive | Negative |
|---|---|---|
| Positive | True positive | False negative |
| Negative | False positive | True negative |
Always label orientation. In scikit-learn, confusion_matrix(y_true, y_pred) returns rows for actual classes and columns for predicted classes.
from sklearn.metrics import confusion_matrix
matrix = confusion_matrix(y_test, y_pred)
Why It Matters
The matrix reveals error types hidden by one score. Missing fraud and blocking legitimate payments have different costs. In medical screening, a false negative can miss disease; in spam filtering, a false positive can hide a legitimate email. The positive class means the class of interest, not a good outcome.
Read a Concrete Matrix
Suppose fraud is positive and rows are actual classes:
| Actual / Predicted | Fraud | Legitimate |
|---|---|---|
| Fraud | 8 | 2 |
| Legitimate | 12 | 978 |
The model detected eight fraud cases (true positives), missed two (false negatives), incorrectly flagged twelve legitimate transactions (false positives), and cleared 978 correctly (true negatives). The table makes the trade-off visible without yet choosing a summary metric. Moving the threshold can change all four counts.
Translate Each Cell Into a Decision
With fraud as the positive class, true positives are fraud transactions correctly blocked. False positives are legitimate transactions flagged for review, which can create customer friction. True negatives are legitimate transactions allowed normally. False negatives are fraud transactions missed by the system. Rows are actual outcomes; columns are predicted outcomes. Say that orientation aloud before interpreting any matrix, because reversing it changes the meaning of every cell.
Error costs depend on the domain. A fraud team may accept more false positives to reduce missed fraud. A spam filter may care strongly about false positives because hiding a legitimate message is disruptive. A medical screen may treat a missed disease case as especially serious. The matrix does not choose the policy; it makes the consequences visible.
From Matrix to Evaluation
confusion_matrix(y_test, y_pred) returns the counts for a chosen set of labels. Check the label order and state the positive class before presenting it. Now that we can see the kinds of mistakes a classifier makes, the next question is why accuracy alone can hide them.
Best Practices
Define the positive class, label axes, and discuss errors with the decision owner. Threshold changes alter these counts. The next lesson uses this vocabulary to explain why accuracy can fail; it introduces precision, recall, and F1 in depth.
Practice Questions
- Identify the false negative for fraud detection.
- Which error harms a spam filter's legitimate email user?
- Why must matrix orientation be stated?
- How can a threshold change matrix counts?
- In the example, how many fraud cases were missed?
Quick Quiz
- Are rows actual or predicted in scikit-learn? Answer: actual.
- What is a false positive? Answer: predicted positive when actual negative.
- Does positive mean good? Answer: no.
Key Takeaway
Key Takeaways
Confusion matrices make classification mistakes concrete before summary metrics hide their types.
Next Lesson
Next, learn why accuracy can conceal important matrix behavior.
Finish this lesson on your terms
Mark it complete when you have worked through the material and are ready to move on.