Skip to content

Instantly share code, notes, and snippets.

@sohang3112
Last active July 23, 2026 01:40
Show Gist options
  • Select an option

  • Save sohang3112/10b878a7562f0beac1fbefefad00af7a to your computer and use it in GitHub Desktop.

Select an option

Save sohang3112/10b878a7562f0beac1fbefefad00af7a to your computer and use it in GitHub Desktop.
Generate a Confusion Matrix Heatmap using Python

Confusion Matrix

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! ❣️

  1. Import the necessary libraries:
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn.metrics import confusion_matrix, ConfusionMatrixDisplay
  1. Define the labels (alphabetically sorted) for confusion matrix:
labels = sorted(['Negative', 'Positive'])
  1. 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')
  1. 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]
  1. 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.

  1. 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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment