Python利用Seaborn绘制多标签的混淆矩阵
作者:SpikeKing 时间:2022-12-12 17:46:22
Seaborn - 绘制多标签的混淆矩阵、召回、精准、F1
导入seaborn\matplotlib\scipy\sklearn等包:
import seaborn as sns
from matplotlib import pyplot as plt
from scipy.special import softmax
from sklearn.metrics import accuracy_score, confusion_matrix, precision_score, recall_score, f1_score
sns.set_theme(color_codes=True)
从dataframe中,获取y_true(真实标签)和y_pred(预测标签):
y_true = df["target"]
y_pred = df['prediction']
计算验证数据整体的准确率acc、精准率precision、召回率recall、F1,使用加权模式average=‘weighted’:
# 准确率acc,精准precision,召回recall,F1
acc = accuracy_score(df["target"], df['prediction'])
precision = precision_score(y_true, y_pred, average='weighted')
recall = recall_score(y_true, y_pred, average='weighted')
f1 = f1_score(y_true, y_pred, average='weighted')
print(f'[Info] acc: {acc}, precision: {precision}, recall: {recall}, f1: {f1}')
计算混淆矩阵:
# 横坐标是真实类别数,纵坐标是预测类别数
cf_matrix = confusion_matrix(y_true, y_pred)
5类矩阵的绘制方案,混淆矩阵、百分比的混淆矩阵、召回矩阵、精准矩阵、F1矩阵:
混淆矩阵是计数,百分比的混淆矩阵是占比
召回矩阵是,每行的和是1,每行代表真实类别数,占比就是召回
精准矩阵是,每列的和是1,每列代表预测列表数,占比就是精准
F1矩阵是按照 2PR/(P+R),注意为0的情况,需要补0,使用np.divide(a, b, out=np.zeros_like(a), where=(b != 0))
代码如下:
# 横坐标是真实类别数,纵坐标是预测类别数
cf_matrix = confusion_matrix(y_true, y_pred)
figure, axes = plt.subplots(2, 2, figsize=(16*1.25, 16))
# 混淆矩阵
ax = sns.heatmap(cf_matrix, annot=True, fmt='g', ax=axes[0][0], cmap='Blues')
ax.title.set_text("Confusion Matrix")
ax.set_xlabel("y_pred")
ax.set_ylabel("y_true")
# plt.savefig(csv_path.replace(".csv", "_cf_matrix.png"))
# plt.show()
# 混淆矩阵 - 百分比
cf_matrix = confusion_matrix(y_true, y_pred)
ax = sns.heatmap(cf_matrix / np.sum(cf_matrix), annot=True, ax=axes[0][1], fmt='.2%', cmap='Blues')
ax.title.set_text("Confusion Matrix (percent)")
ax.set_xlabel("y_pred")
ax.set_ylabel("y_true")
# plt.savefig(csv_path.replace(".csv", "_cf_matrix_p.png"))
# plt.show()
# 召回矩阵,行和为1
sum_true = np.expand_dims(np.sum(cf_matrix, axis=1), axis=1)
precision_matrix = cf_matrix / sum_true
ax = sns.heatmap(precision_matrix, annot=True, fmt='.2%', ax=axes[1][0], cmap='Blues')
ax.title.set_text("Precision Matrix")
ax.set_xlabel("y_pred")
ax.set_ylabel("y_true")
# plt.savefig(csv_path.replace(".csv", "_recall.png"))
# plt.show()
# 精准矩阵,列和为1
sum_pred = np.expand_dims(np.sum(cf_matrix, axis=0), axis=0)
recall_matrix = cf_matrix / sum_pred
ax = sns.heatmap(recall_matrix, annot=True, fmt='.2%', ax=axes[1][1], cmap='Blues')
ax.title.set_text("Recall Matrix")
ax.set_xlabel("y_pred")
ax.set_ylabel("y_true")
# plt.savefig(csv_path.replace(".csv", "_precision.png"))
# plt.show()
# 绘制4张图
plt.autoscale(enable=False)
plt.savefig(csv_path.replace(".csv", "_all.png"), bbox_inches='tight', pad_inches=0.2)
plt.show()
# F1矩阵
a = 2 * precision_matrix * recall_matrix
b = precision_matrix + recall_matrix
f1_matrix = np.divide(a, b, out=np.zeros_like(a), where=(b != 0))
ax = sns.heatmap(f1_matrix, annot=True, fmt='.2%', cmap='Blues')
ax.title.set_text("F1 Matrix")
ax.set_xlabel("y_pred")
ax.set_ylabel("y_true")
plt.savefig(csv_path.replace(".csv", "_f1.png"))
plt.show()
输出混淆矩阵、混淆矩阵(百分比)、召回矩阵、精准矩阵:
F1 Score:
来源:https://blog.csdn.net/caroline_wendy/article/details/125796474
标签:Python,Seaborn,混淆矩阵
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
正则表达式验证IPV4地址功能实例分析
2023-06-13 13:11:05
浅谈PYTHON 关于文件的操作
2022-06-07 19:54:58
python socket模块创建和使用套接字示例详解
2021-12-30 13:05:49
Django框架 querySet功能解析
2021-02-15 16:32:14
SQLServer导出数据到MySQL实例介绍
2024-01-13 08:07:17
Python如何定义有默认参数的函数
2023-08-05 14:38:30
网站渗透常用Python小脚本查询同ip网站
2021-06-12 21:51:28
![](https://img.aspxhome.com/file/2023/1/95501_0s.png)
js动态显示当前日期,时间和星期代码
2007-08-14 12:31:00
vue2.0 中#$emit,$on的使用详解
2023-07-02 16:52:27
用Dreamweaver 4.0编制会议通知
2010-10-20 20:06:00
![](https://img.aspxhome.com/file/UploadPic/20072/200723113127669s.jpg)
vue中插件和组件的区别点及用法总结
2024-05-09 09:30:07
Vue.js -- 过滤器使用总结
2024-05-09 10:41:28
asp删除mssql数据库中没有记录的图片代码
2011-03-11 11:22:00
Python必备技巧之字符数据操作详解
2021-05-07 10:22:20
![](https://img.aspxhome.com/file/2023/8/114778_0s.jpg)
数据库连接字符串的常见问题和解决方法
2008-11-28 15:16:00
CSS中expression的用法
2008-09-11 14:18:00
SQL Server中的文件和文件组介绍
2024-01-14 06:57:45
![](https://img.aspxhome.com/file/2023/3/88243_0s.jpg)
pandas检查和填充缺失值的N种方法总结
2021-03-27 03:25:21
![](https://img.aspxhome.com/file/2023/6/80806_0s.png)
解读JavaScript代码 var ie = !-[1,] 最短的IE判定代码
2011-06-06 10:29:00
使用python 打开文件并做匹配处理的实例
2022-01-15 16:27:30