
๐๐จ๐ฐ ๐ญ๐จ ๐๐ฉ๐จ๐ญ ๐๐ซ๐ซ๐จ๐ซ๐ฌ ๐ข๐ง ๐๐ฅ๐๐ฌ๐ฌ๐ข๐๐ข๐๐๐ญ๐ข๐จ๐ง ๐๐จ๐๐๐ฅ๐ฌ ๐
Understanding ๐๐๐๐๐ ๐๐๐๐ ๐๐๐ ๐๐ ๐๐๐๐ ๐๐ ๐๐๐๐๐ ๐๐ ๐๐๐๐๐๐๐ to improving performance. Lets learn, how a Confusion Matrix can help with this understanding.
๐๐ก๐๐ญ ๐ข๐ฌ ๐๐ข๐ฌ๐๐ฅ๐๐ฌ๐ฌ๐ข๐๐ข๐๐๐ญ๐ข๐จ๐ง?
๐๐๐ ๐๐๐๐ ๐ ๐๐๐๐๐๐ก๐๐๐ โ๐๐๐๐๐๐ ๐คโ๐๐ ๐ ๐๐๐๐๐ ๐๐๐๐๐๐๐ก๐ ๐๐ ๐๐๐๐๐๐๐๐๐ก ๐๐๐๐ ๐ ๐๐๐๐๐ ๐๐๐ ๐ ๐๐๐ฃ๐๐ ๐๐๐๐ข๐ก. For example, classifying a cat ๐ฑ as a dog ๐ถ.

๐๐ก๐๐ญโ๐ฌ ๐ ๐๐จ๐ง๐๐ฎ๐ฌ๐ข๐จ๐ง ๐๐๐ญ๐ซ๐ข๐ฑ?
It is a table used to describe the ๐๐๐๐๐๐๐๐๐๐๐ ๐๐ ๐ ๐๐๐๐ ๐ ๐๐๐๐๐๐ก๐๐๐ ๐๐๐๐๐ ๐๐ฆ ๐๐๐๐๐๐๐๐๐ ๐๐๐ก๐ข๐๐ ๐๐๐๐๐๐ ๐ค๐๐กโ ๐๐๐๐๐๐๐ก๐๐ ๐๐๐๐๐๐ . It breaks down predictions into:
- ๐๐ซ๐ฎ๐ ๐๐จ๐ฌ๐ข๐ญ๐ข๐ฏ๐๐ฌ (๐๐): Correct positive predictions.
- ๐๐ซ๐ฎ๐ ๐๐๐ ๐๐ญ๐ข๐ฏ๐๐ฌ (๐๐): Correct negative predictions.
- ๐ ๐๐ฅ๐ฌ๐ ๐๐จ๐ฌ๐ข๐ญ๐ข๐ฏ๐๐ฌ (๐ ๐): Incorrectly predicted positives.
- ๐ ๐๐ฅ๐ฌ๐ ๐๐๐ ๐๐ญ๐ข๐ฏ๐๐ฌ (๐ ๐): Incorrectly predicted negatives.
from torchmetrics import ConfusionMatrix
from mlxtend.plotting import plot_confusion_matrix
# 2. Setup confusion matrix
confmat = ConfusionMatrix(num_classes=len(class_names),
task='multiclass')
confmat_tensor = confmat(preds=y_pred_tensor,
target=test_data.targets)
# 3. Plot the confusion matrix
fig, ax = plot_confusion_matrix(
conf_mat=confmat_tensor.numpy(),
class_names=class_names,
figsize=(10, 7)
);
๐๐จ๐ฐ ๐๐๐ง ๐ ๐๐จ๐ง๐๐ฎ๐ฌ๐ข๐จ๐ง ๐๐๐ญ๐ซ๐ข๐ฑ ๐๐๐ฅ๐ฉ ๐๐๐๐ง๐ญ๐ข๐๐ฒ ๐๐ซ๐ซ๐จ๐ซ ๐๐๐ ๐ข๐จ๐ง๐ฌ?
The confusion matrix helps you identify error classes:
โขย ย ๐
๐๐ฅ๐ฌ๐ ๐๐จ๐ฌ๐ข๐ญ๐ข๐ฏ๐๐ฌ (๐
๐): Indicates over-predicting certain classes.
โขย ย ๐
๐๐ฅ๐ฌ๐ ๐๐๐ ๐๐ญ๐ข๐ฏ๐๐ฌ (๐
๐): Shows under-prediction or missed classifications.
By studying the matrix, you can spot patterns like:
โขย ย Which classes are most often confused with others.
โขย ย Whether certain classes are consistently misclassified.
๐๐จ๐ฐ ๐ญ๐จ ๐๐๐๐ ๐ ๐๐จ๐ง๐๐ฎ๐ฌ๐ข๐จ๐ง ๐๐๐ญ๐ซ๐ข๐ฑ?
- ๐๐ข๐๐ ๐จ๐ง๐๐ฅ ๐ฏ๐๐ฅ๐ฎ๐s: Represent correct predictions (the more, the better!).
- ๐๐๐-๐๐ข๐๐ ๐จ๐ง๐๐ฅ ๐ฏ๐๐ฅ๐ฎ๐๐ฌ: Represent incorrect predictions or misclassifications.
- Top-right quadrant โ False Positives (FP).
- Bottom-left quadrant โ False Negatives (FN).
๐น๐๐ ๐๐ฅ๐๐๐๐๐, ๐ โ๐๐โ ๐น๐ ๐๐๐ข๐๐ก ๐๐๐โ๐ก ๐ ๐๐๐๐๐ ๐กโ๐ ๐๐๐๐๐ ๐๐ ๐๐๐ ๐ ๐๐๐ ๐๐๐ ๐๐ก๐๐ฃ๐ ๐๐๐ ๐๐ , ๐คโ๐๐โ is ๐๐๐๐ก๐๐๐๐ for ๐๐๐๐ข๐ ๐๐๐ก๐๐๐ก๐๐๐ ๐๐ ๐๐๐๐๐๐๐ ๐๐๐๐๐๐๐ ๐๐ .
๐๐ข๐ฉ๐ฌ ๐ก
- ๐๐ฌ๐ ๐ฐ๐ข๐ญ๐ก ๐ข๐ฆ๐๐๐ฅ๐๐ง๐๐๐ ๐๐๐ญ๐๐ฌ๐๐ญ๐ฌ: For datasets where some classes have more samples than others, better to use confusion matrix.
- ๐๐ซ๐๐๐ข๐ฌ๐ข๐จ๐ง ๐๐ง๐ ๐๐๐๐๐ฅ๐ฅ: Combine the confusion matrix with Precision and Recall to understand modelโs performance.
- ๐๐ฎ๐ง๐ ๐ญ๐ก๐ซ๐๐ฌ๐ก๐จ๐ฅ๐๐ฌ: If FPs or FNs are too high, consider adjusting your classification thresholds.
- ๐๐จ๐จ๐ค ๐๐จ๐ซ ๐ฉ๐๐ญ๐ญ๐๐ซ๐ง๐ฌ: Are certain classes consistently misclassified? Reviewing your data or refining features for those classes.
#MachineLearning #AI #DataScience #DeepLearning #PyTorch #ConfusionMatrix #Classification #ModelTraining
