forked from chuchro3/Warfarin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.py
155 lines (138 loc) · 4.19 KB
/
util.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
import numpy as np
import matplotlib.pyplot as plt
import datetime
def bucket(dose):
"""
Returns the proper bucket given a dose
0 :- dose < 21
1 :- 21 <= dose <= 49
2 :- dose > 49
"""
if dose < 21:
return 0
if dose > 49:
return 2
return 1
def evaluate_performance(labels, true_labels):
acc = get_accuracy_bucketed(labels, true_labels)
prec = get_bucket_precision(labels, true_labels)
recall = get_bucket_recall(labels, true_labels)
f1 = get_f1_score(prec, recall)
dang = get_dangerous(labels, true_labels)
print("Accuracy: " + str(acc))
print("Precision: " + str(prec))
print("Recall: " + str(recall))
print("F1 Score: " + str(f1))
print("Dangerous dose ratio: " + str(dang))
return acc, prec, recall
def get_dangerous(labels, true_labels):
buckets = set()
acc_dic = {}
t_dic = {}
for i in range(len(labels)):
#l = bucket(labels[i])
l = labels[i]
#lt = bucket(true_labels[i])
lt = true_labels[i]
buckets.add(l)
buckets.add(lt)
if (l == 0 and lt == 2) or (l == 2 and lt == 0):
if lt in acc_dic:
acc_dic[lt] += 1
else:
acc_dic[lt] = 1
if lt in t_dic:
t_dic[lt] += 1
else:
t_dic[lt] = 1
acc = np.zeros(len(buckets))
print("dangerous dose count: " + str(acc_dic))
for k in acc_dic:
acc[k] = 1. * acc_dic[k] / t_dic[k]
return acc
def get_f1_score(precision, recall):
return 2. * (precision * recall) / (precision + recall)
def get_accuracy(labels, true_labels):
corr = 0
for i in range(len(labels)):
if bucket(labels[i]) == bucket(true_labels[i]):
corr += 1
return corr / len(labels)
def get_bucket_precision(labels, true_labels):
buckets = set()
acc_dic = {}
t_dic = {}
for i in range(len(labels)):
#l = bucket(labels[i])
l = labels[i]
#lt = bucket(true_labels[i])
lt = true_labels[i]
buckets.add(l)
buckets.add(lt)
if l == lt:
if l in acc_dic:
acc_dic[lt] += 1
else:
acc_dic[lt] = 1
if l in t_dic:
t_dic[l] += 1
else:
t_dic[l] = 1
acc = np.zeros(len(buckets))
print(acc_dic)
print(t_dic)
for k in acc_dic:
acc[k] = 1. * acc_dic[k] / t_dic[k]
return acc
def get_bucket_recall(labels, true_labels):
buckets = set()
acc_dic = {}
t_dic = {}
for i in range(len(labels)):
#l = bucket(labels[i])
l = labels[i]
#lt = bucket(true_labels[i])
lt = true_labels[i]
buckets.add(l)
buckets.add(lt)
if l == lt:
if l in acc_dic:
acc_dic[lt] += 1
else:
acc_dic[lt] = 1
if lt in t_dic:
t_dic[lt] += 1
else:
t_dic[lt] = 1
acc = np.zeros(len(buckets))
print(acc_dic)
print(t_dic)
for k in acc_dic:
acc[k] = 1. * acc_dic[k] / t_dic[k]
return acc
def get_accuracy_bucketed(labels, true_labels):
corr = 0
for i in range(len(labels)):
if labels[i] == true_labels[i]:
corr += 1
return corr / len(labels)
def plot_regret(regrets, alpha, suffix=''):
plt.clf()
plt.title('Regret - ' + str(alpha) + ' - ' + datetime.datetime.now().strftime("%D - %H:%M:%S"))
plt.xlabel('Samples Seen')
plt.ylabel('Regret')
plt.plot(range(1, 1+ len(regrets)), regrets)
if suffix == '':
plt.savefig('plots/regret'+str(alpha).replace('.','_')+ '_' + datetime.datetime.now().strftime('%s'))
else:
plt.savefig('plots/regret'+ '_' + suffix)
def plot_error_rate(error_rates, alpha, suffix=''):
plt.clf()
plt.title('Error Rate- ' + str(alpha) + ' - ' + datetime.datetime.now().strftime("%D - %H:%M:%S"))
plt.xlabel('Samples Seen')
plt.ylabel('Cumulative Error Rate')
plt.plot(range(1, 1+ len(error_rates)), error_rates)
if suffix == '':
plt.savefig('plots/error'+str(alpha).replace('.','_')+ '_' +datetime.datetime.now().strftime('%s'))
else:
plt.savefig('plots/error'+ '_' + suffix)