Why Accuracy is Not Enough
Imagine a test for a rare disease that only 1 in 100 people have. A lazy model that says healthy to everyone is 99 percent accurate, but it never finds a sick person. That is why we need other measures.
The Confusion Matrix
A confusion matrix counts how many samples were predicted correctly and incorrectly. For a yes or no problem, there are four cells.
| Predicted: Yes | Predicted: No | |
|---|---|---|
| Actual: Yes | True Positive (TP) | False Negative (FN) |
| Actual: No | False Positive (FP) | True Negative (TN) |
Precision, Recall and F1
| Metric | Question it answers | Formula |
|---|---|---|
| Accuracy | How many predictions were right overall? | (TP + TN) / all |
| Precision | When the model says yes, how often is it right? | TP / (TP + FP) |
| Recall | Of all real yes cases, how many did we find? | TP / (TP + FN) |
| F1 score | A balance between precision and recall | 2 x P x R / (P + R) |
Doing It with Code
Plot the Confusion Matrix
The diagonal shows the correct predictions. Bright cells away from the diagonal show which classes the model confuses, for example a 4 predicted as a 9.
Binary Models and Thresholds
Changing the threshold trades precision against recall.
Metrics Directly in Keras
Conclusion
A good data scientist never trusts accuracy alone. Check the confusion matrix and use precision, recall and F1, especially when the classes are not balanced.

