-
Notifications
You must be signed in to change notification settings - Fork 0
/
metrics.py
151 lines (124 loc) · 5.38 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
import math
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.metrics import classification_report, confusion_matrix, roc_curve, auc, f1_score, cohen_kappa_score
from sklearn.metrics import average_precision_score, precision_recall_curve, roc_auc_score, accuracy_score
def TP(y, pred, th=0.5):
pred_t = (pred > th)
return np.sum((pred == True) & (y == 1))
def TN(y, pred, th=0.5):
pred_t = (pred > th)
return np.sum((pred == False) & (y == 0))
def FN(y, pred, th=0.5):
pred_t = (pred > th)
return np.sum((pred == False) & (y == 1))
def FP(y, pred, th=0.5):
pred_t = (pred > th)
return np.sum((pred == True) & (y == 0))
def get_accuracy(y, pred, th=0.5):
tp = TP(y,pred,th)
fp = FP(y,pred,th)
tn = TN(y,pred,th)
fn = FN(y,pred,th)
return (tp+tn)/(tp+fp+tn+fn)
def get_prevalence(y):
return np.sum(y)/y.shape[0]
def sensitivity(y, pred, th=0.5):
tp = TP(y,pred,th)
fn = FN(y,pred,th)
return tp/(tp+fn)
def specificity(y, pred, th=0.5):
tn = TN(y,pred,th)
fp = FP(y,pred,th)
return tn/(tn+fp)
def get_ppv(y, pred, th=0.5):
tp = TP(y,pred,th)
fp = FP(y,pred,th)
return tp/(tp+fp)
def get_npv(y, pred, th=0.5):
tn = TN(y,pred,th)
fn = FN(y,pred,th)
return tn/(tn+fn)
def get_far(y, pred, th=0.5):
return 1 - specificity(y, pred, th)
def get_frr(y, pred, th=0.5):
return 1 - sensitivity(y, pred, th)
def get_aer(y, pred, th=0.5):
return 1 - get_accuracy(y, pred, th)
def get_cohen_kappa_score(y, pred, th=0.5):
y1 = y
y2 = (pred >= th)
return cohen_kappa_score(y1, y2)
def get_performance_metrics(y, pred, class_labels, tp=TP,
tn=TN, fp=FP,
fn=FN,
acc=get_accuracy, prevalence=get_prevalence,
spec=specificity,sens=sensitivity, ppv=get_ppv,
npv=get_npv, auc=roc_auc_score, f1=f1_score,
kappa=get_cohen_kappa_score, thresholds=[]):
if len(thresholds) != len(class_labels):
thresholds = [.5] * len(class_labels)
columns = ["Injury", "TP", "TN", "FP", "FN", "Accuracy", "Prevalence",
"Sensitivity", "Specificity", "PPV", "NPV"]#, "AUC", "F1",
#"Kappa", "Threshold"]
df = pd.DataFrame(columns=columns)
for i in range(len(class_labels)):
df.loc[i] = [class_labels[i],
round(tp(y[:, i], pred[:, i]),3),
round(tn(y[:, i], pred[:, i]),3),
round(fp(y[:, i], pred[:, i]),3),
round(fn(y[:, i], pred[:, i]),3),
round(acc(y[:, i], pred[:, i], thresholds[i]),3),
round(prevalence(y[:, i]),3),
round(sens(y[:, i], pred[:, i], thresholds[i]),3),
round(spec(y[:, i], pred[:, i], thresholds[i]),3),
round(ppv(y[:, i], pred[:, i], thresholds[i]),3),
round(npv(y[:, i], pred[:, i], thresholds[i]),3)]
# round(auc(y[:, i], pred[:, i]),3),
# round(f1(y[:, i], pred[:, i] > thresholds[i]),3),
# round(kappa(y[:,i], pred[:,i], thresholds[i]),3),
# round(thresholds[i], 3)]
df = df.set_index("Injury")
return df
def bootstrap_metric(y, pred, classes, metric='auc',bootstraps = 100, fold_size = 1000):
statistics = np.zeros((len(classes), bootstraps))
if metric=='AUC':
metric_func = roc_auc_score
if metric=='Sensitivity':
metric_func = sensitivity
if metric=='Specificity':
metric_func = specificity
if metric=='Accuracy':
metric_func = get_accuracy
if metric=='Kappa':
metric_func = get_cohen_kappa_score
for c in range(len(classes)):
df = pd.DataFrame(columns=['y', 'pred'])
df.loc[:, 'y'] = y[:, c]
df.loc[:, 'pred'] = pred[:, c]
# get positive examples for stratified sampling
df_pos = df[df.y == 1]
df_neg = df[df.y == 0]
prevalence = len(df_pos) / len(df)
for i in range(bootstraps):
# stratified sampling of positive and negative examples
pos_sample = df_pos.sample(n = int(fold_size * prevalence), replace=True)
neg_sample = df_neg.sample(n = int(fold_size * (1-prevalence)), replace=True)
y_sample = np.concatenate([pos_sample.y.values, neg_sample.y.values])
pred_sample = np.concatenate([pos_sample.pred.values, neg_sample.pred.values])
score = metric_func(y_sample, pred_sample)
statistics[c][i] = score
return statistics
def get_confidence_intervals(y,pred,class_labels):
metric_dfs = {}
for metric in ['AUC','Sensitivity','Specificity','Accuracy']:
statistics = bootstrap_metric(y,pred,class_labels,metric)
df = pd.DataFrame(columns=["Mean "+metric+" (CI 5%-95%)"])
for i in range(len(class_labels)):
mean = statistics.mean(axis=1)[i]
max_ = np.quantile(statistics, .95, axis=1)[i]
min_ = np.quantile(statistics, .05, axis=1)[i]
df.loc[class_labels[i]] = ["%.2f (%.2f-%.2f)" % (mean, min_, max_)]
metric_dfs[metric] = df
return metric_dfs