-
Notifications
You must be signed in to change notification settings - Fork 1
/
metrics.py
46 lines (38 loc) · 1.5 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
import numpy as np
from sklearn import metrics
# for decompensation, in-hospital mortality
def print_metrics_binary(y_true, predictions, verbose=1):
predictions = np.array(predictions)
if (len(predictions.shape) == 1):
predictions = np.stack([1 - predictions, predictions]).transpose((1, 0))
cf = metrics.confusion_matrix(y_true, predictions.argmax(axis=1))
if verbose:
print("confusion matrix:")
print(cf)
cf = cf.astype(np.float32)
acc = (cf[0][0] + cf[1][1]) / np.sum(cf)
prec0 = cf[0][0] / (cf[0][0] + cf[1][0])
prec1 = cf[1][1] / (cf[1][1] + cf[0][1])
rec0 = cf[0][0] / (cf[0][0] + cf[0][1])
rec1 = cf[1][1] / (cf[1][1] + cf[1][0])
auroc = metrics.roc_auc_score(y_true, predictions[:, 1])
(precisions, recalls, thresholds) = metrics.precision_recall_curve(y_true, predictions[:, 1])
auprc = metrics.auc(recalls, precisions)
minpse = np.max([min(x, y) for (x, y) in zip(precisions, recalls)])
if verbose:
print("accuracy =", acc)
print("precision class 0 =", prec0)
print("precision class 1 =", prec1)
print("recall class 0 =", rec0)
print("recall calss 1 =", rec1)
print("AUC of ROC =", auroc)
print("AUC of PRC =", auprc)
print("min(+P, Se) =", minpse)
return {"acc": acc,
"prec0": prec0,
"prec1": prec1,
"rec0": rec0,
"rec1": rec1,
"auroc": auroc,
"auprc": auprc,
"minpse": minpse}