在matplotlib中画简单的混淆矩阵的方法及代码是什么
Admin 2022-06-30 群英技术资讯 513 次浏览
混淆矩阵(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()
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
这篇文章主要介绍了Python字符集和字符编码详情,Python中的字符串是通过unicode来表示的,在底层对应的结构体是PyUnicodeObject,但是具体为什么呢?带着疑问一起学习下面文章内容吧
这篇文章主要介绍了Django表单外键选项初始化的问题及解决方法,需本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,要的朋友可以参考下
为什么会报错“UnicodeEncodeError: & 39;ascii& 39; codec can& 39;t encode characters in position 0-1: ordinal not in ra
Python内置函数-frozenset() 函数。frozenset() 返回一个冻结的集合,冻结后集合不能再添加或删除任何元素。
这篇文章主要为大家介绍了python神经网络学习数据增强及预处理示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008