项目中需要判断用户提交的多选题选项的正确率,比如正确答案应该为a, b, c,而用户选择的是a, d,那么如何判断他的正确率呢,这个场景就需要用到F1 score来计算。
From Wikipedia, the free encyclopedia http://en.wikipedia.org/wiki/F1_score
In statistical analysis of Binary classification, the F1 score (also F-score or F-measure) is a measure of a test’s accuracy.
It considers both the precision p and the recall r of the test to compute the score:
p is the number of correct results divided by the number of all returned results and r is the number of correct results divided by the number of results that should have been returned.
The F1 score can be interpreted as a weighted average of the precision and recall, where an F1 score reaches its best value at 1 and worst score at 0.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18#!/usr/bin/env python
#-*- coding:utf-8 -*-
def get_f1(standard_answer, user_answer):
s_user_answer = set(user_answer)
s_standard_answer = set(standard_answer)
correct_results_len = len(s_user_answer & s_standard_answer)
precision = (correct_results_len + 1e-8) / (len(user_answer) + 1e-8)
recall = (correct_results_len + 1e-8) / (len(standard_answer) + 1e-8)
f1 = 2 * precision * recall / (precision + recall)
return f1
if __name__ == ‘__main__’:
standard = [‘a’, ‘c’, ‘d’]
user = [‘a’]
print get_f1(standard, user)
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/181883.html原文链接:https://javaforall.cn