读取两个列表完成带数值的混淆矩阵画图代码
时间: 2023-11-28 16:48:18 浏览: 51
可以使用Python中的Matplotlib库来绘制混淆矩阵的图像。以下是一个示例代码,它将两个列表作为输入,分别表示实际类别和预测类别,然后绘制混淆矩阵的图像。
```python
import matplotlib.pyplot as plt
import numpy as np
# 定义实际类别和预测类别
actual = [0, 1, 2, 3, 4]
predicted = [0, 1, 2, 3, 4]
# 定义混淆矩阵的数值
confusion_matrix = np.array([[10, 2, 0, 0, 0],
[3, 15, 2, 0, 0],
[0, 2, 20, 1, 0],
[0, 0, 2, 18, 0],
[0, 0, 0, 1, 17]])
# 绘制混淆矩阵的图像
plt.imshow(confusion_matrix, interpolation='nearest', cmap=plt.cm.Blues)
plt.title('Confusion Matrix')
plt.colorbar()
tick_marks = np.arange(len(actual))
plt.xticks(tick_marks, actual)
plt.yticks(tick_marks, predicted)
plt.xlabel('Actual')
plt.ylabel('Predicted')
# 将数值添加到混淆矩阵的每个单元格
thresh = confusion_matrix.max() / 2.
for i, j in itertools.product(range(confusion_matrix.shape[0]), range(confusion_matrix.shape[1])):
plt.text(j, i, format(confusion_matrix[i, j], 'd'),
horizontalalignment="center",
color="white" if confusion_matrix[i, j] > thresh else "black")
plt.tight_layout()
plt.show()
```
该代码将绘制以下混淆矩阵的图像:
```
0 1 2 3 4
-----------------------
0 | 10 2 0 0 0
1 | 3 15 2 0 0
2 | 0 2 20 1 0
3 | 0 0 2 18 0
4 | 0 0 0 1 17
```
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/x-zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)