A confusion matrix is used to find out accuracy and precision of a program by comparing predicted with actual outputs for a range of data. We can generate a heatmap (which visually communicates the areas where accuracy is high or low) in Python.
UPDATE: Simple way to show confusion matrix plot is:
from matplotlib import pyplot as plt
from sklearn.metrics import ConfusionMatrixDisplay
labels = ['class_1', 'class_2', 'class_3'] # IMPORTANT: use same as used to train model (maps label indices 0,1,2 to names)
ConfusionMatrixDisplay.from_predictions(y_true, y_predict, display_labels=labels)
plt.show()OLDER STEPS:
These steps were output by Chat GPT - Thanks! ❣️
- Import the necessary libraries:
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn.metrics import confusion_matrix, ConfusionMatrixDisplay- Define the labels (alphabetically sorted) for confusion matrix:
labels = sorted(['Negative', 'Positive'])- Generate the confusion matrix using confusion_matrix function:
y_true = [0, 1, 0, 1, 0, 0, 1, 1]
y_pred = [1, 1, 0, 1, 0, 0, 0, 1]
cm = confusion_matrix(y_true, y_pred, labels=labels)The code below is not required any more, since specifying labels in confusion_matrix automatically takes care of this.
Optional: If a label is not present in both y_true and y_pred but you still need to show it in confidence matrix, then you can add an extra row and column of zeros for it:
cm = np.vstack([cm, [0]*len(labels)])
cm = np.hstack([cm, np.zeros((len(labels)+1, 1))])
# cm = cm.astype(int) # Optional: above 2 lines make cm of type float, so convert back to int
labels.append('extra_label')- Optional: Create a normalized version of the confusion matrix by dividing each element by the sum of the row:
cm_norm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]- Create a confusion matrix image from the normalized confusion matrix:
# 2 ways or doing this:
# 1. using ConfusionMatrixDisplay
plt = ConfusionMatrixDisplay(cm_norm, display_labels=labels).plot()
# or 2. using sns.heatmap
df_cm = pd.DataFrame(cm_norm, index=labels, columns=labels)
plt = sns.heatmap(df_cm, annot=True, cmap='Blues', fmt='.2f') Note: If original data was of type int and you didn't normalize in step 5, then use fmt='d', which will format the numbers
without redundant zeros after decimal.
- Add axis labels and a title using plt.xlabel, plt.ylabel, and plt.title:
plt.xlabel('Predicted')
plt.ylabel('True')
plt.title('Confusion Matrix')
plt.show()This will produce a heatmap with labels and colors according to % prediction. You can customize the color scheme by using a different colormap, such as Reds, Greens, or Oranges. You can also adjust the font size and other properties of the heatmap by passing additional arguments to the sns.heatmap function.
- Accuracy, Precision, Recall
These can be calculated simply using sklearn.metrics.classification_report(y_true, y_pred, target_names=labels).
Old Way:
FP = cm.sum(axis=0) - np.diag(cm) # False Positives
FN = cm.sum(axis=1) - np.diag(cm) # False Negatives
TP = np.diag(cm) # True Positives
TN = cm.sum() - (FP + FN + TP) # True Negatives
metrics_df = pd.DataFrame({'FP':FP, 'FN':FN}, index=labels)
# Accuracy, Precision, Recall in %
metrics_df['Recall'] = 100 * TP/(TP+FN)
metrics_df['Precision'] = 100 * TP/(TP+FP)
metrics_df['Accuracy'] = 100 * (TP+TN)/(TP+FP+FN+TN)
metrics_df.loc['Average'] = metrics_df.mean()