SwadhyAI Notes

All Python notes

Confusion Matrix 😕

Evaluating Classification

A Confusion Matrix is a table used to evaluate the performance of a classification model. It compares the Actual values with the Predicted values.

🏥 Analogy: The Medical Test

Imagine a test for a rare disease:
- True Positive (TP): Sick person correctly identified as Sick. (Good!)
- True Negative (TN): Healthy person correctly identified as Healthy. (Good!)
- False Positive (FP): Healthy person told they are Sick. (Scary, but usually okay after re-test).
- False Negative (FN): Sick person told they are Healthy. (Dangerous! They go home and get worse).

✨ The 4 Quadrants

True Positive (TP)

Correctly predicted Positive.

False Positive (FP)

Incorrectly predicted Positive (Type I Error).

False Negative (FN)

Incorrectly predicted Negative (Type II Error).

True Negative (TN)

Correctly predicted Negative.

🛠️ Implementation

python
from sklearn import metrics
import matplotlib.pyplot as plt

actual = [1, 0, 1, 1, 0, 0, 1, 0, 1, 0]
predicted = [1, 0, 1, 0, 0, 1, 1, 0, 1, 0]

confusion_matrix = metrics.confusion_matrix(actual, predicted)

cm_display = metrics.ConfusionMatrixDisplay(confusion_matrix = confusion_matrix, display_labels = [False, True])

cm_display.plot()
plt.show()

📊 Key Metrics

  • Accuracy: (TP + TN) / Total - Overall correctness.
  • Precision: TP / (TP + FP) - Accuracy of positive predictions.
  • Recall (Sensitivity): TP / (TP + FN) - Ability to find all positive instances.
  • F1 Score: Harmonic mean of Precision and Recall.

⚠️ The Accuracy Paradox

If 99% of people are healthy, a model that says "Everyone is Healthy" has 99% accuracy but is useless for finding sick people.
Always check Precision and Recall for imbalanced data!