新闻动态

matplotlib画混淆矩阵与正确率曲线的实例代码

发布日期:2022-03-28 12:22 | 文章来源:站长之家

混淆矩阵

混淆矩阵(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()

总结

到此这篇关于matplotlib画混淆矩阵与正确率曲线的文章就介绍到这了,更多相关matplotlib画混淆矩阵内容请搜索本站以前的文章或继续浏览下面的相关文章希望大家以后多多支持本站!

国外稳定服务器

版权声明:本站文章来源标注为YINGSOO的内容版权均为本站所有,欢迎引用、转载,请保持原文完整并注明来源及原文链接。禁止复制或仿造本网站,禁止在非www.yingsoo.com所属的服务器上建立镜像,否则将依法追究法律责任。本站部分内容来源于网友推荐、互联网收集整理而来,仅供学习参考,不代表本站立场,如有内容涉嫌侵权,请联系alex-e#qq.com处理。

相关文章

实时开通

自选配置、实时开通

免备案

全球线路精选!

全天候客户服务

7x24全年不间断在线

专属顾问服务

1对1客户咨询顾问

在线
客服

在线客服:7*24小时在线

客服
热线

400-630-3752
7*24小时客服服务热线

关注
微信

关注官方微信
顶部