python分类指标评测

python 复制代码
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.metrics import roc_curve, auc, confusion_matrix, \
    precision_recall_curve, average_precision_score
from sklearn.metrics import roc_auc_score
# 生成假数据
y_true = [0, 1, 0, 1, 1, 0]
y_pred = [0.2, 0.6, 0.3, 0.8, 0.2, 0.1]

# 计算AUC
fpr, tpr, thresholds = roc_curve(y_true, y_pred)
roc_auc = auc(fpr, tpr)

# 绘制ROC曲线
plt.title('Receiver Operating Characteristic')
plt.plot(fpr, tpr, 'b', label='AUC = %0.2f' % roc_auc)
plt.legend(loc='lower right')
plt.plot([0, 1], [0, 1], 'r--')
plt.xlim([-0.1, 1.1])
plt.ylim([-0.1, 1.1])
plt.ylabel('True Positive Rate')
plt.xlabel('False Positive Rate')
plt.show()

# 计算混淆矩阵
tn, fp, fn, tp = confusion_matrix(y_true, [1 if i > 0.5 else 0 for i in y_pred]).ravel()

# 绘制混淆矩阵图
labels = ['True Negative', 'False Positive', 'False Negative', 'True Positive']
categories = ['Negative', 'Positive']
sns.heatmap([[tn, fp], [fn, tp]], annot=True, fmt='d', xticklabels=categories, yticklabels=categories, cmap="YlGnBu")
plt.xlabel('Predicted Label')
plt.ylabel('True Label')
plt.title('Confusion Matrix')
plt.show()

# 计算Precision-Recall曲线和AUC
precision, recall, thresholds = precision_recall_curve(y_true, y_pred)
average_precision = average_precision_score(y_true, y_pred)

# 绘制Precision-Recall曲线图
plt.step(recall, precision, color='b', alpha=0.2,
         where='post')
plt.fill_between(recall, precision, step='post', alpha=0.2,
                 color='b')
plt.xlabel('Recall')
plt.ylabel('Precision')
plt.ylim([0.0, 1.05])
plt.xlim([0.0, 1.0])
plt.title('Precision-Recall curve: AP={0:0.2f}'.format(average_precision))
plt.show()

plt.show()
相关推荐
滴啦嘟啦哒几秒前
【机械臂】【视觉】一、加入摄像机并实现世界坐标与像素坐标的互相转换
python·深度学习·vla
程序炼丹师几秒前
stat函数的核心作用与详细解析
开发语言·c++
YJlio1 分钟前
Sysinternals 学习笔记(15.0):系统信息工具总览——RAMMap、RU、CoreInfo 等一网打尽
开发语言·笔记·python·学习·django·pdf·硬件架构
Root_Smile3 分钟前
【Python】pip freeze用法
开发语言·python·pip
csbysj20203 分钟前
XML与HTML:结构化数据的基石
开发语言
qq_12498707534 分钟前
基于Hadoop的黑龙江旅游景点推荐系统的设计与实现(源码+论文+部署+安装)
大数据·hadoop·分布式·python·信息可视化
比奇堡派星星5 分钟前
Linux 平台设备驱动框架详解
linux·开发语言·驱动开发
2501_941878745 分钟前
从限流策略到系统节奏感的互联网工程语法设计与多语言实践随笔分享
java·开发语言
yangpipi-10 分钟前
《C++并发编程实战》第6章 设计基于锁的并发数据结构
开发语言·数据结构·c++
不过如此195112 分钟前
Jira系统中JQL语句的介绍
python·sql·jira