Skip to content
Advertisement

sklearn plot confusion matrix with labels

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

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

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:

enter image description here

Advertisement