forked from sightmachine/SimpleCV
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfusionMatrix.py
More file actions
107 lines (93 loc) · 3.91 KB
/
Copy pathConfusionMatrix.py
File metadata and controls
107 lines (93 loc) · 3.91 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
from SimpleCV.base import *
from SimpleCV.ImageClass import Image
class ConfusionMatrix():
def __init__(self,classList):
self.classList = classList
self.classCount = len(classList)
self.confusionMatrix = np.zeros([self.classCount,self.classCount])
self.correctCount = 0
self.incorrectCount = 0
self.totalCount = 0
self.nameMap = {}
idx = 0
for obj in classList:
self.nameMap[obj] = idx
idx = idx + 1
def addDataPoint(self,truth_name,test_name):
self.confusionMatrix[self.nameMap[truth_name]][self.nameMap[test_name]] += 1
if( truth_name == test_name ):
self.correctCount += 1
else:
self.incorrectCount +=1
self.totalCount += 1
def getCorrectPercent(self):
if( self.totalCount > 0 and self.correctCount ):
return np.around(float(self.correctCount)/float(self.totalCount),4)
else:
return 0.00
def getIncorrectPercent(self):
if( self.totalCount > 0 and self.correctCount ):
return np.around(float(self.incorrectCount)/float(self.totalCount),4)
else:
return 0.00
def getClassCorrectPercent(self, className):
total = float(np.sum(self.confusionMatrix[:,self.nameMap[className]]))
correct = float(self.confusionMatrix[self.nameMap[className],self.nameMap[className]])
if( correct == 0 or total == 0 ):
return 0
else:
return np.around(correct/total,2)
def getClassIncorrectPercent(self, className):
total = float(np.sum(self.confusionMatrix[:,self.nameMap[className]]))
correct = float(self.confusionMatrix[self.nameMap[className],self.nameMap[className]])
incorrect = total-correct
if( incorrect == 0 or total == 0 ):
return 0
else:
return np.around(incorrect/total,2)
def getClassCorrect(self, className):
correct = self.confusionMatrix[self.nameMap[className],self.nameMap[className]]
return correct
def getClassIncorrect(self, className):
total = np.sum(self.confusionMatrix[:,self.nameMap[className]])
correct = self.confusionMatrix[self.nameMap[className],self.nameMap[className]]
incorrect = total-correct
return incorrect
def getClassCount(self,className):
return np.sum(self.confusionMatrix[:,self.nameMap[className]])
def getMisclassifiedCount(self,className):
# if we're class A, this returns the number of class B, C ...
# that were classified as A
count = np.sum(self.confusionMatrix[self.nameMap[className],:])
correct = self.confusionMatrix[[self.nameMap[className]],self.nameMap[className]]
total = count - correct
return int(total[0])
def toString(self,pad_sz=7):
retVal = 50*'#'
retVal += "\n"
retVal += "Total Data Points " + str(self.totalCount) + "\n"
retVal += "Correct Data Points " + str(self.correctCount) + "\n"
retVal += "Incorrect Data Points " + str(self.incorrectCount) + "\n"
retVal += "\n"
retVal += "Correct " + str(100.00*self.getCorrectPercent()) + "%\n"
retVal += "Incorrect " + str(100.00*self.getIncorrectPercent()) + "% \n"
retVal += 50*'#'
retVal += '\n'
wrdLen = 0
sz = pad_sz
for c in self.classList:
if( len(c) > wrdLen ):
wrdLen = len(c)
top=(wrdLen+1)* " "
for c in self.classList:
top = top + c[0:np.min([len(c),sz])].rjust(sz," ")+"|"
retVal += top+"\n"
for i in range(0,len(self.classList)):
line = self.classList[i].rjust(wrdLen," ")+"|"
nums = self.confusionMatrix[i]
for n in nums:
line += str(n).rjust(sz," ") + "|"
retVal += line+"\n"
retVal += 50*'#'
retVal += "\n"
return retVal