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()
相关推荐
黑子哥呢?30 分钟前
安装Bash completion解决tab不能补全问题
开发语言·bash
失败尽常态52333 分钟前
用Python实现Excel数据同步到飞书文档
python·excel·飞书
2501_9044477435 分钟前
OPPO发布新型折叠屏手机 起售价8999
python·智能手机·django·virtualenv·pygame
青龙小码农35 分钟前
yum报错:bash: /usr/bin/yum: /usr/bin/python: 坏的解释器:没有那个文件或目录
开发语言·python·bash·liunx
大数据追光猿40 分钟前
Python应用算法之贪心算法理解和实践
大数据·开发语言·人工智能·python·深度学习·算法·贪心算法
Leuanghing1 小时前
【Leetcode】11. 盛最多水的容器
python·算法·leetcode
彳卸风1 小时前
Unable to parse timestamp value: “20250220135445“, expected format is
开发语言
dorabighead2 小时前
JavaScript 高级程序设计 读书笔记(第三章)
开发语言·javascript·ecmascript
xinxiyinhe2 小时前
如何设置Cursor中.cursorrules文件
人工智能·python
风与沙的较量丶2 小时前
Java中的局部变量和成员变量在内存中的位置
java·开发语言