Skip to content

Instantly share code, notes, and snippets.

@sunilkumardash9
Last active January 30, 2022 11:47
Show Gist options
  • Save sunilkumardash9/1630689b4ae1c33a20a29f409631b592 to your computer and use it in GitHub Desktop.
Save sunilkumardash9/1630689b4ae1c33a20a29f409631b592 to your computer and use it in GitHub Desktop.
def F1_score(y,y_hat):
tp,tn,fp,fn = 0,0,0,0
for i in range(len(y)):
if y[i] == 1 and y_hat[i] == 1:
tp += 1
elif y[i] == 1 and y_hat[i] == 0:
fn += 1
elif y[i] == 0 and y_hat[i] == 1:
fp += 1
elif y[i] == 0 and y_hat[i] == 0:
tn += 1
precision = tp/(tp+fp)
recall = tp/(tp+fn)
f1_score = 2*precision*recall/(precision+recall)
return f1_score
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment