Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to plot confusion matrix with string axis rather than integer in python

I am following a previous thread on how to plot confusion matrix in Matplotlib. The script is as follows:

from numpy import * import matplotlib.pyplot as plt from pylab import *  conf_arr = [[33,2,0,0,0,0,0,0,0,1,3], [3,31,0,0,0,0,0,0,0,0,0], [0,4,41,0,0,0,0,0,0,0,1], [0,1,0,30,0,6,0,0,0,0,1], [0,0,0,0,38,10,0,0,0,0,0], [0,0,0,3,1,39,0,0,0,0,4], [0,2,2,0,4,1,31,0,0,0,2], [0,1,0,0,0,0,0,36,0,2,0], [0,0,0,0,0,0,1,5,37,5,1], [3,0,0,0,0,0,0,0,0,39,0], [0,0,0,0,0,0,0,0,0,0,38] ]  norm_conf = [] for i in conf_arr:         a = 0         tmp_arr = []         a = sum(i,0)         for j in i:                 tmp_arr.append(float(j)/float(a))         norm_conf.append(tmp_arr)  plt.clf() fig = plt.figure() ax = fig.add_subplot(111) res = ax.imshow(array(norm_conf), cmap=cm.jet, interpolation='nearest')   for i,j in ((x,y) for x in xrange(len(conf_arr))             for y in xrange(len(conf_arr[0]))):     ax.annotate(str(conf_arr[i][j]),xy=(i,j))  cb = fig.colorbar(res) savefig("confusion_matrix.png", format="png") 

I would like to change the axis to show string of letters, say (A, B, C,...) rather than integers (0,1,2,3, ..10). How can one do that. Thanks.

musa

like image 755
Musa Gabere Avatar asked Apr 28 '11 15:04

Musa Gabere


People also ask

How do you plot confusion matrix with labels in python?

Summary: The best way to plot a Confusion Matrix with labels, is to use the ConfusionMatrixDisplay object from the sklearn. metrics module. Another simple and elegant way is to use the seaborn. heatmap() function.

How do you display confusion matrix in Seaborn?

Plotting Confusion Matrix Using Seaborn We have to plot the confusion matrix to look at the count of correct and incorrect predictions. To plot a confusion matrix, we have to create a data frame of the confusion matrix, and then we can use the heatmap() function of Seaborn to plot the confusion matrix in Python.


1 Answers

Here's what I'm guessing you want: enter image description here

import numpy as np import matplotlib.pyplot as plt  conf_arr = [[33,2,0,0,0,0,0,0,0,1,3],              [3,31,0,0,0,0,0,0,0,0,0],              [0,4,41,0,0,0,0,0,0,0,1],              [0,1,0,30,0,6,0,0,0,0,1],              [0,0,0,0,38,10,0,0,0,0,0],              [0,0,0,3,1,39,0,0,0,0,4],              [0,2,2,0,4,1,31,0,0,0,2],             [0,1,0,0,0,0,0,36,0,2,0],              [0,0,0,0,0,0,1,5,37,5,1],              [3,0,0,0,0,0,0,0,0,39,0],              [0,0,0,0,0,0,0,0,0,0,38]]  norm_conf = [] for i in conf_arr:     a = 0     tmp_arr = []     a = sum(i, 0)     for j in i:         tmp_arr.append(float(j)/float(a))     norm_conf.append(tmp_arr)  fig = plt.figure() plt.clf() ax = fig.add_subplot(111) ax.set_aspect(1) res = ax.imshow(np.array(norm_conf), cmap=plt.cm.jet,                  interpolation='nearest')  width, height = conf_arr.shape  for x in xrange(width):     for y in xrange(height):         ax.annotate(str(conf_arr[x][y]), xy=(y, x),                      horizontalalignment='center',                     verticalalignment='center')  cb = fig.colorbar(res) alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' plt.xticks(range(width), alphabet[:width]) plt.yticks(range(height), alphabet[:height]) plt.savefig('confusion_matrix.png', format='png') 
like image 195
amillerrhodes Avatar answered Oct 04 '22 10:10

amillerrhodes