混淆矩阵生成 a 和 b 作为标签,但不是我需要的
Confusion matrix generates a and b as labels but not what I need
我有一个 bin,我的标签应该是 '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,而不是下面的点和上面的点
通过模型工作,我观察到一些我认为是解决这个问题的最佳方法。也许有人会觉得这有帮助,所以我只是想解决这个问题。
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'])
我认为 Chetan Vasudevan 的答案就是您要找的。这很简单。
但是,如果您仍在使用此功能,并且您很有可能仍在使用,我只想补充一点,Sklearn 的新实现,它使用 y_true
、y_pred
而不是 cm
,特别需要一个 numpy
数组。您不能再将 python 列表用于 class 标签。由于我已经习惯了旧的,所以我自己一直在为新的而苦苦挣扎。现在函数的签名如下所示:
plot_confusion_matrix(y_true, y_pred, classes,
normalize=False,
title=None,
cmap=plt.cm.Blues)
我一直在使用以下内容:
plot_confusion_matrix(y_true=test_labels, y_pred=predictions,
classes=['dog', 'cats'])
但它会 return 这个 TypeError 说:
TypeError: only integer scalar arrays can be converted to a scalar index
我希望您注意的是,classes
现在必须是一个 numpy
数组。如果您想了解原因,请查看实施中的以下行:
classes = classes[unique_labels(y_true, y_pred)]
我有一个 bin,我的标签应该是 '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,而不是下面的点和上面的点
通过模型工作,我观察到一些我认为是解决这个问题的最佳方法。也许有人会觉得这有帮助,所以我只是想解决这个问题。
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'])
我认为 Chetan Vasudevan 的答案就是您要找的。这很简单。
但是,如果您仍在使用此功能,并且您很有可能仍在使用,我只想补充一点,Sklearn 的新实现,它使用 y_true
、y_pred
而不是 cm
,特别需要一个 numpy
数组。您不能再将 python 列表用于 class 标签。由于我已经习惯了旧的,所以我自己一直在为新的而苦苦挣扎。现在函数的签名如下所示:
plot_confusion_matrix(y_true, y_pred, classes,
normalize=False,
title=None,
cmap=plt.cm.Blues)
我一直在使用以下内容:
plot_confusion_matrix(y_true=test_labels, y_pred=predictions,
classes=['dog', 'cats'])
但它会 return 这个 TypeError 说:
TypeError: only integer scalar arrays can be converted to a scalar index
我希望您注意的是,classes
现在必须是一个 numpy
数组。如果您想了解原因,请查看实施中的以下行:
classes = classes[unique_labels(y_true, y_pred)]