How to create and plot a confusion matrix (or crosstab) from two dataframe columns using pandas in python ?

Published: December 16, 2020

Tags: Python; Pandas; DataFrame;

DMCA.com Protection Status

An example of how to create and plot a confusion matrix (or crosstab) from dataframe columns using pandas in python:

Create a datafrrame

import pandas as pd

data = {'prediction':['a','a','a','b','b','b','c','c','c'], 
        'actual':['a','a','b','b','b','b','b','c','c']}


df = pd.DataFrame(data)

print(df)

  prediction actual
0          a      a
1          a      a
2          a      b
3          b      b
4          b      b
5          b      b
6          c      b
7          c      c
8          c      c

Create a confusion table

contingency_matrix = pd.crosstab(df['prediction'], df['actual'])

print(contingency_matrix)

actual      a  b  c
prediction         
a           2  1  0
b           0  3  0
c           0  1  2

Plot the confusion table

import matplotlib.pyplot as plt
import seaborn as sn

plt.clf()

ax = fig.add_subplot(111)

ax.set_aspect(1)

res = sn.heatmap(contingency_matrix.T, annot=True, fmt='.2f', cmap="YlGnBu", cbar=False)

plt.savefig("crosstab_pandas.png", bbox_inches='tight', dpi=100)

plt.show()

How to create and plot a contingency table (or crosstab) from two dataframe columns using pandas in python ?
How to create and plot a contingency table (or crosstab) from two dataframe columns using pandas in python ?

References

Image

of