我有一个垃圾箱,我的标签应该是“点”,但是当我生成混淆矩阵时,它会生成称为 a 和 b 的标签,但它不会将标签显示为高于 90 的点和低于 90 的点按照我的垃圾箱。这是我的代码。
print(y_test.values)
cm = confusion_matrix(y_test.values, preds)
def plot_confusion_matrix(cm, classes,
normalize=False,
title='Confusion matrix',
cmap=plt.cm.Blues):
"""
This function prints and plots the confusion matrix.
Normalization can be applied by setting `normalize=True`.
"""
if normalize:
cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
print("Normalized confusion matrix")
else:
print('Confusion matrix, without normalization')
print(cm)
plt.imshow(cm, interpolation='nearest', cmap=cmap)
plt.title(title)
plt.colorbar()
tick_marks = np.arange(len(classes))
plt.xticks(tick_marks, classes, rotation=45)
plt.yticks(tick_marks, classes)
fmt = '.2f' if normalize else 'd'
thresh = cm.max() / 2.
for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
plt.text(j, i, format(cm[i, j], fmt),
horizontalalignment="center",
color="white" if cm[i, j] > thresh else "black")
plt.tight_layout()
plt.ylabel('True label')
plt.xlabel('Predicted label')
plt.figure()
plot_confusion_matrix(cm)
plt.show()
最佳答案
通过该模型,我观察到了一些我认为是解决这个问题的最佳方法的事情。也许有人会觉得这很有帮助,所以我只是想澄清这个问题。
I had created a bin from one label, so the label was points and the bins I created was 'points above 90' and 'points below 90' so these were the labels for the graph which had to show the value instead of 'a' and 'b'. In the above case I had balanced the data well during bin's creation.
因此,我更改了代码以获得混淆矩阵图,如下所示
plt.tight_layout()
plt.ylabel('True label')
plt.xlabel('Predicted label')
plt.figure()
plot_confusion_matrix(cm,['points above 90', 'points below 90'])
关于python - 混淆矩阵生成 a 和 b 作为标签,但不是我需要的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47665606/