-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot_comparison.py
More file actions
154 lines (115 loc) · 5.07 KB
/
plot_comparison.py
File metadata and controls
154 lines (115 loc) · 5.07 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
import os
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
from createTreeModel import _classification_ids
path_fig = os.path.join(
'figs_comparison',
'dataset_id={}-upc={}-n_estimators={}-{}.pdf'
)
labels = ['TreeGrad-Ranker with GA', 'TreeGrad-Ranker with ADAM', 'Banzhaf',
'Beta-Insertion', 'Beta-Deletion', 'Beta-Joint']
p = sns.color_palette("Set1", 10)
colors = [p[i] for i in [0,1,2,3,4,7]]
beta_shapley = [(16,1), (8,1), (4,1), (2,1), (1,1), (1,2), (1,4), (1,8),
(1,16), (1,32)]
n_players = {
4538 : 32,
44 : 57,
43174 : 81,
1475 : 51,
41150 : 50,
41145 : 308,
41168 : 54,
44975 : 48,
4549 : 77,
}
def skip_arg(arg):
if 'T_max' in arg:
if arg['T_max'] != 100:
return 1
if arg['lr'] != 5:
return 1
return 0
def plot_curves(r, upc, n_estimators, dataset_id):
path_components = path_fig.split(os.sep)
os.makedirs(os.sep.join(path_components[:-1]), exist_ok=True)
curves = []
curves.append(r['TreeGrad-Ranker'][0])
curves.append(r['TreeGrad-Ranker'][1])
curves.append(r['Banzhaf'])
beta = r['Beta']
beta_auc = beta.mean(axis=3)
index_insertion = np.argmax(beta_auc[:, :, 0], axis=0)
curves.append(beta[index_insertion, np.arange(200)])
index_deletion = np.argmin(beta_auc[:, :, 1], axis=0)
curves.append(beta[index_deletion, np.arange(200)])
index_joint = np.argmax(beta_auc[:,:,0] - beta_auc[:,:,1], axis=0)
curves.append(beta[index_joint, np.arange(200)])
x = np.arange(n_players[dataset_id] + 1)
for i, tp in enumerate(['insertion', 'deletion']):
fig, ax = plt.subplots(figsize=(32, 24))
for j, (label, curve) in enumerate(zip(labels, curves)):
curve_mean = curve[:, i].mean(axis=0)
ax.plot(x, curve_mean, linewidth=10, c=colors[j])
ax.tick_params(axis='x', labelsize=80)
ax.tick_params(axis='y', labelsize=80)
if dataset_id in _classification_ids:
plt.ylabel('predicted probability', fontsize=100)
else:
plt.ylabel('predicted value', fontsize=100)
if i:
plt.xlabel('#deleted features')
else:
plt.xlabel('#inserted features')
plt.grid()
plt.savefig(path_fig.format(dataset_id, upc, n_estimators, tp), bbox_inches='tight')
plt.close(fig)
def export_legend(legend, fig_saved):
fig = legend.figure
fig.canvas.draw()
bbox = legend.get_window_extent().transformed(fig.dpi_scale_trans.inverted())
fig.savefig(fig_saved, dpi="figure", bbox_inches=bbox)
if __name__ == '__main__':
from main import arg_dict
from args import process_arg_dict
from collections import defaultdict
for i, label in enumerate(labels):
plt.plot([], [], label=label, color=colors[i], linewidth=30)
legend = plt.legend(ncol=6, fontsize=100, loc="upper left", bbox_to_anchor=(1, 1))
export_legend(legend, 'legend_comparison.pdf')
args = process_arg_dict(arg_dict)
args_upc = defaultdict(list)
for arg in args:
if skip_arg(arg):
continue
args_upc[arg['use_predicted_class']].append(arg)
for upc, args_2nd in args_upc.items():
args_n_estimators = defaultdict(list)
for arg in args_2nd:
args_n_estimators[arg['n_estimators']].append(arg)
for n_estimators, args_3rd in args_n_estimators.items():
args_id = defaultdict(list)
for arg in args_3rd:
args_id[arg['dataset_id']].append(arg)
for dataset_id, args_4th in args_id.items():
r = dict()
r['TreeGrad-Ranker'] = np.empty((2, 200, 2, n_players[dataset_id] + 1), dtype=np.float64)
r['Banzhaf'] = np.empty((200, 2, n_players[dataset_id] + 1), dtype=np.float64)
r['Beta'] = np.empty((10, 200, 2, n_players[dataset_id] + 1), dtype=np.float64)
for arg in args_4th:
data = np.load(arg['path_results'])
if isinstance(arg['method'], tuple):
r['Beta'][beta_shapley.index(arg['method']), arg['sample_id']] = data['results'][1:]
elif arg['method'] == 0.5:
r['Banzhaf'][arg['sample_id']] = data['results'][1:]
elif arg['method'] == 'treegrad_ranker':
if arg['optimizer'] == 'GA':
r['TreeGrad-Ranker'][0, arg['sample_id']] = data['results'][1:]
elif arg['optimizer'] == 'Adam':
r['TreeGrad-Ranker'][1, arg['sample_id']] = data['results'][1:]
else:
raise ValueError
else:
raise ValueError
plot_curves(r, upc, n_estimators, dataset_id)