I want to plot a confusion matrix to visualize the classifer’s performance, but it shows only the numbers of the labels, not the labels themselves:
JavaScript
x
16
16
1
from sklearn.metrics import confusion_matrix
2
import pylab as pl
3
y_test=['business', 'business', 'business', 'business', 'business', 'business', 'business', 'business', 'business', 'business', 'business', 'business', 'business', 'business', 'business', 'business', 'business', 'business', 'business', 'business']
4
5
pred=array(['health', 'business', 'business', 'business', 'business',
6
'business', 'health', 'health', 'business', 'business', 'business',
7
'business', 'business', 'business', 'business', 'business',
8
'health', 'health', 'business', 'health'],
9
dtype='|S8')
10
11
cm = confusion_matrix(y_test, pred)
12
pl.matshow(cm)
13
pl.title('Confusion matrix of the classifier')
14
pl.colorbar()
15
pl.show()
16
How can I add the labels (health, business..etc) to the confusion matrix?
Advertisement
Answer
As hinted in this question, you have to “open” the lower-level artist API, by storing the figure and axis objects passed by the matplotlib functions you call (the fig
, ax
and cax
variables below). You can then replace the default x- and y-axis ticks using set_xticklabels
/set_yticklabels
:
JavaScript
1
16
16
1
from sklearn.metrics import confusion_matrix
2
3
labels = ['business', 'health']
4
cm = confusion_matrix(y_test, pred, labels)
5
print(cm)
6
fig = plt.figure()
7
ax = fig.add_subplot(111)
8
cax = ax.matshow(cm)
9
plt.title('Confusion matrix of the classifier')
10
fig.colorbar(cax)
11
ax.set_xticklabels([''] + labels)
12
ax.set_yticklabels([''] + labels)
13
plt.xlabel('Predicted')
14
plt.ylabel('True')
15
plt.show()
16
Note that I passed the labels
list to the confusion_matrix
function to make sure it’s properly sorted, matching the ticks.
This results in the following figure: