Package nltk_lite :: Package contrib :: Package classifier :: Module confusionmatrix
[hide private]
[frames] | no frames]

Source Code for Module nltk_lite.contrib.classifier.confusionmatrix

 1  # Natural Language Toolkit - Confusion Matrix 
 2  #  Updates itself with each classification result and constructs a matrix  
 3  #  Using the matrix it is capable of calculating several performance figures 
 4  # 
 5  # Author: Sumukh Ghodke <sumukh dot ghodke at gmail dot com> 
 6  # 
 7  # URL: <http://nltk.sf.net> 
 8  # This software is distributed under GPL, for license information see LICENSE.TXT 
 9   
10  from nltk_lite.contrib.classifier.exceptions import systemerror as se 
11 -class ConfusionMatrix:
12 - def __init__(self, klass):
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
22 - def accuracy(self):
23 return self.__div(self.tp() + self.tn(), self.tp() + self.fp() + self.fn() + self.tn())
24
25 - def error(self):
26 return 1 - self.accuracy()
27
28 - def tpr(self, index = 0):
29 return self.__div(self.tp(index), self.tp(index) + self.fn(index))
30 sensitivity = tpr 31
32 - def tnr(self, index = 0):
33 return self.__div(self.tn(index), self.fp(index) + self.tn(index))
34 specificity = tnr 35
36 - def fpr(self, index = 0):
37 return self.__div(self.fp(index), self.fp(index) + self.tn(index))
38
39 - def precision(self, index = 0):
40 return self.__div(self.tp(index), self.tp(index) + self.fp(index))
41
42 - def recall(self, index = 0):
43 return self.__div(self.tp(index), self.tp(index) + self.fn(index))
44
45 - def fscore(self, beta = 1, index = 0):
46 p, r = self.precision(index), self.recall(index) 47 return (1 + beta * beta) * self.__div(p * r, r + beta * beta * p)
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):
55 return self.matrix[index][index]
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