-
Notifications
You must be signed in to change notification settings - Fork 20
/
process_results.py
285 lines (256 loc) · 11.2 KB
/
process_results.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
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
from __future__ import print_function
import argparse
import os
import pickle
import sklearn
import numpy as np
import utils
def submodular_anchor_precrecall(z_anchor, dataset, preds_validation,
preds_test, k):
# returns picked, precisions, recalls
picked = utils.greedy_pick_anchor(
z_anchor['exps'],
dataset.data[z_anchor['validation_idx']],
k=k,
threshold=1.1)
precs = []
recs = []
for i in range(1, k + 1):
exs = picked[:i]
anchors = [z_anchor['exps'][i] for i in exs]
data_anchors = dataset.data[z_anchor['validation_idx']][exs]
pred_anchors = preds_validation[exs]
prec, rec = utils.evaluate_anchor(
anchors, data_anchors, pred_anchors,
dataset.data[z_anchor['test_idx']], preds_test,
threshold=1.1)
precs.append(prec)
recs.append(rec)
return picked, precs, recs
def random_anchor_precrecall(z_anchor, dataset, preds_validation, preds_test,
k, do_all=False):
# returns (anchor_prec, anchor_rec, anchor_prec_std, anchor_rec_std)
temp_prec_anchor = []
temp_rec_anchor = []
all_ids = range(len(z_anchor['exps']))
if do_all:
all_exs = np.array(all_ids).reshape((-1, 1))
else:
all_exs = [np.random.choice(all_ids, k, replace=False) for x in range(500)]
for exs in all_exs:
anchors = [z_anchor['exps'][i] for i in exs]
data_anchors = dataset.data[z_anchor['validation_idx']][exs]
pred_anchors = preds_validation[exs]
prec, rec = utils.evaluate_anchor(
anchors, data_anchors, pred_anchors,
dataset.data[z_anchor['test_idx']], preds_test,
threshold=1.1)
n = preds_test.shape[0]
temp_prec_anchor.append(prec)
temp_rec_anchor.append(rec)
# print (predicted_right / predicted, predicted / total, 0, 0)
return (np.mean(temp_prec_anchor), np.mean(temp_rec_anchor),
np.std(temp_prec_anchor), np.std(temp_rec_anchor))
def random_until_k(random_fn, k):
# random_fn takes k and returns prec, rec, prec_std, rec_std
# returns (precisions, recalls, precisions_stds, recalls_stds)
precs = []
recs = []
prec_stds = []
rec_stds = []
for i in range(1, k + 1):
prec, rec, prec_std, rec_std = random_fn(i)
precs.append(prec)
recs.append(rec)
prec_stds.append(prec_std)
rec_stds.append(rec_std)
return precs, recs, prec_stds, rec_stds
def submodular_lime_precrecall(z_lime, dataset, preds_validation, preds_test,
k, val_weights, val_vals, desired_precision=0,
to_change='distance', verbose=False,
threshold=0, pred_threshold=0.5):
# returns (picked, precisions, recalls, threshold, pred_threshold)
binary = np.bincount(preds_test).shape[0] <= 2
if desired_precision != 0:
thresholds = (np.linspace(0, 1, 101) if to_change == 'distance' else
np.linspace(0.5, 1, 51))
for t in thresholds:
if to_change == 'distance':
threshold = t
elif to_change == 'pred':
pred_threshold = t
else:
print('Error: to_change must be pred or distance, it is',
to_change)
quit()
picked = utils.submodular_coverage_pick(val_weights, val_vals,
threshold, pred_threshold,
binary, k, verbose=False)
exps = [z_lime['exps'][i] for i in picked]
data_exps = dataset.data[z_lime['validation_idx']][picked]
preds_exps = preds_validation[picked]
w, v = utils.compute_lime_weight_vals(
exps, data_exps, dataset.data[z_lime['test_idx']])
prec, rec = utils.evaluate_lime(
w, v, preds_exps, preds_test, threshold, pred_threshold,
binary=binary)
if verbose:
print(t, prec, rec)
if prec >= desired_precision:
break
picked_ = utils.submodular_coverage_pick(val_weights, val_vals,
threshold, pred_threshold,
binary, k)
precs = []
recs = []
for i in range(1, k + 1):
picked = picked_[:i]
exps = [z_lime['exps'][i] for i in picked]
data_exps = dataset.data[z_lime['validation_idx']][picked]
preds_exps = preds_validation[picked]
weights, vals = utils.compute_lime_weight_vals(
exps, data_exps, dataset.data[z_lime['test_idx']])
prec, rec = utils.evaluate_lime(weights, vals, preds_exps, preds_test,
threshold, pred_threshold,
binary=binary)
precs.append(prec)
recs.append(rec)
return picked, precs, recs, threshold, pred_threshold
return (prec, rec, threshold, pred_threshold)
def random_lime_precrecall(
z_lime, dataset, preds_validation, preds_test, k, desired_precision=0,
to_change='distance', do_all=False, verbose=False,
threshold=0, pred_threshold=0.5):
binary = np.bincount(preds_test).shape[0] <= 2
all_ids = range(len(z_lime['exps']))
if do_all:
all_exs = np.array(all_ids).reshape((-1, 1))
else:
all_exs = [np.random.choice(all_ids, k, replace=False) for x in range(500)]
ws = []
vs = []
for picked in all_exs:
exps = [z_lime['exps'][i] for i in picked]
data_exps = dataset.data[z_lime['validation_idx']][picked]
w, v = utils.compute_lime_weight_vals(
exps, data_exps, dataset.data[z_lime['test_idx']])
ws.append(w)
vs.append(v)
thresholds = (np.linspace(0, 1, 101) if to_change == 'distance' else
np.linspace(0.5, 1, 51))
for t in thresholds:
if desired_precision != 0:
if to_change == 'distance':
threshold = t
elif to_change == 'pred':
pred_threshold = t
else:
print('Error: to_change must be pred or distance, it is',
to_change)
quit()
temp_prec_lime = []
temp_rec_lime = []
for picked, w, v in zip(all_exs, ws, vs):
preds_exps = preds_validation[picked]
prec, rec = utils.evaluate_lime(
w, v, preds_exps, preds_test, threshold, pred_threshold,
binary=binary)
temp_prec_lime.append(prec)
temp_rec_lime.append(rec)
if verbose:
print(t, np.mean(temp_prec_lime), np.mean(temp_rec_lime))
if np.mean(temp_prec_lime) >= desired_precision:
return (np.mean(temp_prec_lime), np.mean(temp_rec_lime),
np.std(temp_prec_lime), np.std(temp_rec_lime),
threshold, pred_threshold)
return (1, 0, 0, 0, 1, 1)
def main():
parser = argparse.ArgumentParser(description='Graphs')
parser.add_argument(
'-p', dest='pickle_folder',
default='./out_pickles')
parser.add_argument('-d', dest='dataset', required=True,
choices=['adult', 'recidivism', 'lending'],
help='dataset to use')
parser.add_argument('-m', dest='model', required=True,
choices=['xgboost', 'logistic', 'nn'],
help='model: xgboost, logistic or nn')
parser.add_argument(
'-o', dest='output_folder',
default='./results')
args = parser.parse_args()
dataset = utils.load_dataset(args.dataset, balance=True)
dataset_name = args.dataset
algorithm = args.model
z_anchor = pickle.load(
open(os.path.join(args.pickle_folder, '%s-anchor-%s' % (
dataset_name, algorithm))))
z_lime = pickle.load(
open(os.path.join(args.pickle_folder, '%s-lime-%s' % (
dataset_name, algorithm))))
preds_validation = z_anchor['model'].predict(
z_anchor['encoder'].transform(
dataset.data[z_anchor['validation_idx']]))
preds_test = z_anchor['model'].predict(
z_anchor['encoder'].transform(
dataset.data[z_anchor['test_idx']]))
ret = {}
ret['accuracy'] = sklearn.metrics.accuracy_score(
dataset.labels[z_anchor['test_idx']], preds_test)
print('accuracy', ret['accuracy'])
print('Lime weights')
val_weights, val_vals = utils.compute_lime_weight_vals(
z_lime['exps'], dataset.data[z_lime['validation_idx']],
dataset.data[z_lime['validation_idx']])
print('Submodular anchor')
picked, precs, recs = submodular_anchor_precrecall(
z_anchor, dataset, preds_validation, preds_test, 10)
ret['anchor_submodular'] = (picked, precs, recs)
anchor_prec = precs[-1]
print('Submodular lime pred')
picked, precs, recs, t1, t2 = submodular_lime_precrecall(
z_lime, dataset, preds_validation, preds_test, 10, val_weights,
val_vals, desired_precision=anchor_prec, to_change='pred',
verbose=True)
ret['lime_pred_submodular'] = (picked, precs, recs)
ret['lime_pred_submodular_threshold'] = t2
print('Random anchor')
(prec, cov, prec_std, cov_std) = random_anchor_precrecall(
z_anchor, dataset, preds_validation, preds_test, 1, do_all=True)
ret['anchor_1'] = (prec, cov, prec_std, cov_std)
print('Random lime')
(prec, cov, prec_std, cov_std, _, _) = random_lime_precrecall(
z_lime, dataset, preds_validation, preds_test, k=1,
desired_precision=0.0, to_change='distance', verbose=True,
do_all=True)
ret['lime_naive_1'] = (prec, cov, prec_std, cov_std)
# print('Distance random lime')
# (prec, cov, prec_std, cov_std, t1, t2) = random_lime_precrecall(
# z_lime, dataset, preds_validation, preds_test, k=1,
# desired_precision=0.0, to_change='distance', verbose=True,
# do_all=True, threshold=ret['lime_distance_submodular_threshold'])
# ret['lime_distance_1'] = (prec, cov, prec_std, cov_std)
# ret['lime_distance_1_threshold'] = t1
print('Pred random lime')
(prec, cov, prec_std, cov_std, t1, t2) = random_lime_precrecall(
z_lime, dataset, preds_validation, preds_test, k=1,
desired_precision=0.0, to_change='pred', verbose=True,
do_all=True, pred_threshold=ret['lime_pred_submodular_threshold'])
ret['lime_pred_1'] = (prec, cov, prec_std, cov_std)
ret['lime_pred_1_threshold'] = t2
def random_fn_lime(k):
return random_lime_precrecall(
z_lime, dataset, preds_validation, preds_test, k=k,
desired_precision=0.0, to_change='pred', verbose=True,
do_all=False,
pred_threshold=ret['lime_pred_submodular_threshold'])[:4]
def random_fn_anchor(k):
return random_anchor_precrecall(
z_anchor, dataset, preds_validation, preds_test, k, do_all=False)
ret['anchor_random'] = random_until_k(random_fn_anchor, 10)
ret['lime_pred_random'] = random_until_k(random_fn_lime, 10)
path = os.path.join(args.output_folder, '%s-%s.pickle' % (
dataset_name, algorithm))
pickle.dump(ret, open(path, 'w'))
if __name__ == '__main__':
main()