原因参见老师的博客:基于序的评价指标 (特别针对推荐系统和多标签学习)

Peak-F1_11​

由于算法的输出数值类型的,所以阈值的确定影响着算法的效果。
由二分类问题中的分类结果混淆矩阵引申,得到F1F_1F1​在该算法中的最大值称为Peak−F1Peak-F_1Peak−F1​.
Python实现:

    def compute_peak_f1(self):temp_predict_vector = self.predict_prob_matrix.reshape(-1)temp_predict_sort_index = np.argsort(-temp_predict_vector)temp_test_target_vector = self.test_target.reshape(-1)temp_test_target_sort = temp_test_target_vector[temp_predict_sort_index]temp_f1_list = []TP_FN = np.sum(self.test_target > 0)for i in range(temp_predict_sort_index.size):TP = np.sum(temp_test_target_sort[0:i + 1] == 1)P = TP / (i + 1)R = TP / TP_FNtemp_f1 = 0if (P + R) != 0:temp_f1 = 2.0 * P * R / (P + R)passtemp_f1_list.append(temp_f1)passtemp_f1_list = np.array(temp_f1_list)temp_max_f1_index = np.argmax(temp_f1_list)peak_f1 = np.max(temp_f1_list)threshold_value = temp_predict_vector[temp_max_f1_index]self.threshold_value = threshold_valueprint("compute_peak_f1:", peak_f1)return peak_f1pass

直接从算法的代码中粘贴过来了。
predict_prob_matrix是模型预测的数值结果,test_target是测试集对应的真正值。
matlab代码:

function [score] = F1(label_prob, label_target)
[sortArray,temp] = sort(-label_prob);
allLabelSort = label_target(temp);
tempF1 = zeros(1, numel(temp));
allTP = sum(label_target == 1);for i = 1: numel(temp)sliceArray = allLabelSort(1:i); TP = sum(sliceArray == 1);P = TP / (i);R = TP / allTP;if(P + R == 0)tempF1(i) = 0;elsetempF1(i) = (2.0 * P * R) / (P + R);end
end
score = max(tempF1);
end

auc:

代码一:

    def compute_auc(self):temp_predict_vector = self.predict_prob_matrix.reshape(-1)temp_test_target_vector = self.test_target.reshape(-1)temp_predict_sort_index = np.argsort(temp_predict_vector)M, N = 0, 0for i in range(temp_predict_vector.size):if temp_test_target_vector[i] == 1:M += 1else:N = N + 1passpasssigma = 0for i in range(temp_predict_vector.size - 1, -1, -1):if temp_test_target_vector[temp_predict_sort_index[i]] == 1:sigma += i + 1passpassauc = (sigma - (M + 1) * M / 2) / (M * N)print("compute_auc:", auc)return auc

代码二:

    def computeAUC(self):tempProbVector = self.predict_prob_matrix.reshape(-1)tempTargetVector = self.test_target.reshape(-1)auc = metrics.roc_auc_score(tempTargetVector, tempProbVector)print("computeAUC:", auc)return auc

matlab实现:

function [result] = AUC(output, test_targets)
[A,I]=sort(output);
M=0;N=0;
for i=1:length(output)if(test_targets(i)==1)M=M+1;elseN=N+1;end
end
sigma=0;
for i=M+N:-1:1if(test_targets(I(i))==1)sigma=sigma+i;end
end
result=(sigma-(M+1)*M/2)/(M*N);
end

ndcg:

python实现
代码一:

    def compute_ndgc(self):temp_predict_vector = self.predict_prob_matrix.reshape(-1)temp_test_target_vector = self.test_target.reshape(-1)temp_predict_sort_index = np.argsort(-temp_predict_vector)temp_predict_target_sort = temp_test_target_vector[temp_predict_sort_index]temp_target_sort = np.sort(temp_test_target_vector)temp_target_sort = np.flipud(temp_target_sort)dcg = 0;for i in range(temp_predict_vector.size):rel = temp_predict_target_sort[i]denominator = math.log2(i + 2)dcg += rel / denominatoridcg = 0for i in range(temp_predict_vector.size):rel = temp_target_sort[i]denominator = math.log2(i + 2)idcg += rel / denominatorndcg = dcg / idcgprint("compute_ndgc: ", ndcg)return ndcg

代码二

    def computeNDCG(self):# 获得概率序列与原目标序列tempProbVector = self.predict_prob_matrix.reshape(-1)tempTargetVector = self.test_target.reshape(-1)# 按照概率序列排序原1/0串temp = np.argsort(-tempProbVector)allLabelSort = tempTargetVector[temp]# 获得最佳序列: 1111...10000...0sortedTargetVector = np.sort(tempTargetVector)[::-1]# compute DCG(使用预测的顺序, rel是真实顺序, 实际是111110111101110000001000100DCG = 0for i in range(temp.size):rel = allLabelSort[i]denominator = np.log2(i + 2)DCG += (rel / denominator)# compute iDCG(使用最佳顺序: 11111111110000000000)iDCG = 0for i in range(temp.size):rel = sortedTargetVector[i]denominator = np.log2(i + 2)iDCG += (rel / denominator)ndcg = DCG / iDCGprint("computeNDCG: ", ndcg)return ndcg

matlab实现:

function [ndcg] = NDCG(label_prob, label_target)
[sortArray,temp] = sort(-label_prob);  % 按照预测值进行排序
allLabelSort = label_target(temp); % 根据排序后的预测值获取对应的标签值
sortedTargetVector = sort(label_target);  % 对标签值进行排序(这里是升序排列)
sortedTargetVector = fliplr(sortedTargetVector);%对排序后的标签值进行翻转,使之呈降序排列dcg = 0;
for i = 1: numel(temp)rel = allLabelSort(i);denominator = log2(i + 1);dcg = dcg + (rel / denominator);
endidcg = 0;    %最理想的DCG状态就是按照目标值的进行排列
for i = 1: numel(temp) rel = sortedTargetVector(i);denominator = log2(i + 1);idcg = idcg + (rel / denominator);
end ndcg = max(dcg / idcg);
end

