-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathevaluator.py
174 lines (132 loc) · 5.86 KB
/
evaluator.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
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
class evaluator(object):
'''will calculate precision
, accuracy, recall
'''
def __init__(self,model = None):
#three classes
self.model = model
self.unique_classes = list(set(self.model.y_test))
self.c1_n = sum([1 for item in list(self.model.y_test) if item == self.unique_classes[0]])
self.c2_n = sum([1 for item in list(self.model.y_test) if item == self.unique_classes[1]])
self.c3_n = sum([1 for item in list(self.model.y_test) if item == self.unique_classes[2]])
def accuracy(self):
'''calculates TP+TN/(Total)
averaged across all three unique classes
'''
unique_classes = self.unique_classes
#class 1 accuracy
y_pred = list(self.model.y_pred)
y_pred = [0 if item != unique_classes[0] else 1 for item in y_pred]
y_test = list(self.model.y_test)
y_test = [0 if item != unique_classes[0] else 1 for item in y_pred]
n = len(y_test)
c1_accuracy = sum([y_pred[i]!=y_test[i] for i in range(n)])/float(n)
#class 2 accuracy
y_pred = list(self.model.y_pred)
y_pred = [0 if item != unique_classes[1] else 1 for item in y_pred]
y_test = list(self.model.y_test)
y_test = [0 if item != unique_classes[1] else 1 for item in y_pred]
n = len(y_test)
c2_accuracy = sum([y_pred[i]!=y_test[i] for i in range(n)])/float(n)
#class 3 accuracy
y_pred = list(self.model.y_pred)
y_pred = [0 if item != unique_classes[2] else 1 for item in y_pred]
y_test = list(self.model.y_test)
y_test = [0 if item != unique_classes[2] else 1 for item in y_pred]
n = len(y_test)
c3_accuracy = sum([y_pred[i]!=y_test[i] for i in range(n)])/float(n)
return (self.c1_n*c1_accuracy+self.c2_n*c2_accuracy+self.c3_n*c3_accuracy)/float(n)
def precision(self):
'''calculate TP/TP+FP
'''
unique_classes = self.unique_classes
#class 1 precision
y_pred = list(self.model.y_pred)
y_pred = [0 if item != unique_classes[0] else 1 for item in y_pred]
y_test = list(self.model.y_test)
y_test = [0 if item != unique_classes[0] else 1 for item in y_pred]
n = len(y_test)
tp = sum([y_pred[i] == 1 and y_test[i] == 1 for i in range(n)])
fp = sum([y_pred[i] == 1 and y_test[i] == 0 for i in range(n)])
if tp+fp ==0:
c1_p = 0
else:
c1_p = tp/float(tp+fp)
#class 2 precision
y_pred = list(self.model.y_pred)
y_pred = [0 if item != unique_classes[1] else 1 for item in y_pred]
y_test = list(self.model.y_test)
y_test = [0 if item != unique_classes[1] else 1 for item in y_pred]
n = len(y_test)
tp = sum([y_pred[i] == 1 and y_test[i] == 1 for i in range(n)])
fp = sum([y_pred[i] == 1 and y_test[i] == 0 for i in range(n)])
if tp+fp ==0:
c2_p = 0
else:
c2_p = tp/float(tp+fp)
#class 3 precision
y_pred = list(self.model.y_pred)
y_pred = [0 if item != unique_classes[2] else 1 for item in y_pred]
y_test = list(self.model.y_test)
y_test = [0 if item != unique_classes[2] else 1 for item in y_pred]
n = len(y_test)
tp = sum([y_pred[i] == 1 and y_test[i] == 1 for i in range(n)])
fp = sum([y_pred[i] == 1 and y_test[i] == 0 for i in range(n)])
if tp+fp ==0:
c3_p = 0
else:
c3_p = tp/float(tp+fp)
return (self.c1_n*c1_p+self.c2_n*c2_p+self.c3_n*c3_p)/float(n)
def recall(self):
'''calculates TP/TP+FN
'''
unique_classes = self.unique_classes
#class 1 precision
y_pred = list(self.model.y_pred)
y_pred = [0 if item != unique_classes[0] else 1 for item in y_pred]
y_test = list(self.model.y_test)
y_test = [0 if item != unique_classes[0] else 1 for item in y_pred]
n = len(y_test)
tp = sum([y_pred[i] == 1 and y_test[i] == 1 for i in range(n)])
fn = sum([y_pred[i] == 0 and y_test[i] == 1 for i in range(n)])
if tp+fn ==0:
c1_r = 0
else:
c1_r = tp/float(tp+fn)
#class 2 precision
y_pred = list(self.model.y_pred)
y_pred = [0 if item != unique_classes[1] else 1 for item in y_pred]
y_test = list(self.model.y_test)
y_test = [0 if item != unique_classes[1] else 1 for item in y_pred]
n = len(y_test)
tp = sum([y_pred[i] == 1 and y_test[i] == 1 for i in range(n)])
fn = sum([y_pred[i] == 0 and y_test[i] == 1 for i in range(n)])
if tp+fn ==0:
c2_r = 0
else:
c2_r = tp/float(tp+fn)
#class 3 precision
y_pred = list(self.model.y_pred)
y_pred = [0 if item != unique_classes[2] else 1 for item in y_pred]
y_test = list(self.model.y_test)
y_test = [0 if item != unique_classes[2] else 1 for item in y_pred]
n = len(y_test)
tp = sum([y_pred[i] == 1 and y_test[i] == 1 for i in range(n)])
fn = sum([y_pred[i] == 0 and y_test[i] == 1 for i in range(n)])
if tp+fn ==0:
c3_r = 0
else:
c3_r = tp/float(tp+fn)
return (self.c1_n*c1_r+self.c2_n*c2_r+self.c3_n*c3_r)/float(n)
def main():
#import statements
import pickle
model = None
with open('models/svm.pkl','rb') as f:
model = pickle.load(f)
eval = evaluator(model)
print (eval.precision)
print (eval.recall)
print (eval.accuracy)
if __name__ == '__main__':
main()