我有一个分类箱,我的标签应该是’points’,但是在生成混淆矩阵时,它生成的标签是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()
这是我的图表,显示的是a和b,而不是’90分以下’和’90分以上’
回答:
在研究模型的过程中,我观察到了一些我认为是解决这个问题的更好方法。也许有人会觉得这有帮助,所以我想澄清这个问题。
我从一个标签创建了一个分类箱,所以标签是points,我创建的分类箱是’90分以上’和’90分以下’,所以这些应该是图表的标签,而不是‘a’和’b’。在上面的例子中,我在创建分类箱时很好地平衡了数据。
因此,我修改了我的代码,以便得到如下的混淆矩阵图
plt.tight_layout() plt.ylabel('True label') plt.xlabel('Predicted label') plt.figure() plot_confusion_matrix(cm,['points above 90', 'points below 90'])