C = confusion_matrix(mmask_list, pre_list, labels=['0', '1'])绘制混淆矩阵图并保存
时间: 2023-12-21 14:07:32 浏览: 63
要绘制混淆矩阵图并保存,可以使用matplotlib库中的imshow()函数和colorbar()函数。具体步骤如下:
1. 导入matplotlib库:
```python
import matplotlib.pyplot as plt
```
2. 计算混淆矩阵C:
```python
C = confusion_matrix(mmask_list, pre_list, labels=['0', '1'])
```
3. 使用imshow()函数绘制混淆矩阵图:
```python
plt.imshow(C, interpolation='nearest', cmap=plt.cm.Blues)
```
其中,interpolation='nearest'表示使用最近邻插值法绘制图像,cmap=plt.cm.Blues表示使用蓝色色阶绘制图像。
4. 添加颜色标尺:
```python
plt.colorbar()
```
5. 添加图像标题和轴标签:
```python
plt.title('Confusion Matrix')
plt.xlabel('Predicted Labels')
plt.ylabel('True Labels')
```
6. 保存图像:
```python
plt.savefig('confusion_matrix.png')
```
其中,'confusion_matrix.png'是保存图像的文件名。
综合起来,完整的代码如下:
```python
import matplotlib.pyplot as plt
from sklearn.metrics import confusion_matrix
C = confusion_matrix(mmask_list, pre_list, labels=['0', '1'])
plt.imshow(C, interpolation='nearest', cmap=plt.cm.Blues)
plt.colorbar()
plt.title('Confusion Matrix')
plt.xlabel('Predicted Labels')
plt.ylabel('True Labels')
plt.savefig('confusion_matrix.png')
```
执行完毕后,当前目录下会生成一个名为'confusion_matrix.png'的混淆矩阵图像文件。
阅读全文
相关推荐

















