Skip to content

Instantly share code, notes, and snippets.

@mesquita
Last active July 26, 2024 13:52
Show Gist options
  • Save mesquita/f6beffcc2579c6f3a97c9d93e278a9f1 to your computer and use it in GitHub Desktop.
Save mesquita/f6beffcc2579c6f3a97c9d93e278a9f1 to your computer and use it in GitHub Desktop.
Nice Confusion Matrix with percentage cbar
from matplotlib.ticker import PercentFormatter
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.metrics import confusion_matrix
def cm_analysis(y_true, y_pred, filename, labels, classes, ymap=None, figsize=(17,17)):
"""
Generate matrix plot of confusion matrix with pretty annotations.
The plot image is saved to disk.
args:
y_true: true label of the data, with shape (nsamples,)
y_pred: prediction of the data, with shape (nsamples,)
filename: filename of figure file to save
labels: string array, name the order of class labels in the confusion matrix.
use `clf.classes_` if using scikit-learn models.
with shape (nclass,).
classes: aliases for the labels. String array to be shown in the cm plot.
ymap: dict: any -> string, length == nclass.
if not None, map the labels & ys to more understandable strings.
Caution: original y_true, y_pred and labels must align.
figsize: the size of the figure plotted.
"""
sns.set(font_scale=2.8)
if ymap is not None:
y_pred = [ymap[yi] for yi in y_pred]
y_true = [ymap[yi] for yi in y_true]
labels = [ymap[yi] for yi in labels]
cm = confusion_matrix(y_true, y_pred, labels=labels)
cm_sum = np.sum(cm, axis=1, keepdims=True)
cm_perc = cm / cm_sum.astype(float) * 100
annot = np.empty_like(cm).astype(str)
nrows, ncols = cm.shape
for i in range(nrows):
for j in range(ncols):
c = cm[i, j]
p = cm_perc[i, j]
if i == j:
s = cm_sum[i]
annot[i, j] = '%.2f%%\n%d/%d' % (p, c, s)
#elif c == 0:
# annot[i, j] = ''
else:
annot[i, j] = '%.2f%%\n%d' % (p, c)
cm = confusion_matrix(y_true, y_pred, labels=labels, normalize='true')
cm = pd.DataFrame(cm, index=labels, columns=labels)
cm = cm * 100
cm.index.name = 'True Label'
cm.columns.name = 'Predicted Label'
fig, ax = plt.subplots(figsize=figsize)
plt.yticks(va='center')
sns.heatmap(cm, annot=annot, fmt='', ax=ax, xticklabels=classes, cbar=True, cbar_kws={'format':PercentFormatter()}, yticklabels=classes, cmap="Blues")
plt.savefig(filename, bbox_inches='tight')
@pingaowang
Copy link

Super !! Thanks a lot!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment