Domanda

I am making a confusion matrix with sci-kit learn with two different lists: gold_labels and predicted labels

cm = confusion_matrix(gold_labels, predicted_labels)
pl.matshow(cm) #I use pl to generate an image
pl.title('Confusion Matrix')
pl.ylabel('True label')
pl.xlabel('Predicted label')
pl.colorbar()

where gold labels/predicted labels looks something like this: (list of strings)

gold_labels =["hello", "apple".....] 
predicted_labels=["hi", "apple"....]

The confusion matrix is generated and it looks beautiful but the labels are indices (0,1,2) and I can't tell if 0 maps to "hello" or "apple" So, I have two questions: 1) Is there a way to make the labels appear on the generated confusion matrix in pl 2) If not, how do I know what in my list of strings matches to its corresponding index

È stato utile?

Soluzione

Just call the plt.xticks and plt.yticks functions.

First you have to choose where you want your ticks to be on the axis, and then you have to set the labels.

For example: suppose you have an x axis that spans from 5 to 25 and you want 3 ticks at 8, 15 and 22, and you want labels foo, bar, baz.

Then you should call:

# do your plotting first, for example
x = np.arange(5, 25)
y = x * x
plt.plot(x, y)
# and then the ticks
plt.xticks([8, 15, 22], ['foo', 'bar', 'baz'])
# And finally show the plot
plt.show()

In your case, since your labels ticks are at [0, 1, 2] and you want hello, apple and orange as your labels. You should do:

plt.xticks([0, 1, 2], ['hello', 'apple', 'orange'])
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top