-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmetrics.py
484 lines (404 loc) · 23.9 KB
/
metrics.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import plotly.graph_objects as go
import math
from plotly.subplots import make_subplots
import seaborn as sns
class Metrics:
def __init__(self, test, prediction, threshold = 0.5):
self.test = test
self.prediction = prediction
self.tabla = np.hstack([test, prediction])
self.threshold = threshold
self.TP = 0; self.FN = 0; self.FP = 0; self.TN = 0;
for i in self.tabla:
if i[0] == 0 and i[1] <= self.threshold:
self.TN += 1
elif i[0] == 1 and i[1] >= self.threshold:
self.TP += 1
elif i[0] == 1 and i[1] <= self.threshold:
self.FN += 1
elif i[0] == 0 and i[1] >= self.threshold:
self.FP += 1
def confusion_matrix(self):
return np.array([[self.TP, self.FP], [self.FN, self.TN]])
def specificity(self):
try:
return np.round(self.TN/(self.TN+self.FP), decimals = 3)
except:
return None
def sensitivity(self):
try:
return np.round(self.TP/(self.TP+self.FN), decimals = 3)
except:
return None
def false_positive_rate(self):
try:
return np.round(self.FP/(self.FP+self.TN), decimals = 3)
except:
return None
def precision(self):
try:
return np.round(self.TP/(self.TP+self.FP), decimals = 3)
except:
return None
def PRC_Value(self, number_threshold = 100):
precision = []
recall = []
for i in np.linspace(1.0, 0.0, num=number_threshold):
aux = Metrics(self.test, self.prediction, threshold = i)
precision_value = aux.precision()
sensitivity_value = aux.sensitivity()
if precision_value != None: #Con un threshold de 1 no existen ni FP ni TP,
# entonces precision: ZeroDivisionError-> salta el except y devuelve None
precision.append(precision_value)
recall.append(sensitivity_value)
#precision, recall, _ = precision_recall_curve(self.test, self.prediction)
return precision, recall
def ROC_Value(self, number_threshold = 100):
recall = []
false_positive_rate = []
for i in np.linspace(1.0, 0.0, num=number_threshold):
aux = Metrics(self.test, self.prediction, threshold = i)
recall.append(aux.sensitivity())
false_positive_rate.append(aux.false_positive_rate())
return recall, false_positive_rate
def auc(self, trapeze=False): #Area-under-the-curve for ROC
recall, false_positive_rate = self.ROC_Value()
if trapeze is False:
return (sum([recall[i] * (false_positive_rate[i+1] - false_positive_rate[i]) for i in range(0, len(recall)-1)])).round(3)
else:
return (np.trapz(recall,false_positive_rate)).round(3)
#auc = roc_auc_score(self.test, self.prediction)
def ap(self, trapeze=False): #Area-under-the-curve for PRC
precision, recall = self.PRC_Value()
if trapeze is False:
return (sum([precision[i] * (recall[i+1] - recall[i]) for i in range(0, len(precision)-1)])).round(3)
else:
return (np.trapz(precision,recall)).round(3)
def youden_index(self):
return (self.sensitivity() + self.specificity() - 1).round(3) #the optimum cut-off point, height above the chance line
def f1(self):
try:
precision = self.precision()
sensitivity = self.sensitivity()
return (2 *( (precision*sensitivity) / (precision + sensitivity) )).round(3)
except:
return None
class Graphs:
def __init__(self, data, names=None):
self.data = data #Lista que contiene tuplas de la forma: [(Y_test, prediction)]
self.names = names #Lista que contiene los nombres de los modelos
def plot_ROC_plotly(self, double = False, fill = False, legend = True, threshold = False, methods=None, number_threshold = 100):
fig = go.Figure()
for i, (Y_test, prediction) in enumerate(self.data):
metrics = Metrics(Y_test, prediction)
recall, false_positive_rate = metrics.ROC_Value(number_threshold)
AUC = metrics.auc()
linspace = list(np.linspace(0.0, 1.0, num=100))
fig.add_trace(go.Scatter(x=linspace, y = linspace, mode = 'lines', showlegend = False, line=dict(width=0.5)))
fig.add_trace(go.Scatter(x=false_positive_rate, y=recall, mode='lines', name="ROC curve Model {} (AUC = {})".format(self.names[i], AUC)))
if threshold:
opt = Optimum(Y_test, prediction)
if "Youden" in methods:
threshold_youden, object_max_youden = opt.optimum_by_youden()
fig.add_trace(go.Scatter(x=[object_max_youden.false_positive_rate()], y=[object_max_youden.sensitivity()],
name= "Youden threshold {} Model {}".format(threshold_youden, self.names[i]),
mode = "markers",
line=dict(width=4, dash='dot')))
if "F-score" in methods:
threshold_f_score, object_f_score = opt.optimum_by_f_score()
fig.add_trace(go.Scatter(x=[object_f_score.false_positive_rate()], y=[object_f_score.sensitivity()],
name= "F-score threshold {} Model {}".format(threshold_f_score, self.names[i]),
mode = "markers",
line=dict(width=4, dash='dot')))
if "Distance ROC" in methods:
threshold_ROC, object_ROC = opt.optimum_for_ROC()
fig.add_trace(go.Scatter(x=[object_ROC.false_positive_rate()], y=[object_ROC.sensitivity()],
name= "Distance_ROC threshold {} Model {}".format(threshold_ROC, self.names[i]),
mode = "markers",
line=dict(width=4, dash='dot')))
if "Difference Sensitivity-Specificity" in methods:
threshold_difference_S_S, object_difference_S_S = opt.optimum_by_sensitivity_specificity_difference()
fig.add_trace(go.Scatter(x=[object_difference_S_S.false_positive_rate()], y=[object_difference_S_S.sensitivity()],
name= "Sensitivity_Specificity_Difference threshold {} Model {}".format(threshold_difference_S_S, self.names[i]),
mode = "markers",
line=dict(width=4, dash='dot')))
if fill:
fig.add_trace(go.Scatter(x=false_positive_rate, y=recall,
mode='lines',
name="ROC curve Model {} (AUC = {})".format(self.names[i], AUC),
fill = 'tozeroy'))
if double == False:
if fill:
dicc = dict(x=-.1, y=1.08 + 0.08 * (len(methods) if methods != None else 0) +
0.03 * (len(self.names) if len(self.names) > 1 else 0) )
else:
dicc = dict(x=-.1, y=1.08 + 0.06 * (len(methods) if methods != None else 0) +
0.03 * (len(self.names) if len(self.names) > 1 else 0) )
fig.update_layout(showlegend=legend, legend=dicc, autosize=False,
width=702, height=900, xaxis_title="false_positive_rate", yaxis_title="true_positive_rate")
return fig
def plot_PRC_plotly(self, double = False, fill = False, legend = True, threshold = False, methods=None, number_threshold = 100):
fig = go.Figure()
for i, (Y_test, prediction) in enumerate(self.data):
metrics = Metrics(Y_test, prediction)
precision, recall = metrics.PRC_Value(number_threshold)
AP = metrics.ap()
linspace = list(np.linspace(0.0, 1.0, num=100))
fig.add_trace(go.Scatter(x=recall, y=precision, mode='lines', name="Precision-recall-curve Model {} (AP = {})".format(self.names[i], AP)))
fig.add_trace(go.Scatter(x=linspace, y = linspace, mode = 'lines', showlegend = False, line=dict(width=0.0)))
if threshold:
opt = Optimum(Y_test, prediction)
if "Distance PRC" in methods:
threshold_PRC, object_PRC = opt.optimum_for_PRC()
fig.add_trace(go.Scatter(x=[object_PRC.sensitivity()], y=[object_PRC.precision()],
name="Distance_PRC threshold {} Model {}".format(threshold_PRC, self.names[i]),
mode = "markers",
line=dict(width=4, dash='dot')))
if "Difference Recall-Precision" in methods:
threshold_difference_R_P, object_difference_R_P = opt.optimum_by_recall_precision_difference()
fig.add_trace(go.Scatter(x=[object_difference_R_P.sensitivity()], y=[object_difference_R_P.precision()],
name="Difference_Recall_Precision threshold {} Model {}".format(threshold_difference_R_P, self.names[i]),
mode = "markers",
line=dict(width=4, dash='dot')))
if fill:
fig.add_trace(go.Scatter(x=recall, y=precision,
mode='lines',
name="Precision-recall-curve Model {} (AP = {})".format(self.names[i], AP),
fill = 'tozeroy'))
if double == False:
if fill:
dicc = dict(x=-.1, y=1.08 + 0.08 * (len(methods) if methods != None else 0) +
0.03 * (len(self.names) if len(self.names) > 1 else 0) )
else:
dicc = dict(x=-.1, y=1.08 + 0.06 * (len(methods) if methods != None else 0) +
0.03 * (len(self.names) if len(self.names) > 1 else 0) )
fig.update_layout(showlegend=legend, legend=dicc, autosize=False,
width=702, height=900, xaxis_title='Recall(sensitivity)', yaxis_title='Precision(PPV)')
return fig
def plot_all_plotly(self, fill = False, legend = True, threshold = False, methods=None, number_threshold = 100):
fig1 = self.plot_PRC_plotly(double = True, fill = fill, legend = legend, threshold = threshold, methods=methods, number_threshold = number_threshold)
fig2 = self.plot_ROC_plotly(double = True, fill = fill, legend = legend, threshold = threshold, methods=methods, number_threshold = number_threshold)
fig = make_subplots(rows=1, cols=2, subplot_titles=('PRC', 'ROC'))
for j in fig1.data:
fig.append_trace(j, 1, 1)
for i in fig2.data:
fig.append_trace(i, 1, 2)
if fill:
dicc = dict(x=-.1, y=1.25 + 0.30 * (len(methods) if methods != None else 0) +
0.03 * (len(self.names) if len(self.names) > 1 else 0) )
else:
dicc = dict(x=-.1, y=1.25 + 0.19 * (len(methods) if methods != None else 0) +
0.03 * (len(self.names) if len(self.names) > 1 else 0) )
fig.layout.update(showlegend=legend, legend=dicc, autosize=False, height=670, width=770)
fig.layout.xaxis1.update(title='Recall', showgrid=False)
fig.layout.yaxis1.update(title='Precision(PPV)', showgrid=False)
fig.layout.xaxis2.update(title='true_positive_rate', showgrid=False)
fig.layout.yaxis2.update(title='false_positive_rate', showgrid=False)
return fig
def plot_precision_recall_vs_threshold_plotly(self, legend = True, threshold = False, methods=None, number_threshold = 100):
fig = go.Figure()
for i, (Y_test, prediction) in enumerate(self.data):
metrics = Metrics(Y_test, prediction)
precisions, recalls = metrics.PRC_Value(number_threshold)
fig.add_trace(go.Scatter(x=np.linspace(1.0, 0.0, num=len(precisions)-1), y=precisions[:-1],
mode='lines',
name='Precision'))
fig.add_trace(go.Scatter(x=np.linspace(1.0, 0.0, num=len(precisions)-1), y=recalls[:-1],
mode='lines',
name='Recall'))
if threshold:
opt = Optimum(Y_test, prediction)
if "Distance PRC" in methods:
threshold_PRC, object_PRC = opt.optimum_for_PRC()
fig.add_trace(go.Scatter(x=[threshold_PRC], y=[object_PRC.precision()],
name="Distance_PRC threshold precision {} ".format(threshold_PRC),
mode = "markers",
line=dict(color='royalblue', width=4, dash='dot')))
fig.add_trace(go.Scatter(x=[threshold_PRC], y=[object_PRC.sensitivity()],
showlegend=False,
line=dict(color='royalblue', width=4, dash='dot')))
if "Difference Recall-Precision" in methods:
threshold_difference_R_P, object_difference_R_P = opt.optimum_by_recall_precision_difference()
fig.add_trace(go.Scatter(x=[threshold_difference_R_P], y=[object_difference_R_P.precision()],
name="Difference Recall-Precision threshold precision {} ".format(threshold_difference_R_P),
mode = "markers",
line=dict(color='firebrick', width=4, dash='dot')))
fig.add_trace(go.Scatter(x=[threshold_difference_R_P], y=[object_difference_R_P.sensitivity()],
showlegend=False,
line=dict(color='firebrick', width=4, dash='dot')))
# Edit the layout
dicc = dict(x=-.1, y=1.08 + 0.05 * (len(methods) if methods != None else 0))
fig.update_layout(showlegend=legend, legend=dicc, autosize=False, width=702, height=900, xaxis_title='Decision threshold',yaxis_title='Score')
return fig
def plot_tprate_fprate_vs_threshold_plotly(self, legend = True, threshold = False, methods=None, number_threshold = 100):
fig = go.Figure()
for i, (Y_test, prediction) in enumerate(self.data):
metrics = Metrics(Y_test, prediction)
recall, false_positive_rate = metrics.ROC_Value(number_threshold)
fig.add_trace(go.Scatter(x=np.linspace(1.0, 0.0, num=len(recall)-1), y=recall[:-1],
mode='lines',
name='Precision'))
fig.add_trace(go.Scatter(x=np.linspace(1.0, 0.0, num=len(recall)-1), y=false_positive_rate[:-1],
mode='lines',
name='Recall'))
if threshold:
opt = Optimum(Y_test, prediction)
if "Youden" in methods:
threshold_youden, object_max_youden = opt.optimum_by_youden()
fig.add_trace(go.Scatter(x=[threshold_youden], y=[object_max_youden.sensitivity()],
name= "Youden threshold {} Model {}".format(threshold_youden, self.names[i]),
mode = "markers",
line=dict(color='violet', width=4, dash='dot')))
fig.add_trace(go.Scatter(x=[threshold_youden], y=[object_max_youden.false_positive_rate()],
showlegend=False,
line=dict(color='violet', width=4, dash='dot')))
if "F-score" in methods:
threshold_f_score, object_f_score = opt.optimum_by_f_score()
fig.add_trace(go.Scatter(x=[threshold_f_score], y=[object_f_score.sensitivity()],
name= "F-score threshold {} Model {}".format(threshold_f_score, self.names[i]),
mode = "markers",
line=dict(color='yellowgreen', width=4, dash='dot')))
fig.add_trace(go.Scatter(x=[threshold_f_score], y=[object_f_score.false_positive_rate()],
showlegend=False,
line=dict(color='yellowgreen', width=4, dash='dot')))
if "Distance ROC" in methods:
threshold_ROC, object_ROC = opt.optimum_for_ROC()
fig.add_trace(go.Scatter(x=[threshold_ROC], y=[object_ROC.sensitivity()],
name= "Distance_ROC threshold {} Model {}".format(threshold_ROC, self.names[i]),
mode = "markers",
line=dict(color='goldenrod', width=4, dash='dot')))
fig.add_trace(go.Scatter(x=[threshold_ROC], y=[object_ROC.false_positive_rate()],
showlegend = False,
line=dict(color='goldenrod', width=4, dash='dot')))
if "Difference Sensitivity-Specificity" in methods:
threshold_difference_S_S, object_difference_S_S = opt.optimum_by_sensitivity_specificity_difference()
fig.add_trace(go.Scatter(x=[threshold_difference_S_S], y=[object_difference_S_S.sensitivity()],
name= "Sensitivity_Specificity_Difference threshold {} Model {}".format(threshold_difference_S_S, self.names[i]),
mode = "markers",
line=dict(color='turquoise', width=4, dash='dot')))
fig.add_trace(go.Scatter(x=[threshold_difference_S_S], y=[object_difference_S_S.false_positive_rate()],
showlegend = False,
line=dict(color='turquoise', width=4, dash='dot')))
# Edit the layout
dicc = dict(x=-.1, y=1.08 + 0.05 * (len(methods) if methods != None else 0))
fig.update_layout(showlegend=legend, legend=dicc, autosize=False, width=702, height=900, xaxis_title='Decision threshold',yaxis_title='Score')
return fig
class Optimum:
def __init__(self, test, prediction):
self.test = test
self.prediction = prediction
def optimum_by_youden(self):
object_max = None
threshold = None
youden = 0
for i in np.linspace(1.0, 0.0, num=100):
aux = Metrics(self.test, self.prediction, threshold = i)
youden_index = aux.youden_index()
if youden_index > youden:
youden = youden_index
object_max = aux
threshold = i
return threshold.round(2), object_max
def optimum_for_ROC(self):
object_min = None
threshold = None
distance = math.sqrt(2)
for i in np.linspace(1.0, 0.0, num=100):
aux = Metrics(self.test, self.prediction, threshold = i)
distance_aux = math.sqrt(
(aux.false_positive_rate())**2 + (aux.sensitivity() - 1)**2
)
if distance_aux < distance:
distance = distance_aux
object_min = aux
threshold = i
return threshold.round(2), object_min
def optimum_for_PRC(self):
object_min = None
threshold = None
distance = math.sqrt(2)
for i in np.linspace(1.0, 0.0, num=100):
aux = Metrics(self.test, self.prediction, threshold = i)
if aux.precision() is not None:
distance_aux = math.sqrt(
(aux.sensitivity() - 1)**2 + (aux.precision() - 1)**2
)
if distance_aux < distance:
distance = distance_aux
object_min = aux
threshold = i
return threshold.round(2), object_min
def optimum_by_f_score(self):
object_max = None
threshold = None
f_score = 0 # F1 score reaches its best value at 1 and worst at 0
for i in np.linspace(1.0, 0.0, num=100):
aux = Metrics(self.test, self.prediction, threshold = i)
f_score_aux = aux.f1()
if f_score_aux != None and f_score_aux > f_score:
f_score = f_score_aux
object_max = aux
threshold = i
return threshold.round(2), object_max
def optimum_by_sensitivity_specificity_difference(self):
object_min=None
threshold=None
dif_sensitivity_specificity=[]
for i in np.linspace(1.0,0.0,num=100):
aux=Metrics(self.test,self.prediction,threshold=i)
sensitivity_aux= aux.sensitivity()
specificity_aux= aux.specificity()
dif = abs(sensitivity_aux - specificity_aux)
dif_sensitivity_specificity.append(dif)
if dif == min(dif_sensitivity_specificity):
object_min= aux
threshold= i
return threshold.round(2),object_min
def optimum_by_recall_precision_difference(self):
object_min=None
threshold=None
dif_recall_precision=[]
for i in np.linspace(1.0,0.0,num=100):
aux=Metrics(self.test,self.prediction,threshold=i)
recall_aux= aux.sensitivity()
precision_aux= aux.precision()
if precision_aux!= None:
dif = abs(recall_aux - precision_aux)
dif_recall_precision.append(dif)
if dif == min(dif_recall_precision):
object_min= aux
threshold= i
return threshold.round(2),object_min
def report(self, colormap = True):
threshold_youden, object_youden = self.optimum_by_youden()
threshold_f_score, object_f_score = self.optimum_by_f_score()
threshold_ROC, object_ROC = self.optimum_for_ROC()
threshold_PRC, object_PRC = self.optimum_for_PRC()
threshold_difference_S_S, object_difference_S_S = self.optimum_by_sensitivity_specificity_difference()
threshold_difference_R_P, object_difference_R_P = self.optimum_by_recall_precision_difference()
df = pd.DataFrame({"Threshold":[threshold_youden, threshold_f_score, threshold_ROC, threshold_PRC, threshold_difference_S_S,threshold_difference_R_P],
"TP":[object_youden.TP, object_f_score.TP, object_ROC.TP, object_PRC.TP, object_difference_S_S.TP, object_difference_R_P.TP],
"TN":[object_youden.TN, object_f_score.TN, object_ROC.TN, object_PRC.TN, object_difference_S_S.TN, object_difference_R_P.TN],
"FP":[object_youden.FP, object_f_score.FP, object_ROC.FP, object_PRC.FP, object_difference_S_S.FP, object_difference_R_P.FP],
"FN":[object_youden.FN, object_f_score.FN, object_ROC.FN, object_PRC.FN, object_difference_S_S.FN, object_difference_R_P.FN],
"Specificity":[object_youden.specificity(), object_f_score.specificity(), object_ROC.specificity(),
object_PRC.specificity(),object_difference_S_S.specificity(), object_difference_R_P.specificity()],
"Sensitivity":[object_youden.sensitivity(), object_f_score.sensitivity(), object_ROC.sensitivity(),
object_PRC.sensitivity(),object_difference_S_S.sensitivity(), object_difference_R_P.sensitivity()],
"PPV":[object_youden.precision(), object_f_score.precision(), object_ROC.precision(),
object_PRC.precision(), object_difference_S_S.precision(), object_difference_R_P.precision()],
"AUC":[object_youden.auc() for i in range(6)],
"AP":[object_youden.ap() for i in range(6)]},
index = ["Youden", "F-score", "Distance_ROC", "Distance_PRC","Difference_Sensitivity_Specificity","Difference_Recall_Precision"])
if colormap:
cm = sns.light_palette("green", as_cmap=True)
s = df.sort_values(by='Threshold',ascending=False).style\
.background_gradient(cmap = cm, high = 0.5, low = -0.5, axis = 0)
#.set_properties(**{'width': '75px', 'text-align': 'center', 'font-size': '10pt'})
return s
return df