Advertisement
jabihad

F1-Score.py

Jul 9th, 2020
1,367
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.06 KB | None | 0 0
  1. # Abu Bakar Siddique
  2. # jabihad@gmail.com
  3. # Calculating F1 Score
  4. import os
  5. person = {}
  6. with open("H:\\Assignment\\person") as f:
  7.     for line in f:
  8.        (key, val) = line.rstrip("\n").split(',')
  9.        if(val=="True"):
  10.            person[key] = 1
  11.        else:
  12.            person[key] = 0
  13.  
  14. pred  = {}
  15. path = 'H:\\Assignment\\pred\\'
  16.  
  17. # r=root, d=directories, f = files
  18. for r, d, f in os.walk(path):
  19.     for file in f:
  20.         name = os.path.join(r, file)
  21.         rd = open(name, 'r')
  22.         val = int(rd.read())
  23.         pred[str(file)] = val
  24.         rd.close()
  25.  
  26. True_Positive = 0
  27. False_Positive = 0
  28. False_Negative = 0
  29.  
  30. for k in pred.keys():
  31.     if(pred[k]==1 and person[k]==1):
  32.         True_Positive += 1
  33.     elif(pred[k]==1 and person[k]==0):
  34.         False_Positive += 1
  35.     elif(pred[k]==0 and person[k]==1):
  36.         False_Negative += 1
  37.  
  38.  
  39. Precision = True_Positive / (True_Positive + False_Positive)
  40. Recall = True_Positive / ( True_Positive + False_Negative)
  41. F1 = (2 * Precision * Recall) / (Precision + Recall)
  42.  
  43. print("F1 Score is: ", F1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement