precision_score, recall_score, f1_score的计算

2024-05-23 22:18
文章标签 计算 recall precision f1 score

本文主要是介绍precision_score, recall_score, f1_score的计算,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1 使用numpy计算true positives等

[python]  view plain copy
  1. import numpy as np  
  2.   
  3. y_true = np.array([011010])  
  4. y_pred = np.array([111001])  
  5.   
  6. # true positive  
  7. TP = np.sum(np.multiply(y_true, y_pred))  
  8. print(TP)  
  9.   
  10. # false positive  
  11. FP = np.sum(np.logical_and(np.equal(y_true, 0), np.equal(y_pred, 1)))  
  12. print(FP)  
  13.   
  14. # false negative  
  15. FN = np.sum(np.logical_and(np.equal(y_true, 1), np.equal(y_pred, 0)))  
  16. print(FN)  
  17.   
  18. # true negative  
  19. TN = np.sum(np.logical_and(np.equal(y_true, 0), np.equal(y_pred, 0)))  
  20. print(TN)  
输出结果:

2
2
1
1

2 使用tensorflow计算true positives等

[python]  view plain copy
  1. import tensorflow as tf  
  2.   
  3. sess = tf.Session()  
  4.   
  5. y_true = tf.constant([011010])  
  6. y_pred = tf.constant([111001])  
  7.   
  8. # true positive  
  9. TP = tf.reduce_sum(tf.multiply(y_true, y_pred))  
  10. print(sess.run(TP))  
  11.   
  12. # false positive  
  13. FP = tf.reduce_sum(tf.cast(tf.logical_and(tf.equal(y_true, 0), tf.equal(y_pred, 1)), tf.int32))  
  14. print(sess.run(FP))  
  15.   
  16. # false negative  
  17. FN = tf.reduce_sum(tf.cast(tf.logical_and(tf.equal(y_true, 1), tf.equal(y_pred, 0)), tf.int32))  
  18. print(sess.run(FN))  
  19.   
  20. # true negative  
  21. TN = tf.reduce_sum(tf.cast(tf.logical_and(tf.equal(y_true, 0), tf.equal(y_pred, 0)), tf.int32))  
  22. print(sess.run(TN))  
输出结果:

2
2
1
1

3 使用sklearn的metrics模块计算precision,recall和f1-score

3.1 数据是list类型

[python]  view plain copy
  1. from sklearn.metrics import precision_score, recall_score, f1_score  
  2.   
  3. y_true = [011010]  
  4. y_pred = [111001]  
  5.   
  6. p = precision_score(y_true, y_pred, average='binary')  
  7. r = recall_score(y_true, y_pred, average='binary')  
  8. f1score = f1_score(y_true, y_pred, average='binary')  
  9.   
  10. print(p)  
  11. print(r)  
  12. print(f1score)  
输出结果:

0.5
0.666666666667
0.571428571429

3.2 数据是ndarray类型

[python]  view plain copy
  1. from sklearn.metrics import precision_score, recall_score, f1_score  
  2. import numpy as np  
  3.   
  4. y_true = np.array([[011],   
  5.                    [010]])  
  6. y_pred = np.array([[111],   
  7.                    [001]])  
  8.   
  9. y_true = np.reshape(y_true, [-1])  
  10. y_pred = np.reshape(y_pred, [-1])  
  11.   
  12. p = precision_score(y_true, y_pred, average='binary')  
  13. r = recall_score(y_true, y_pred, average='binary')  
  14. f1score = f1_score(y_true, y_pred, average='binary')  
  15.   
  16. print(p)  
  17. print(r)  
  18. print(f1score)  
输出结果:

0.5
0.666666666667
0.571428571429
转自:http://blog.csdn.net/blythe0107/article/details/75003890

这篇关于precision_score, recall_score, f1_score的计算的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/996568

相关文章

计算绕原点旋转某角度后的点的坐标

