Please cite us if you use the software

Example-7 (How to plot via seaborn+pandas)

⚠️ This example is deprecated. You can use plot method from version 3.0

Environment check

Checking that the notebook is running on Google Colab or not.

import sys
try:
  import google.colab
  !{sys.executable} -m pip -q -q install pycm
except:
  pass

Install seaborn,pandas

!{sys.executable} -m pip -q -q install seaborn;
!{sys.executable} -m pip -q -q install pandas;
Keyring is skipped due to an exception: invalid syntax (core.py, line 48)
Keyring is skipped due to an exception: invalid syntax (core.py, line 48)

Plotting

from pandas import *
import seaborn as sns
from pycm import *
import numpy as np
def plot_confusion_matrix(cm,normalize=False, title='Confusion matrix', annot=False, cmap="YlGnBu"):
    if normalize == True:
        df = DataFrame(cm.normalized_matrix).T.fillna(0)
    else:
        df = DataFrame(cm.matrix).T.fillna(0)
    ax = sns.heatmap(df, annot=annot, cmap=cmap)
    ax.set_title(title)
    ax.set(xlabel='Predict', ylabel='Actual')
np.random.seed(100)
x1 = np.random.randint(low=0, high=3, size=20000)
x2 = np.random.randint(low=0, high=3, size=20000)
cm1 = ConfusionMatrix(x1, x2)
plot_confusion_matrix(cm1, title="cm1", annot=True)
plot_confusion_matrix(cm1,normalize=True, title="cm1(Normalized)", annot=True)
x1 = np.random.randint(low=0, high=50, size=2000000)
x2 = np.random.randint(low=0, high=50, size=2000000)
cm2 = ConfusionMatrix(x1, x2)
plot_confusion_matrix(cm2, title="cm2", cmap="Dark2")
plot_confusion_matrix(cm2, normalize=True, title="cm2(Normalized)")