Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Abu Bakar Siddique
- # jabihad@gmail.com
- # Calculating F1 Score
- import os
- person = {}
- with open("H:\\Assignment\\person") as f:
- for line in f:
- (key, val) = line.rstrip("\n").split(',')
- if(val=="True"):
- person[key] = 1
- else:
- person[key] = 0
- pred = {}
- path = 'H:\\Assignment\\pred\\'
- # r=root, d=directories, f = files
- for r, d, f in os.walk(path):
- for file in f:
- name = os.path.join(r, file)
- rd = open(name, 'r')
- val = int(rd.read())
- pred[str(file)] = val
- rd.close()
- True_Positive = 0
- False_Positive = 0
- False_Negative = 0
- for k in pred.keys():
- if(pred[k]==1 and person[k]==1):
- True_Positive += 1
- elif(pred[k]==1 and person[k]==0):
- False_Positive += 1
- elif(pred[k]==0 and person[k]==1):
- False_Negative += 1
- Precision = True_Positive / (True_Positive + False_Positive)
- Recall = True_Positive / ( True_Positive + False_Negative)
- F1 = (2 * Precision * Recall) / (Precision + Recall)
- print("F1 Score is: ", F1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement