Skip to content

Instantly share code, notes, and snippets.

@axiaoxin
Created March 17, 2014 07:06
Show Gist options
  • Save axiaoxin/9595121 to your computer and use it in GitHub Desktop.
Save axiaoxin/9595121 to your computer and use it in GitHub Desktop.
#-*- 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)
if correct_results_len > 0:
precision = float(correct_results_len) / len(user_answer)
recall = float(correct_results_len) / len(standard_answer)
f1 = 2 * precision * recall / (precision + recall)
return f1
else:
return 0
if __name__ == '__main__':
standard = ['a', 'c', 'd']
user = ['a']
print get_f1(standard, user)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment