-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevaluation.py
More file actions
67 lines (59 loc) · 2.51 KB
/
evaluation.py
File metadata and controls
67 lines (59 loc) · 2.51 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
"""
Performance evaluation
From the DBG github;
https://github.com/fpour/DGB/blob/main/
"""
import math
import random
import pandas as pd
import numpy as np
np.seterr(divide='ignore', invalid='ignore')
from sklearn.metrics import *
def get_measures_for_threshold(y_true, y_pred_score, threshold):
"""
compute measures for a specific threshold
"""
perf_measures = {}
y_pred_label = y_pred_score > threshold
perf_measures['acc'] = accuracy_score(y_true, y_pred_label)
prec, rec, f1, num = precision_recall_fscore_support(y_true, y_pred_label, average='binary', zero_division=1)
perf_measures['prec'] = prec
perf_measures['rec'] = rec
perf_measures['f1'] = f1
return perf_measures
def extra_measures(y_true, y_pred_score):
"""
compute extra performance measures
"""
perf_dict = {}
# find optimal threshold of au-roc
perf_dict['ap'] = average_precision_score(y_true, y_pred_score)
perf_dict['au_roc_score'] = roc_auc_score(y_true, y_pred_score)
fpr, tpr, roc_thresholds = roc_curve(y_true, y_pred_score)
opt_idx = np.argmax(tpr - fpr)
opt_thr_auroc = roc_thresholds[opt_idx]
perf_dict['opt_thr_au_roc'] = opt_thr_auroc
auroc_perf_dict = get_measures_for_threshold(y_true, y_pred_score, opt_thr_auroc)
perf_dict['acc_auroc_opt_thr'] = auroc_perf_dict['acc']
perf_dict['prec_auroc_opt_thr'] = auroc_perf_dict['prec']
perf_dict['rec_auroc_opt_thr'] = auroc_perf_dict['rec']
perf_dict['f1_auroc_opt_thr'] = auroc_perf_dict['f1']
prec_pr_curve, rec_pr_curve, pr_thresholds = precision_recall_curve(y_true, y_pred_score)
perf_dict['au_pr_score'] = auc(rec_pr_curve, prec_pr_curve)
# convert to f score
fscore = (2 * prec_pr_curve * rec_pr_curve) / (prec_pr_curve + rec_pr_curve)
opt_idx = np.argmax(fscore)
opt_thr_aupr = pr_thresholds[opt_idx]
perf_dict['opt_thr_au_pr'] = opt_thr_aupr
aupr_perf_dict = get_measures_for_threshold(y_true, y_pred_score, opt_thr_aupr)
perf_dict['acc_aupr_opt_thr'] = aupr_perf_dict['acc']
perf_dict['prec_aupr_opt_thr'] = aupr_perf_dict['prec']
perf_dict['rec_aupr_opt_thr'] = aupr_perf_dict['rec']
perf_dict['f1_aupr_opt_thr'] = aupr_perf_dict['f1']
# threshold = 0.5
perf_half_dict = get_measures_for_threshold(y_true, y_pred_score, 0.5)
perf_dict['acc_thr_0.5'] = perf_half_dict['acc']
perf_dict['prec_thr_0.5'] = perf_half_dict['prec']
perf_dict['rec_thr_0.5'] = perf_half_dict['rec']
perf_dict['f1_thr_0.5'] = perf_half_dict['f1']
return perf_dict