/
ABCD.py
47 lines (41 loc) · 1.33 KB
/
ABCD.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
39
40
41
42
43
44
45
46
47
from __future__ import division
import math
class counter():
def __init__(self, before, after, indx):
self.indx = indx
self.actual = before
self.predicted = after
self.TP, self.TN, self.FP, self.FN = 0, 0, 0, 0
for a, b in zip(self.actual, self.predicted):
if a == indx and b == indx:
self.TP += 1
elif a == b and a != indx:
self.TN += 1
elif a != indx and b == indx:
self.FP += 1
elif a == indx and b != indx:
self.FN += 1
elif a != indx and b != indx:
pass
def stats(self):
try:
Sen = self.TP / (self.TP + self.FN)
Spec = self.TN / (self.TN + self.FP)
Prec = self.TP / (self.TP + self.FP)
Acc = (self.TP + self.TN) / (self.TP + self.FN + self.TN + self.FP)
F = 2 * (Prec*Sen) / (Prec+Sen)
F1 = 2 * self.TP / (2 * self.TP + self.FP + self.FN)
G = 2 * Sen * Spec / (Sen + Spec)
G1 = Sen * Spec / (Sen + Spec)
return Sen, Prec, Spec, Acc, F, G
except ZeroDivisionError:
return 0, 0, 0, 0, 0, 0, 0
class ABCD():
"Statistics Stuff, confusion matrix, all that jazz..."
def __init__(self, before, after):
self.actual = before
self.predicted = after
def __call__(self):
uniques = set(self.actual)
for u in list(uniques):
yield counter(self.actual, self.predicted, indx=u)