如何了解混淆矩阵中实际标签的顺序?

我对如何了解混淆矩阵中的实际标签感到困惑。我知道如何传递标签,但我的主要问题是我们如何知道应该以什么顺序传递标签?

from sklearn.metrics import confusion_matrix
cm = confusion_matrix(y_test, y_pred_classes)

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

Result of confusion_matrix() function

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

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('实际类别')
    plt.xlabel('预测类别')

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

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

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

Plotted Confusion Matrix

现在,我的具体问题是,我已经传递了每个类别的标签,但我如何知道应该以什么顺序传递这些标签?


回答:

您可以将类别标签传递到混淆矩阵函数中。

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

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注