python - 混淆矩阵生成 a 和 b 作为标签,但不是我需要的

标签 python machine-learning data-mining

我有一个垃圾箱,我的标签应该是“点”,但是当我生成混淆矩阵时,它会生成称为 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,而不是下面的点和上面的点 enter image description here

最佳答案

通过该模型,我观察到了一些我认为是解决这个问题的最佳方法的事情。也许有人会觉得这很有帮助,所以我只是想澄清这个问题。

I had created a bin from one label, so the label was points and the bins I created was 'points above 90' and 'points below 90' so these were the labels for the graph which had to show the value instead of 'a' and 'b'. In the above case I had balanced the data well during bin's creation.

因此,我更改了代码以获得混淆矩阵图,如下所示

 plt.tight_layout()
 plt.ylabel('True label')
 plt.xlabel('Predicted label')
 plt.figure()
 plot_confusion_matrix(cm,['points above 90', 'points below 90'])

enter image description here

关于python - 混淆矩阵生成 a 和 b 作为标签,但不是我需要的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47665606/

相关文章:

python - 具有可能不精确的纬度/经度坐标的 DBSCAN

algorithm - Web挖掘-分类算法

python - 如何将我自己的错误附加到 Django 表单的 ErrorDict 中?

Python 和 Scipy : How to fit a von mises distribution?

python - IO错误: [Errno 21] Is a directory: '/tmp/speech_dataset/'

image - 在 scikit-learn - csv 文件中生成图像特征数据集

python - 通过反向过滤查询集在 Django 中存在检查

python - 如何在Python中根据属性对二维列表进行分组?

python - Pytorch和多项式线性回归问题

python - 用sklearn对弧度距离矩阵进行DBSCAN?