python 混淆矩阵画不出图
时间: 2023-09-11 07:08:23 浏览: 334
你可以使用`matplotlib`库来画出混淆矩阵的图形。以下是一个简单的示例代码,可以帮助你绘制混淆矩阵的图形:
```python
import numpy as np
import matplotlib.pyplot as plt
# 创建混淆矩阵
confusion_matrix = np.array([[10, 2, 3],
[4, 15, 1],
[6, 8, 12]])
# 绘制混淆矩阵
fig, ax = plt.subplots()
im = ax.imshow(confusion_matrix, cmap='Blues')
# 设置图形的标题、标签和刻度
ax.set_title("Confusion Matrix")
ax.set_xlabel("Predicted labels")
ax.set_ylabel("True labels")
ax.set_xticks(np.arange(confusion_matrix.shape[1]))
ax.set_yticks(np.arange(confusion_matrix.shape[0]))
ax.set_xticklabels(["Class 0", "Class 1", "Class 2"])
ax.set_yticklabels(["Class 0", "Class 1", "Class 2"])
# 在每个单元格中添加数值
for i in range(confusion_matrix.shape[0]):
for j in range(confusion_matrix.shape[1]):
ax.text(j, i, confusion_matrix[i, j], ha="center", va="center", color="w")
# 添加颜色条
cbar = ax.figure.colorbar(im, ax=ax)
# 显示图形
plt.show()
```
你可以自定义混淆矩阵的数据,并根据需要修改图形的标题、标签和刻度等内容。运行上述代码后,将会显示一个绘制好的混淆矩阵图形。
阅读全文
相关推荐













