Skip to content

Instantly share code, notes, and snippets.

@XinyueZ
Last active January 8, 2022 20:01
Show Gist options
  • Select an option

  • Save XinyueZ/0c1c93d98c7bd6a7ab5ad4952aa60111 to your computer and use it in GitHub Desktop.

Select an option

Save XinyueZ/0c1c93d98c7bd6a7ab5ad4952aa60111 to your computer and use it in GitHub Desktop.
F_Score
# An approach to do F_Score without np, however, using np is quite effective.
def F_Score(pred_list, true_list):
def bitwise(a, b, core):
return [int(core(pred, true)) for pred, true in zip(pred_list, true_list)]
true_positive = bitwise(pred_list, true_list, lambda pred, true: pred==1 and true==1)
true_positive = sum(true_positive)
false_positive = bitwise(pred_list, true_list, lambda pred, true: pred==1 and true==0)
false_positive = sum(false_positive)
false_negative = bitwise(pred_list, true_list, lambda pred, true: pred==0 and true==1)
false_negative = sum(false_negative)
score = 0
if (true_positive+false_positive) * (true_positive+false_negative) > 0:
precision = true_positive/(true_positive+false_positive);
recall = true_positive/(true_positive+false_negative);
score = (2*precision*recall)/(precision+recall);
return score
pred_list = [1, 1, 0, 0, 0, 1, 0] # prediction
true_list = [1, 0, 1, 0, 1, 1, 1] # true
print(F_Score(pred_list,
true_list))
pred_list = [0, 0, 0, 0, 0, 0, 0] # prediction
true_list = [0, 0, 0, 0, 0, 0, 0] # true
print(F_Score(pred_list,
true_list))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment