1. fpr tpr等五个指标记录

def calculate_fpr_tpr_tnr_f1score_accuracy(y_true, y_pred):'''y_true 和 y_pred均是ndarray类型。'''y_true = y_true.flatten()y_pred = y_pred.flatten()Tp = 0Fp = 0Tn = 0Fn = 0for label, pred in zip(y_true, y_pred):if (label == 0) and (pred == 0):Tp = Tp + 1elif (label == 1) and (pred == 0):Fp = Fp + 1elif (label == 1) and (pred == 1):Tn = Tn + 1elif (label == 0) and (pred == 1):Fn = Fn + 1else:print('something weird with labels')return -1# sys.exit()# calculate precision, recall, accuracy, f1# it's possible for division by zero in some of these cases, so do a try/excepttry:precision = Tp / (Tp + Fp)except:precision = 0try:recall = Tp / (Tp + Fn)except:recall = 0try:accuracy = (Tn + Tp) / (Tn + Tp + Fn + Fp)except:accuracy = 0try:f1Score = 2 * precision * recall / (precision + recall)except:f1Score = 0try:fpr = Fp / (Fp + Tn)except:fpr = 0try:tpr = Tp / (Tp + Fn)except:tpr = 0try:tnr = Tn / (Tn + Fp)except:tnr = 0return (fpr, tpr, tnr, f1Score, accuracy)(fpr, tpr, tnr, f1Score, accuracy) = \calculate_fpr_tpr_tnr_f1score_accuracy(test_label, prediction)

2. ROC曲线画图

ef compute_metrics(probs, yprobs):'''Returnsfpr : Increasing false positive rates such that element i is the false positive rate of predictions with score >= thresholds[i]tpr : Increasing true positive rates such that element i is the true positive rate of predictions with score >= thresholds[i].thresholds : Decreasing thresholds on the decision function used to compute fpr and tpr. thresholds[0] represents no instances being predicted and is arbitrarily set to max(y_score) + 1auc : Area Under the ROC Curve metric'''# calculate AUCauc = roc_auc_score(yprobs, probs)# calculate roc curvefpr, tpr, thresholds = roc_curve(yprobs, probs)return fpr, tpr, thresholds, aucdef find_nearest(array, value):idx = np.searchsorted(array, value, side="left")if idx > 0 and (idx == len(array) or math.fabs(value - array[idx - 1]) < math.fabs(value - array[idx])):return array[idx - 1], idx - 1else:return array[idx], idxdef draw_roc(fpr, tpr, thresholds):# find thresholdtargetfpr = 1e-3_, idx = find_nearest(fpr, targetfpr)threshold = thresholds[idx]recall = tpr[idx]# plot no skillplt.plot([0, 1], [0, 1], linestyle='--')# plot the roc curve for the modelplt.plot(fpr, tpr, marker='.')plt.title('AUC: {0:.3f}\nSensitivity : {2:.1%} @FPR={1:.0e}\nThreshold={3})'.format(auc, targetfpr, recall,abs(threshold)))# show the plotplt.show()

Python分类检测问题的常用指标 - TPR TNR TPR f1-score相关推荐

  1. 什么是性能测试?性能测试目的?性能测试的主要分类以及性能测试的常用指标?

    性能测试的必备条件: 1.要是直接能连接到服务器的网络.不要使用跳板机.堡垒机或者VPN等网络连接方式,同时保证内外网分开. 因为我们要测试的是服务器的性能,这些东西会导致性能测试的结果受到很大的网络 ...

  2. LoadRunner入门教程(4)—web性能测试常用指标

    2019独角兽企业重金招聘Python工程师标准>>> web性能测试常用指标 1.响应时间(Response time) 响应时间就是用户感受软件系统为其服务所耗费的时间,对于网站 ...

  3. python 检测文件编码_[常用] 在Python中检测网页编码

    [常用] 在Python中检测网页编码 在使用Python抓取网页并进行分析时出现这个错误: UnicodeDecodeError: 'utf8' codec can't decode byte 0x ...

  4. 机器学习算法常用指标总结

    点击上方"小白学视觉",选择加"星标"或"置顶" 重磅干货,第一时间送达 机器学习性能评价标准是模型优化的前提,在设计机器学习算法过程中,不 ...

  5. python分类预测降低准确率_【火炉炼AI】机器学习011-分类模型的评估:准确率,精确率,召回率,F1值...

    [火炉炼AI]机器学习011-分类模型的评估:准确率,精确率,召回率,F1值 (本文所使用的Python库和版本号: Python 3.5, Numpy 1.14, scikit-learn 0.19 ...

  6. 【理论 | 代码】机器学习分类与回归性能评估指标大全

    一.回归指标 1.1 均方误差(Mean Squared Error, MSE) MSE 称为均方误差,又被称为 L2 范数损失,该统计参数是预测数据和原始数据对应点误差的平方和的均值,公式如下: M ...

  7. Python分类模型评估

    Python分类模型评估 1 声明 本文的数据来自网络,部分代码也有所参照,这里做了注释和延伸,旨在技术交流,如有冒犯之处请联系博主及时处理. 2 分类模型评估简介 对给给定的分类模型进行质量评估,常 ...

  8. 分类检测分割中的损失函数和评价指标

    文章目录 一.分类 1.1 CrossEntropy Loss 1.2 带权重的交叉熵Loss 1.3 Focal Loss 二.检测 2.1 L1, L2, smooth L1, IoU loss ...

  9. 语义分割的常用指标详解

    1 混淆矩阵 假设有6个类别,L为10个真实标签的取值,P为对应的预测的标签值,先计算对应的n(类别数,这里假设为6)xL+P: bin的值一定是分类数的平方.混淆矩阵先将真实标签和预测标签抻成一维向 ...

最新文章

  1. 从无到有<前端异常监控系统>落地
  2. 机器学习中非平衡数据处理
  3. 5 结构型模式之 - 适配器模式
  4. linux 提取cpio_15. Linux提取RPM包文件(cpio命令)详解
  5. C语言,向函数传递一维数组,调用函数并计算平均成绩
  6. 手把手教你启动若依微服务项目
  7. 合作 | 2018数博会AI全球赛项目征集!提供场景、数据集,总奖金池500万
  8. GNU make manual 翻译(三十八)
  9. Python标准库——走马观花
  10. 小程序开发中遇到的坑
  11. IT项目管理期末复习-西北大学
  12. 实录分享|一篇文章看CNTV的容器化探索和平台搭建
  13. 百度地图 控件——路网地图和影像地图切换
  14. Netty内置处理器整理
  15. nginx根据项目名实现内网地址转发
  16. 论文开题报告的研究基础怎么写?
  17. java简单记事本代码_简单记事本的java程序代码
  18. 职业规划师:如何给自己挑选一个好老板
  19. CancerSubtypes包的介绍(根据生信技能树Jimmy老师分享的乳腺癌分子分型包资料整理)
  20. python自动登录教程_python+selenium实现163邮箱自动登陆的方法

热门文章

  1. 谷歌chrome浏览器ERR_SPDY_PROTOCOL_ERROR错误
  2. 路由器ACL(访问控制列表)的类型及配置
  3. 前端ThinkJS框架解析
  4. HSSFWorkbook下载xls表格模板及导入excel数据
  5. 《黑客与画家》读后感——黑客与画家
  6. 如何姿势优美地招不到合适的程序员?——招不聘独孤九式
  7. React hooks学习 -- “useContext”
  8. Mac上播放 swf 格式文件 小技巧
  9. 甘特图:项目管理中的任务分解工具
  10. java字节码文件学习