【发布时间】:2017-06-10 17:01:18
【问题描述】:
我有一个混淆矩阵,我想绘制而不是打印,我从here 获取代码。
这是我稍作修改后采用的功能。
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`.
"""
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)
if normalize:
cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
print("Normalized confusion matrix")
else:
print('Confusion matrix, without normalization')
print(cm)
thresh = cm.max() / 2.
for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
plt.text(j, i, round(cm[i, j],4)*100,
horizontalalignment="center",
color="white" if cm[i, j] > thresh else "black")
plt.tight_layout()
plt.ylabel('True label')
plt.xlabel('Predicted label')
当我尝试使用 plot_confusion_matrix(cm, classes=['Front', 'Left', 'Rear', 'Right'],normalize=True,title='Confusion matrix, without normalization') 绘制图形时
如您所见,left 的预测标签和真实标签的值缺失。当我在没有标准化的情况下尝试这个时,一切似乎都在工作,我可以看到预测标签与真实标签的实际数量。
我正在使用 python 3.5.3 并在 Jupyter Notebook 5.0.0 上运行代码。什么可能导致这个问题?
编辑
函数调用前的cm(混淆矩阵)为
cm = np.array([[20633, 219, 357, 118],
[ 136, 340, 199, 0],
[ 49, 10, 15536, 67],
[ 270, 2, 196, 353]])
plot_confusion_matrix(cm, classes=['Front', 'Left', 'Rear', 'Right'],normalize=True,title='Confusion matrix, without normalization')
【问题讨论】:
-
问题是这是不可重现的。如果没有minimal reproducible example 的问题,谁能知道为什么缺少此值?
-
也许左/左组合设置为 NaN?正如@ImportanceOfBeingErnest 所说,如果没有使用参数(数据、参数)实际调用您的函数,就不可能知道它为什么会有这种行为。
-
编辑了问题以反映 cmets。我希望现在一切都好
标签: python matplotlib plot confusion-matrix