In Machine Learning, a Confusion Matrix is a table used to evaluate a classification model.
It shows:
Correct predictions
Wrong predictions
Confusion Matrix Structure
For a binary classification problem:
Predicted Yes Predicted No
Actual Yes True Positive (TP) False Negative (FN)
Actual No False Positive (FP) True Negative (TN)
Meaning of Terms
1. True Positive (TP)
Model predicted Yes, and actual answer is also Yes.
Example:
Student actually got a job
Model predicted “Yes”
Correct prediction.
2. True Negative (TN)
Model predicted No, and actual answer is also No.
Example:
Student did not get a job
Model predicted “No”
Correct prediction.
3. False Positive (FP)
Model predicted Yes, but actual answer is No.
Example:
Model says student got job
Actually student did not
Wrong prediction.
4. False Negative (FN)
Model predicted No, but actual answer is Yes.
Example:
Model says student did not get job
Actually student got job
Wrong prediction.
Example Confusion Matrix
[[8 2]
[1 9]]
This means:
Predicted No Predicted Yes
Actual No 8 2
Actual Yes 1 9
Interpretation:
8 → Correctly predicted No
9 → Correctly predicted Yes
2 → Wrongly predicted Yes
1 → Wrongly predicted No
In Python
Using scikit-learn:
from [Link] import confusion_matrix
cm = confusion_matrix(y_test, y_pred)
print(cm)
Visual Understanding
Predicted
No Yes
Actual No TN FP
Actual Yes FN TP
Why Confusion Matrix is Important
It helps understand:
Where the model is making mistakes
Whether false positives or false negatives are high
Real performance beyond simple accuracy
Relation with Metrics
From confusion matrix:
Accuracy
Precision
Recall
F1-score
are calculated.
import pandas as pd