问题: A点(x, y)按顺时针旋转 theta 角度后点的坐标为A1点(x1,y1)  ,求x1 y1坐标用(x,y)和 theta 来表示 方法一: 设 OA 向量和x轴的角度为 alpha , 那么顺时针转过 theta后 ,OA1 向量和x轴的角度为 (alpha - theta) 。 使用圆的参数方程来表示点坐标。A的坐标可以表示为: \[\left\{ {\begin{ar

【云计算 复习】第1节 云计算概述和 GFS + chunk

一、云计算概述 1.云计算的商业模式 (1)软件即服务(SaaS) 有些景区给游客提供烧烤场地,游客需要自己挖坑或者砌烧烤台,然后买肉、串串、烧烤。 (2)平台即服务(PaaS) 有些景区给游客提供烧烤场地,同时搭建好烧烤台,游客只需要自己带食材和调料、串串、烧烤。 (3)基础设施即服务(IaaS) 有些景区给游客提供烧烤场地,同时搭建好烧烤台,还有专门的厨师来烧烤,用户不需要关心前面的所有

什么是dB?dBm、dBc、dBi、dBd怎么计算,有什么区别?

什么是dB?dBm、dBc、dBi、dBd怎么计算,有什么区别? 引言 在电子工程、通信和音频领域,dB(分贝)是一个常见的术语。许多人刚接触时可能会感到困惑,因为它不仅仅是一个简单的单位,还有多种不同的形式,如dBm、dBc、dBi和dBd。这篇文章将详细解释这些概念,并介绍如何计算它们,帮助初学者更好地理解和应用。 什么是dB? dB,即分贝,是一种表示两个数值比值的对数单位。分贝的基

php字符串计算汉字、中英文数字个数

$str = '123abcDEF测试的事发地点';$length = strlen(preg_replace('/[\x00-\x7F]/', '', $str));$arr['en'] = strlen( $str) - $length; //(非中文)$arr['cn'] = intval($length / 3); // 编码GBK,除以2 (中文)print_r($

计算广告:第四章——合约广告

计算广告:第四章——合约广告 一、广告位合约 二、受众定向 1、受众定向方法概览 2、 受众定向标签体系 三、展示量合约 1、流量预测 2、流量塑性 3、在线分配 包括按 CPM 计费的展示量合约广告和按 CPT 结算的广告位合约。   一、广告位合约 按CPT结算广告位合约 缺点:无法做到按受众类型投放广告,无法进行深入的优化效果 优点:强曝光属性带来品牌冲击,或

计算广告:第三章——在线广告产品概览

第三章——在线广告产品概览 一、商业产品的设计原则 二、需求方层级组织及接口 二、供给方管理接口 (1)合约广告产品——主要服务于后续效果不宜直接衡量的品牌类广告主 按时段售卖的CPT广告按约定展示量售卖的CPM广告   (2)竞价广告产品 其形式主要是搜索广告,其产品形式为对搜索关键词的竞价。这种广告拓展到站外广告时,演变为了对页面关键词或者用户标签竞价的产品形式,也就是

计算广告:第二章——计算广告基础

一、广告有效性原理 二、互联网广告的技术特点 1、技术和计算向导 2、效果的可衡量性 3、创意和投放方式的标准化 4、媒体概念的多样化 5、数据驱动的投放决策 三、计算广告的核心问题 1、广告收入的分解 2、结算方式与ECMP估计关系 四、在线广告相关行业协会 五、问题 可衡量的效果以及相应的计算优化是在线广告区别线下广告的主要特点,千次展示期望收入(expect

给定正整数n,计算出n个元素的集合{1,2,....,n}可以划分为多少个不同的非空集合

给定正整数n,计算出n个元素的集合{1,2,....,n}可以划分为多少个不同的非空集合 附源代码: #include<iostream>using namespace std;int F(int n,int m){if(n<=2)return 1;if(m==1||n==m)return 1;elsereturn F(n-1,m-1)+m*F(n-1,m);}void main(

numpy.ndarray数据计算及操作集锦

目录 1. numpy.ndarray各列求均值1.1 列1.2 行 1. numpy.ndarray各列求均值 1.1 列 要对 v_sec_trans 数组的每一列求均值,可以使用 numpy 库中的 mean 函数。以下是具体的代码示例: import numpy as np# 定义 v_sec_trans 数组v_sec_trans = np.array([[ 7.

SQL Plu计算算数表达式及SQL Plus下清屏快捷键

用PL/SQL数据库语言计算sqrt(58+25*3+(19-9)^2)的值 SQL Plus下清屏快捷键是host cls或者 clear screen PL/SQL计算乘方是2个*号: