-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevaluate.py
More file actions
executable file
·287 lines (253 loc) · 6.59 KB
/
evaluate.py
File metadata and controls
executable file
·287 lines (253 loc) · 6.59 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
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
import numpy as np
import pandas as pd
class Evaluator:
def __init__(self, task="regression"):
self.logs = dict()
if task == "regression":
self.metrics = dict({
# linearity metrics
"R2": r2,
"r": pearsonr,
"r2": pearsonr2,
"CCC": ccc,
# error-based metrics
"MAE": mae,
"RMSE": rmse,
"RMSPE": rmspe,
"RSR": rsr
})
elif task == "classification":
self.metrics = dict({
"accuracy": accuracy,
# machine learning metrics
"precision": precision,
"recall": recall,
# medical diagnostics metrics
"sensitivity": sensitivity,
"specificity": specificity,
# self-explanatory metrics
"TPR": TPR,
"FNR": FNR,
"FPR": FPR,
"TNR": TNR,
# composite metrics
"f1": f1,
"f2": f2,
"f05": f05,
"mcc": mcc
})
for m in self.metrics:
self.logs[m] = []
def log(self, y, y_hat, conf=None):
y = np.array(y)
if conf is not None:
y_hat = (np.array(y_hat) > conf).astype(int)
else:
y_hat = np.array(y_hat)
for k, func in self.metrics.items():
metric = func(y, y_hat)
self.logs[k].append(metric)
def to_dataframe(self):
"""
return
------
iteration | {metrics}
"""
df = pd.DataFrame(self.logs).reset_index()
df.columns = ["iteration"] + df.columns[1:].tolist()
return df
def to_tidy(self):
"""
return
------
iteration | metric | value | (estimator)
"""
df = self.to_dataframe()
df = df.melt(id_vars="iteration", var_name="metric")
return df
def summary(self, estimator=None, func=["mean", "var"]):
"""
return
------
metric | {func} | (estimator)
"""
df = self.to_tidy().iloc[:, 1:].\
groupby("metric").agg(func)
df = df.reset_index()
df.columns = ["metric"] + func
if estimator:
df["estimator"] = estimator
return df
def is_single_input(x):
"""
Check if input is a single value or an array with only one element
"""
is_single = isinstance(x, (int, float))
is_array = isinstance(x, np.ndarray) and len(x) == 1
return is_single or is_array
# Regression metrics ---------------------------------------------------------
def r2(y, yhat):
"""
R-squared
"""
if is_single_input(y):
return np.nan
ss_residual = np.sum((y - yhat) ** 2)
ss_total = np.sum((y - np.mean(y)) ** 2)
R2 = 1 - (ss_residual / ss_total)
return R2
def pearsonr(y, yhat):
"""
correlation coefficient
"""
if is_single_input(y):
return np.nan
cov = np.cov(y, yhat)[0, 1] # get the covariance (off-diagonal element)
std_y = np.std(y)
std_yhat = np.std(yhat)
return cov / (std_y * std_yhat)
def pearsonr2(y, yhat):
"""
squared correlation coefficient
"""
if is_single_input(y):
return np.nan
return pearsonr(y, yhat) ** 2
def ccc(y, yhat):
"""
Lins' concordance correlation coefficient
"""
if is_single_input(y):
return np.nan
r = pearsonr(y, yhat)
mean_y = np.mean(y)
mean_yhat = np.mean(yhat)
var_y = np.var(y)
var_yhat = np.var(yhat)
sd_y = np.std(y)
sd_yhat = np.std(yhat)
num = 2 * r * sd_y * sd_yhat
den = var_y + var_yhat + (mean_y - mean_yhat) ** 2
return num / den
def mae(y, yhat):
"""
mean absolute error
"""
return np.mean(np.abs(y - yhat))
def rmse(y, yhat):
"""
root mean squared error
"""
return np.sqrt(np.mean((y - yhat) ** 2))
def rmspe(y, yhat):
"""
root mean squared percentage error
"""
return np.sqrt(np.mean(((y - yhat) / y) ** 2))
def rsr(y, yhat):
"""
RMSE standard deviation ratio
"""
if is_single_input(y):
return np.nan
return rmse(y, yhat) / np.std(y)
def mae_var(y, yhat):
"""
MAE variance
"""
return np.var(np.abs(y - yhat))
def rmse_var(y, yhat):
"""
RMSE variance
"""
return np.var(np.sqrt((y - yhat) ** 2))
def rmspe_var(y, yhat):
"""
RMSPE variance
"""
return np.var(np.sqrt(((y - yhat) / y) ** 2))
# Classification metrics -----------------------------------------------------
def accuracy(y, yhat):
"""
accuracy
"""
return np.mean(y == yhat)
# machine learning metrics
def precision(y, yhat):
"""
precision
"""
tp = np.sum((y == 1) & (yhat == 1))
fp = np.sum((y == 0) & (yhat == 1))
return tp / (tp + fp)
def recall(y, yhat):
"""
recall = sensitivity = true positive rate
"""
tp = np.sum((y == 1) & (yhat == 1))
fn = np.sum((y == 1) & (yhat == 0))
return tp / (tp + fn)
# medical diagnostics metrics
def sensitivity(y, yhat):
"""
sensitivity = recall = true positive rate
"""
return recall(y, yhat)
def specificity(y, yhat):
"""
specificity = true negative rate
"""
tn = np.sum((y == 0) & (yhat == 0))
fp = np.sum((y == 0) & (yhat == 1))
return tn / (tn + fp)
# self-explanatory metrics
def TPR(y, yhat):
"""
true positive rate
"""
return sensitivity(y, yhat)
def FNR(y, yhat):
"""
false negative rate
"""
return 1 - sensitivity(y, yhat)
def FPR(y, yhat):
"""
false positive rate
"""
return 1 - specificity(y, yhat)
def TNR(y, yhat):
"""
true negative rate
"""
return specificity(y, yhat)
# composite metrics
def f1(y, yhat):
"""
f1 score
"""
p = precision(y, yhat)
r = recall(y, yhat)
return 2 * (p * r) / (p + r)
def f2(y, yhat):
return fbeta(y, yhat, beta=2)
def f05(y, yhat):
return fbeta(y, yhat, beta=0.5)
def fbeta(y, yhat, beta=2):
"""
f-beta score
"""
p = precision(y, yhat)
r = recall(y, yhat)
return (1 + beta ** 2) * (p * r) / ((beta ** 2 * p) + r)
def mcc(y, yhat):
"""
Matthews correlation coefficient
"""
tp = np.sum((y == 1) & (yhat == 1))
tn = np.sum((y == 0) & (yhat == 0))
fp = np.sum((y == 0) & (yhat == 1))
fn = np.sum((y == 1) & (yhat == 0))
num = (tp * tn) - (fp * fn)
den = np.sqrt((tp + fp) * (tp + fn) * (tn + fp) * (tn + fn))
return num / den