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

Install seaborn,pandas

In [1]:
import sys
!{sys.executable} -m pip -q -q install seaborn;
!{sys.executable} -m pip -q -q install pandas;

Plotting

In [2]:
from pandas import *
import seaborn as sns
from pycm import *
import numpy as np
In [3]:
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')
In [4]:
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)
In [5]:
plot_confusion_matrix(cm1,normalize=True,title="cm1(Normalized)",annot=True)
In [6]:
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")
In [7]:
plot_confusion_matrix(cm2,normalize=True,title="cm2(Normalized)")