0

我正在使用 sci-kit learn 制作一个混淆矩阵,其中包含两个不同的列表:gold_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()

黄金标签/预测标签看起来像这样:(字符串列表)

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

生成了混淆矩阵,它看起来很漂亮,但标签是索引 (0,1,2),我不知道 0 是映射到“hello”还是“apple”所以,我有两个问题:1) 是否有使标签出现在pl 2中生成的混淆矩阵上的方法)如果没有,我怎么知道我的字符串列表中的内容与其对应的索引匹配

4

1 回答 1

1

只需调用plt.xticksplt.yticks函数。

首先,您必须选择您希望刻度在轴上的位置,然后您必须设置标签。

例如:假设您有一个从tox跨越的轴,并且您希望在、和处有 3 个刻度,并且您需要标签、、。52581522foobarbaz

然后你应该打电话:

# 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()

在您的情况下,因为您的标签刻度在[0, 1, 2]并且您想要helloapple并且orange作为您的标签。你应该做:

plt.xticks([0, 1, 2], ['hello', 'apple', 'orange'])
于 2013-08-23T18:10:44.603 回答