python - PyTorch 混淆矩阵图
问题描述
我遇到了绘制混淆矩阵的问题。上下线放错了。当我绘制它时,它看起来像这样。
我认为我的代码没有任何问题,因为我完全是从这个 YouTube 中获取的。
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')
# Graphical analytics
cm = confusion_matrix(train_set.targets, train_preds.argmax(dim=1))
names = ('T-shirt/top','Trouser','Pullover','Dress','Coat','Sandal','Shirt','Sneaker','Bag','Ankle boot')
plt.figure(figsize=(10,10))
plot_confusion_matrix(cm, names)
解决方案
您可以手动更改y轴的范围。
plt.ylim(-0.5, len(names) - 0.5)
由于某种原因,估计轴范围的启发式方法不会让您不仅对正在绘制的点感兴趣,而且对两个轴上的 (-0.5; +0.5) 周围的点感兴趣。
最低点的y坐标为 0,最高点的y坐标为len(names) - 1
。
推荐阅读
- asp.net - 在同一端口 iis 上运行多个应用程序的需求是什么
- angular - 整个项目中所有数字的千位角分隔符
- apache-spark - 使用的 YARN vCores : Spark on YARN
- sql-server - 处理 Azure Analysis Services 表格模型时发生意外错误
- java - 如何使用 Websphere V7 的 Jython 将自定义属性动态添加到 JMS 提供程序
- node.js - 如何使用 PhpStorm / WebStorm 连接到远程 nodejs 调试会话?
- animation - 将文本作为类型朗读
- ios - FFmpeg iOS 从 AVFrame 获取 UIImage
- mysql - 如何使用 MySQL 在 Windows 中设置 Master-Master 复制
- ios - Swift/IOS:SCNNode 在 SCNAction 完成后似乎回到了原来的位置,但它的实际位置被 SCNAction 改变了