阿小信大人的头像
Talk is cheap. Show me the code. Linus Torvalds

F1 score2014-09-03 03:22

项目中需要判断用户提交的多选题选项的正确率,比如正确答案应该为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)

如果您觉得从我的分享中得到了帮助,并且希望我的博客持续发展下去,请点击支付宝捐赠,谢谢!

若非特别声明,文章均为阿小信的个人笔记,转载请注明出处。文章如有侵权内容,请联系我,我会及时删除。

#Python#   阅读[3035] 评论[0]

你可能也感兴趣的文章推荐

本文最近访客

网友207.*.*.124[美国]2021-09-22 04:39
网友157.*.*.115[Redmond]2021-09-22 04:19
网友40.*.*.3[美国]2021-09-22 04:19
网友40.*.*.82[美国]2021-09-22 04:18

发表评论