f1 score java_F1 score

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值