【发布时间】:2020-12-18 07:47:54
【问题描述】:
27个类存在多分类问题。
y_predict=[0 0 0 20 26 21 21 26 ....]
y_true=[1 10 10 20 26 21 18 26 ...]
名为“answer_vocabulary”的列表存储了每个索引对应的 27 个单词。 answer_vocabulary=[0 1 10 11 2 3 农商东住北.....]
cm = 混淆矩阵(y_true=y_true, y_pred=y_predict)
我对混淆矩阵的顺序感到困惑。它是按升序排列的吗?如果我想用标签序列= [0 1 2 3 10 11农业商业生活东北...]重新排序混淆矩阵,我该如何实现它?
这是我尝试绘制混淆矩阵的函数。
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, cm[i, j],
horizontalalignment="center",
color="white" if cm[i, j] > thresh else "black")
plt.tight_layout()
plt.ylabel('True label')
plt.xlabel('Predicted label')
【问题讨论】:
标签: machine-learning scikit-learn deep-learning confusion-matrix