matplotlib画混淆矩阵与正确率曲线的实例代码
作者:小鱼爱吃肉 时间:2021-09-21 05:41:10
混淆矩阵
混淆矩阵(Confusion Matrix)是机器学习中用来总结分类模型预测结果的一个分析表,是模式识别领域中的一种常用的表达形式。它以矩阵的形式描绘样本数据的真实属性和分类预测结果类型之间的关系,是用来评价分类器性能的一种常用方法。
我们可以通过一个简单的例子来直观理解混淆矩阵
#!/usr/bin/python3.5
# -*- coding: utf-8 -*-
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['FangSong'] #可显示中文字符
plt.rcParams['axes.unicode_minus']=False
classes = ['a','b','c','d','e','f','g']
confusion_matrix = np.array([(99,1,2,2,0,0,6),(1,98,7,6,2,1,1),(0,0,86,0,0,2,0),(0,0,0,86,1,0,0),(0,0,0,1,94,1,0),(0,1,5,1,0,96,8),(0,0,0,4,3,0,85)],dtype=np.float64)
plt.imshow(confusion_matrix, interpolation='nearest', cmap=plt.cm.Oranges) #按照像素显示出矩阵
plt.title('混淆矩阵')
plt.colorbar()
tick_marks = np.arange(len(classes))
plt.xticks(tick_marks, classes, rotation=-45)
plt.yticks(tick_marks, classes)
thresh = confusion_matrix.max() / 2.
#iters = [[i,j] for i in range(len(classes)) for j in range((classes))]
#ij配对,遍历矩阵迭代器
iters = np.reshape([[[i,j] for j in range(7)] for i in range(7)],(confusion_matrix.size,2))
for i, j in iters:
plt.text(j, i, format(confusion_matrix[i, j]),fontsize=7) #显示对应的数字
plt.ylabel('真实类别')
plt.xlabel('预测类别')
plt.tight_layout()
plt.show()
正确率曲线
fig ,ax= plt.subplots()
plt.plot(np.arange(iterations), fig_acc,'b')
plt.plot(np.arange(iterations), fig_realacc, 'r')
ax.set_xlabel('迭代次数')
ax.set_ylabel('正确率(%)')
labels = ["训练正确率", "测试正确率"]
# labels = [l.get_label() for l in lns]
plt.legend( labels, loc=7)
plt.show()
总结
来源:https://blog.csdn.net/yuan0401yu/article/details/88730555
标签:matplotlib,混淆矩阵,曲线
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
PHP使用星号替代用户名手机和邮箱的实现代码
2023-11-17 05:50:02
如何解决python多种版本冲突问题
2023-12-28 06:41:17
![](https://img.aspxhome.com/file/2023/7/82557_0s.jpg)
详解如何利用Python绘制科赫曲线
2023-05-02 15:26:42
![](https://img.aspxhome.com/file/2023/1/78491_0s.png)
ASP函数过滤数组中重复数据方法
2010-01-02 20:32:00
centos7利用yum安装lnmp的教程(linux+nginx+php7.1+mysql5.7)
2023-11-14 11:40:18
[译]艺术和设计的差异 (1)
2009-09-25 12:38:00
Python程序员面试题 你必须提前准备!
2023-09-12 09:52:44
![](https://img.aspxhome.com/file/2023/4/91894_0s.jpg)
python使用turtle绘制分形树
2022-10-21 12:10:23
![](https://img.aspxhome.com/file/2023/2/77662_0s.jpg)
使用wxpy实现自动发送微信消息功能
2022-04-01 21:16:06
![](https://img.aspxhome.com/file/2023/2/78572_0s.jpg)
RS.GETROWS使用详解
2008-01-16 13:21:00
Python 使用tf-idf算法计算文档关键字权重并生成词云的方法
2022-03-28 13:09:18
![](https://img.aspxhome.com/file/2023/5/82965_0s.png)
python求最大连续子数组的和
2022-05-12 07:23:24
Oracle捕获问题SQL解决CPU过渡消耗
2010-07-21 13:14:00
php基于PDO实现功能强大的MYSQL封装类实例
2023-11-16 22:50:27
Python编辑器Pycharm安装配置超详细教程
2023-03-25 14:55:15
![](https://img.aspxhome.com/file/2023/2/101062_0s.jpg)
PHP addcslashes()函数讲解
2023-06-10 01:32:33
![](https://img.aspxhome.com/file/2023/9/55399_0s.png)
sql自动增长标识导致导入数据问题的解决方法
2023-07-04 04:39:22
PyQt5+Pycharm安装和配置图文教程详解
2022-12-20 08:50:26
![](https://img.aspxhome.com/file/2023/0/100880_0s.jpg)
解决Python3用PIL的ImageFont输出中文乱码的问题
2022-09-09 04:49:22
![](https://img.aspxhome.com/file/2023/2/105572_0s.jpg)
Idea开发工具之SpringBoot整合JSP的过程
2023-06-14 19:50:23
![](https://img.aspxhome.com/file/2023/2/69272_0s.png)