当前位置:网站首页>【笔记】混淆矩阵和ROC曲线
【笔记】混淆矩阵和ROC曲线
2022-08-03 03:05:00 【Sprite.Nym】
一、混淆矩阵
| 预测为真 | 预测为假 | |
|---|---|---|
| 真实为真 | TP (True Positive) | FN (False Negative) |
| 真实为假 | FP (False Positive) | TN (True Negative) |
案例:
真实值:0 1 1 0 1 1 0 0 1 0 1 0 1 0 0
预测值:1 1 1 1 1 0 0 0 0 0 1 1 1 0 1
二、重要指标
(1)精确率(precision、positive predictive value、ppv):
TP / (TP + FP)
即鉴定为阳性的所有数据中,确实为阳性的数据所占比。衡量算法的精确度。
(2)召回率(recall、sensitivity(敏感度)、True Positive Rate(真阳性率/TPR)):
TP / (TP + FN)
即所有确实为阳性的数据中被鉴定出来的比率。
(3)特异度(specificity、True Negative Rate(真阴性率/TNR)):
TN / (TN + FP)
即所有确实为阴性的数据中被鉴定出来的比率。
(4)假阳性率(False Positive Rate(FPR)):
FP / (FP + TN)
即所有确实为阴性的数据中被鉴定成阳性的比率。
(5)F1值:精确率和召回率的调和平均数。F1值越接近1,模型输出越好。
2 1 p r e c i s i o n + 1 r e c a l l \Large \frac 2 {\frac 1 {precision} + \frac 1 {recall}} precision1+recall12
三、ROC曲线
在已经通过算法计算得到预测概率的前提下,每确定一个概率阈值,都可以产生一组新的预测值,每组新的预测值都可以和测试集中的真实值计算出一个新的混淆矩阵和对应的TPR、FPR。改变阈值,得到很多对不同的TPR、FPR后,以FPR为X坐标,TPR为Y坐标绘制成折线图,即得到ROC曲线。ROC曲线和X轴与X=1这两条线围成的图形面积叫AUC值,Area Under Curve。
在sklearn中绘制ROC曲线(数据事先准备):
# 导入metrics
from sklearn import metrics
# 定义一个绘图函数
def show_roc(y_test, scores, pos_label):
# 得到FPR、TPR和阈值
fpr, tpr, threshold = metrics.roc_curve(y_test, scores, pos_label=pos_label)
plt.plot(fpr, tpr, color='green')
# 绘制对角线
plt.plot(np.linspace(0,1,10),np.linspace(0,1,10),color='red',ls='--')
# 加X轴Y轴标签
plt.xlabel('FPR')
plt.ylabel('TPR')
# 加名字
plt.title('ROC space')
plt.show()
# 调用函数
show_roc(y_test, scores, 1)

边栏推荐
猜你喜欢
随机推荐
370万欧元!西班牙iPronics加速可重构光子芯片商用
AttributeError: module ‘xxx‘ has no attribute
C语言实验十三 指针(三)
Fiddler基本使用
为什么要使用 playwright 做浏览器自动化测试?
重定向printf到USB CDC、串口2
ClickHouse—入门
问题记录:jenkins构建时报错The goal you specified requires a project to execute but there is no POM in...
How to write test cases in software testing technology (2)
【leetcode热题Hot100】——任务调度器
ROS计算图——rqt_graph
记录学习--Navicat使用自定义数据库列表
leetcode:139. 单词拆分
MySQL-多表查询
SPI机制是什么?
ROS2自学笔记:机器视觉基础
利用索引机制进行绕过
IPv4编址;A类、B类、C类、D类、E类IP地址(IP地址;网络地址和主机地址;子网掩码;网关;广播地址;)
els 计分
Jincang Database OCCI Migration Guide (5. Program Development Example)









