1
2
3
4
5
6
7
8
9
10 from nltk_lite.contrib.classifier.exceptions import systemerror as se
13 self.index, self.matrix = {}, []
14 self.__num_class_vals = len(klass)
15 for i in range(self.__num_class_vals):
16 self.index[klass[i]] = i
17 self.matrix.append([0] * self.__num_class_vals)
18
19 - def count(self, actual, predicted):
20 self.matrix[self.index[actual]][self.index[predicted]] += 1
21
23 return self.__div(self.tp() + self.tn(), self.tp() + self.fp() + self.fn() + self.tn())
24
27
28 - def tpr(self, index = 0):
30 sensitivity = tpr
31
32 - def tnr(self, index = 0):
34 specificity = tnr
35
36 - def fpr(self, index = 0):
38
41
44
45 - def fscore(self, beta = 1, index = 0):
48
49 - def __div(self, num, den):
50 if num == 0: return 0;
51 if den == 0: raise se.SystemError('Divide by Zero Error')
52 return float(num)/ den
53
54 - def tp(self, index = 0):
56
57 - def tn(self, index = 0):
58 sum = 0
59 for i in range(self.__num_class_vals):
60 if i == index: continue
61 for j in range(self.__num_class_vals):
62 if j == index: continue
63 sum += self.matrix[i][j]
64 return sum
65
66 - def fp(self, index = 0):
67 sum = 0
68 for i in range(self.__num_class_vals):
69 if i == index: continue
70 sum += self.matrix[i][index]
71 return sum
72
73 - def fn(self, index = 0):
74 sum = 0
75 for i in range(self.__num_class_vals):
76 if i == index: continue
77 sum += self.matrix[index][i]
78 return sum
79