Last active
January 8, 2022 20:01
-
-
Save XinyueZ/0c1c93d98c7bd6a7ab5ad4952aa60111 to your computer and use it in GitHub Desktop.
F_Score
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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