【问题标题】:How to get to know the order of actual labels for Confusion Matrix?如何知道混淆矩阵的实际标签顺序?
【发布时间】:2020-05-18 02:54:38
【问题描述】:

我很困惑,我如何知道 Confusion Matrix 中的实际标签?我知道要传递标签,但我的主要问题是我们如何知道我必须传递标签的序列?

from sklearn.metrics import confusion_matrix

cm = confusion_matrix(y_test,y_pred_classes)

这会返回confusion_matrix() 函数的结果:

然后我声明标签并传递标签来绘制混淆矩阵:

import itertools

def plotConfusionMatrix(cm, classes, normalize=False, title='Confusion Matrix', cmap = plt.cm.Blues):

    plt.figure(figsize = (10,7))
    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('Un-normalized Confusion Matrix')

    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', fontsize=25, fontweight='bold')
        plt.tight_layout()
        plt.ylabel('Actual Class')
        plt.xlabel('Predicted Class')

然后调用函数并传递标签:

classes = ['climbingdown','climbingup','jumping','lying','running','sitting','standing','walking']
plotConfusionMatrix(cm, classes)

绘制的混淆矩阵的输出是:

现在,我的确切问题是,我已经通过了每个班级的标签,但我将如何知道我必须通过的顺序?

【问题讨论】:

标签: python machine-learning deep-learning confusion-matrix


【解决方案1】:

您可以将类标签传递给confusion matrix function。

如果您不这样做,它将仅使用标签的排序顺序。所以我想这取决于你的 y_true 和 y_pred 标签是如何映射的。

【讨论】:

  • 我知道了,非常感谢。
猜你喜欢
  • 2020-12-18
  • 2020-01-22
  • 1970-01-01
  • 1970-01-01
  • 2020-07-31
  • 2018-11-06
  • 1970-01-01
  • 2021-11-11
  • 2018-07-05
相关资源
最近更新 更多