以后直接上这儿粘贴了,为偷懒打下基础。

输出数值类型的算法评价指标相关推荐

  1. C#进程调度的模拟实现:模拟先来先服务调度算法、短作业优先调度算法和优先级调度算法(考虑非抢占式和抢占式),进行算法评价,输出调度结果和算法评价指标。

    没什么水平,希望能帮到你 环境:visual studio 2019 附带工程资源:C#进程调度的模拟实现附带资源-C#文档类资源-CSDN下载 先来先服务的调度算法:是一种非抢占式的算法,先来先服务 ...

  2. Python入门基础-六、案例3 基础代谢率(BMR)计算器 #Python中常用的数值类型#字符串分割与格式化输出#异常处理机制

    (课程相关的所有资料代码,已上传至CSDN,请自行下载 https://download.csdn.net/download/qq_34243930/10764180 ) BMR 计算器 1.0 Py ...

  3. 变量、基本数值类型、格式化输出、运算符

    前言:这周迷上了打羽毛球,正所谓"人菜瘾大",一下班,就喊上狂徒张三去楼下空地打球,一直打到天黑为止.打完球浑身大汗淋漓,又是洗澡洗衣,额外占用了点学习的时间,但人很快乐,感觉自己 ...

  4. python bool类型_Python 的内置数值类型

    Python 是一种敏捷的.动态类型化的.极富表现力的开源编程语言,可以被自由地安装到多种平台上.Python 代码是被解释的.如果您对编辑.构建和执行循环较为熟悉,则 Python 代码对您来说更简 ...

  5. python语言整数类型-Python 的内置数值类型

    Python 是一种敏捷的.动态类型化的.极富表现力的开源编程语言,可以被自由地安装到多种平台上.Python 代码是被解释的.如果您对编辑.构建和执行循环较为熟悉,则 Python 代码对您来说更简 ...

  6. mysql可以存储整数数值的是_MySQL知识树 数值类型 整数

    数值类型 MySQL的数值类型包括整数类型.浮点数类型.定点数类型.位类型. 整数类型 MySQL支持的整数类型有tinyint.smallint.mediumint.int.bigint(范围从小到 ...

  7. MySQL列类型之——数值类型

    如想进一步了解有关mysql的信息请点击http://dev.mysql.com/doc/refman/5.1/zh/column-types.html MySQL支持多种列类型:数值类型.日期/时间 ...

  8. python数值类型教程_Python数值类型 int、float、complex 详解

    Python数值类型 int.float.complex 详解 Python数值类型:int.float.complex 在Python程序中,int.float和complex是三种十分重要的数值类 ...

  9. 聚类算法评价指标学习笔记

    聚类算法评价指标学习笔记 本文列举常用聚类性能度量指标,并列出相应代码与参考资料 聚类性能度量大致分两类,一类将聚类结果与某个"参考模型"(reference model)进行比较 ...

  10. python提供了两种基本的数值类型_Python 基本数据类型

    Python 基本数据类型 Python 中的变量不需要声明.每个变量在使用前都必须赋值,变量赋值以后该变量才会被创建. 在 Python 中,变量就是变量,它没有类型,我们所说的"类型&q ...

最新文章

  1. 永远不要对 AI 说:“我不行!”
  2. 一个NPOI导出到excel文件的范例记录
  3. Python之路【第七篇】:初识Socket
  4. 计算机网络工程本科培养计划,网络工程专业卓越计划本科培养方案2015版-西安电子科技大学计算机.doc...
  5. ubuntu下git更改默认编辑器
  6. sscanf函数和正则表达式
  7. c++系统给baijq分配了空间
  8. 哔哩哔哩2021年Q4及全年财报:全年营收194亿元,同比增长62%
  9. 合成未来宝宝照片_当英国皇室宝宝长大:阿奇王子变卷毛星人,夏洛特公主颜值最能抗...
  10. Android AsyncTask异步线程
  11. 一套OA系统 破解中小企业4大管理难题
  12. android最优化启动画面,Android启动页黑屏及最优解决方案
  13. 信息周刊:随意设置电脑密码存在安全隐患
  14. leetcode36.有效的数独(中等)
  15. JNi调用过程(转载)
  16. 西门子触摸屏脚本程序_新手请收藏,超详细的西门子触摸屏设置与编程图文教程...
  17. 关于MSTP的个人总结,如何查看华为生成树状态信息
  18. 2020年,抖音赚钱全攻略,做抖音,看这就够了!
  19. LC-恢复二叉搜索树(JavaScript实现)
  20. 为何我工作十年,内心仍无比恐慌(腾讯产品总监曹菲)

热门文章

  1. 关于错误local variable ‘str‘ referenced before assignment
  2. 解决Vmware虚拟机startx进入图形界面卡退、白屏、黑屏的问题
  3. AI读书笔记:《智能简史(谁会替代人类成为主导物种)》
  4. r语言员工离职_基于随机森林的优秀员工离职因素实证分析及预测
  5. 阿里达摩院——算法面经
  6. python用语句输入一个3*3的二维矩阵、并将之输出_从键盘输入一个3行4列的矩阵,将其转置后,变成4行3列的矩阵输出。这个c语言的代码咋写...
  7. 【MySQL 8】MySQL 5.7都即将停只维护了,是时候学习一波MySQL 8了
  8. SysAnti.exe和autorun.inf病毒的查杀
  9. 《关键对话》如何高效能沟通之何谓关键对话
  10. 不同的count用法