The confusion matrix shows how well a classification model is doing by counting correct and wrong predictions. It breaks down results into four groups: True Positives (TP), True Negatives (TN), False Positives (FP), and False Negatives (FN). This helps us calculate important metrics like precision, recall, and accuracy. These metrics tell us if the model is good at finding the right answers or if it makes many mistakes.
Confusion matrix in ML Python - Model Metrics & Evaluation
Actual \ Predicted | Positive | Negative
-------------------|----------|---------
Positive | TP=50 | FN=10
Negative | FP=5 | TN=35
This matrix means: out of all positive cases, 50 were correctly found (TP), 10 were missed (FN). Out of all negative cases, 35 were correctly identified (TN), and 5 were wrongly marked positive (FP).
Precision tells us how many predicted positives are actually correct. Recall tells us how many real positives the model found. Sometimes, improving one lowers the other.
For example, in email spam detection, high precision is important so good emails are not marked as spam. In disease detection, high recall is important so no sick patient is missed.
Good values mean high precision and recall, close to 1.0 (or 100%). For example, precision = 0.9 and recall = 0.85 means the model is mostly correct and finds most positives.
Bad values are low precision or recall, like precision = 0.4 or recall = 0.3, meaning many wrong positives or many missed positives.
- Accuracy paradox: High accuracy can be misleading if classes are imbalanced. For example, if 95% are negative, a model always predicting negative gets 95% accuracy but is useless.
- Data leakage: If test data leaks into training, metrics look too good but model fails in real life.
- Overfitting: Very high training metrics but poor test metrics mean the model memorizes data, not learns general patterns.
Your model has 98% accuracy but only 12% recall on fraud cases. Is it good for production?
Answer: No. The model misses 88% of fraud cases (low recall), which is dangerous. High accuracy is because most transactions are not fraud, so the model guesses 'no fraud' mostly. You need to improve recall to catch fraud better.
Practice
What does a confusion matrix primarily show in machine learning?
Solution
Step 1: Understand the purpose of a confusion matrix
A confusion matrix shows how many times a model predicted each class correctly or incorrectly.Step 2: Identify what it displays
It displays counts of true positives, true negatives, false positives, and false negatives for each class.Final Answer:
The number of correct and incorrect predictions for each class -> Option AQuick Check:
Confusion matrix = correct and incorrect counts [OK]
- Thinking it shows training speed
- Confusing it with dataset size
- Assuming it shows feature count
Which of the following is the correct way to represent a confusion matrix for a binary classifier?
[[TP, FP],
[FN, TN]]Solution
Step 1: Recall confusion matrix layout for binary classification
The standard layout is: first row has true Positives (TP) and false Positives (FP), second row has false Negatives (FN) and true Negatives (TN).Step 2: Match the given matrix to the layout
The given matrix [[TP, FP], [FN, TN]] matches the standard layout exactly.Final Answer:
true Positives, false Positives in first row; false Negatives, true Negatives in second row -> Option AQuick Check:
Binary confusion matrix = [[TP, FP], [FN, TN]] [OK]
- Swapping rows and columns
- Confusing FP with FN positions
- Mixing up TP and TN placement
Given the confusion matrix for a 3-class classifier:
[[5, 2, 0],
[1, 7, 1],
[0, 2, 6]]What is the total number of correct predictions?
Solution
Step 1: Identify correct predictions in confusion matrix
Correct predictions are on the diagonal: 5 (class 1), 7 (class 2), and 6 (class 3).Step 2: Sum the diagonal values
5 + 7 + 6 = 18 correct predictions in total.Final Answer:
18 -> Option DQuick Check:
Sum diagonal = 18 correct predictions [OK]
- Summing all matrix elements instead of diagonal
- Adding off-diagonal values by mistake
- Ignoring one of the diagonal values
Consider this confusion matrix for a binary classifier:
[[50, 10],
[5, 35]]There is a mistake in interpreting the false Positives (FP). Which is the correct FP count?
Solution
Step 1: Recall FP position in confusion matrix
In a binary confusion matrix [[TP, FP], [FN, TN]], FP is at position [0][1].Step 2: Identify FP value in given matrix
Value at [0][1] is 10, so FP = 10.Final Answer:
10 -> Option CQuick Check:
FP = matrix[0][1] = 10 [OK]
- Confusing FP with FN
- Taking FP as bottom-left value
- Mixing FP with TN or TP
A model's confusion matrix for a 3-class problem is:
[[40, 5, 0],
[3, 30, 7],
[0, 2, 38]]Calculate the overall accuracy of the model.
Solution
Step 1: Calculate total correct predictions
Sum diagonal: 40 + 30 + 38 = 108 correct predictions.Step 2: Calculate total predictions
Sum all elements: (40+5+0) + (3+30+7) + (0+2+38) = 45 + 40 + 40 = 125 total predictions.Step 3: Compute accuracy
Accuracy = correct / total = 108 / 125 = 0.864, rounded to 0.86.Final Answer:
0.86 -> Option BQuick Check:
Accuracy = 108/125 ≈ 0.86 [OK]
- Using only one class's correct count
- Forgetting to sum all matrix elements
- Rounding incorrectly
Ready to prove it?
Pass the test and this pattern moves unknown
