-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfmat.py
38 lines (30 loc) · 870 Bytes
/
confmat.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
class BinaryConfusionMatrix:
def __init__(self, pos_tag, neg_tag):
self.TP = 0
self.FP = 0
self.TN = 0
self.FN = 0
self.ptag = pos_tag
self.ntag = neg_tag
def as_dict(self):
return {
"tp": self.TP,
"fp": self.FP,
"tn": self.TN,
"fn": self.FN
}
def update(self, truth, pred):
if truth == pred:
if pred == self.ptag:
self.TP += 1
else:
self.TN += 1
else:
if pred == self.ptag:
self.FP += 1
else:
self.FN += 1
def compute_from_dicts(self, truth_dict, pred_dict):
for key in list(truth_dict.keys()):
if key in pred_dict:
self.update(truth_dict[key], pred_dict[key])