forked from khaendler/ProeFI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_plots.py
More file actions
255 lines (213 loc) · 8.6 KB
/
create_plots.py
File metadata and controls
255 lines (213 loc) · 8.6 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
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
import os
import re
import itertools
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from pathlib import Path
import scipy.stats as ss
from critdd import Diagram
import seaborn as sns
from river import metrics
from tree import ProeFI
from utils.io_helpers import load
from ipfi.scaler import MinMaxScaler
from data.datasets.experiment_datasets import DriftingAgrawal
model_names = ["ht",
"hat",
"efdt",
"proefi",
]
data_names = ["airlines", "electricity", "covtype", "nomao", "kdd99", "wisdm",
"agr_a", "agr_g", "rbf_f", "rbf_m", "led_a", "led_g"
]
seeds = [40, 41, 42, 43, 44]
plot_dir = "./results/plots"
Path(plot_dir).mkdir(parents=True, exist_ok=True)
def get_df_summary(metric, data_dir="./results"):
if metric != "tradeoff":
table = pd.DataFrame(index=data_names, columns=model_names)
for model in model_names:
for data_name in data_names:
value = 0
seeds_seen = 0
for seed in seeds:
file_path = f"{data_dir}/summary/{model}_seed{seed}_{data_name}.csv"
if os.path.exists(file_path):
df = pd.read_csv(file_path)
value += df.iloc[0][metric]
seeds_seen += 1
else:
try:
df = pd.read_csv(f"{data_dir}/summary/{model}_{data_name}.csv")
value += df.iloc[0][metric]
seeds_seen += 1
break
except:
pass
if seeds_seen > 0:
value /= seeds_seen
table.at[data_name, model] = value
table.index = table.index.str.replace("_", " ")
else:
table1 = get_df_summary(metric="Auroc", data_dir=data_dir)
table2 = get_df_summary(metric="Avg Node Count", data_dir=data_dir)
table2 = table2.apply(pd.to_numeric)
table2 = np.log2(table2) + 1
table = table1 / table2
return table
def make_latex_table(metric, highlight_max=True, precision=3, data_dir="./results"):
table = get_df_summary(metric=metric, data_dir=data_dir)
# Add mean ranks
ranks = table.rank(axis=1, method='average', ascending=False if highlight_max else True)
mean_ranks = ranks.mean()
table.loc['Mean Rank'] = mean_ranks
if highlight_max:
print(table.style.highlight_max(axis=1, props="textbf:--rwrap;").format(precision=precision).to_latex())
else:
print(table.style.highlight_min(axis=1, props="textbf:--rwrap;").format(precision=precision).to_latex())
# https://mirkobunse.github.io/critdd/
# Install with: pip install 'critdd @ git+https://github.com/mirkobunse/critdd'
def get_cridd(metric, data_dir="./results"):
df = get_df_summary(metric=metric, data_dir=data_dir)
df = df.rename_axis('dataset_name', axis=0)
df = df.rename_axis('classifier_name', axis=1)
df = df.astype(np.float64)
# create a CD diagram from the Pandas DataFrame
diagram = Diagram(
df.to_numpy(),
treatment_names=df.columns,
maximize_outcome=True
)
# inspect average ranks and groups of statistically indistinguishable treatments
diagram.average_ranks # the average rank of each treatment
diagram.get_groups(alpha=.05, adjustment="holm")
# export the diagram to a file
diagram.to_file(
f"./results/plots/critdd-{metric}.tex",
alpha=.05,
adjustment="holm",
reverse_x=True,
)
def plot_tau_values(metric, data_dir="./results"):
taus = ['$\\tau_t$']
taus.extend([0.01 * i for i in range(1, 11)])
taus.extend([0.2, 0.3, 0.4, 0.5])
# Read files
table = pd.DataFrame(index=data_names, columns=taus)
for tau in taus:
if tau == "$\\tau_t$":
model_name = "proefi"
else:
model_name = f"proefi_tau_{tau}"
for data_name in data_names:
value = 0
seeds_seen = 0
for seed in seeds:
file_path = f"{data_dir}/summary/{model_name}_seed{seed}_{data_name}.csv"
if os.path.exists(file_path):
df = pd.read_csv(file_path)
value += df.iloc[0][metric]
seeds_seen += 1
if seeds_seen > 0:
value /= seeds_seen
table.at[data_name, tau] = value
table.index = table.index.str.replace("_", " ")
table = table.apply(pd.to_numeric, errors='coerce')
plt.rcParams.update({'font.size': 16})
sns.boxplot(table)
if metric == "Avg Node Count":
plt.yscale("log")
plt.ylabel("#nodes")
else:
plt.ylabel("AUROC")
plt.xlabel("$\\tau$ values")
plt.tick_params("x", rotation=45)
plt.tight_layout()
metric = metric.replace(" ", "_")
plt.savefig(f"./results/plots/boxplot_tau_threshold_{metric}.pdf", format="pdf", bbox_inches="tight")
def plot_model_behavior():
""" This first trains ProeFI on Agrawal with an abrupt concept drift.
Then it plots the feature importance, AUROC and node count over time.
"""
model = ProeFI(seed=40)
data = DriftingAgrawal(width=50, seed=40)
metric = metrics.ROCAUC()
auc_list = []
node_list = []
for i, (x, y) in enumerate(data.take(1000000), start=1):
y_pred = model.predict_proba_one(x)
metric.update(y, y_pred)
auc_list.append(metric.get())
node_list.append(model.n_nodes)
model.learn_one(x, y)
fi_values = model.collected_importance_values
plot_feature_importance(fi_values)
plot_auroc_and_nodes(auc_list, node_list)
def plot_feature_importance(fi_values):
plt.rcParams.update({'font.size': 16})
fig, ax = plt.subplots(figsize=(8, 5))
x = list(range(len(fi_values)))
scaler = MinMaxScaler()
for row in fi_values:
scaler.learn_one(row)
fi_values = [scaler.transform_one(row) for row in fi_values]
highlight_set = set()
all_keys = list(fi_values[0].keys())
totals = {key: sum(abs(row[key]) for row in fi_values) for key in all_keys}
top_keys = sorted(totals, key=totals.get, reverse=True)[:4]
highlight_set.update(top_keys)
default_colors = plt.rcParams['axes.prop_cycle'].by_key()['color']
color_cycle = itertools.cycle(default_colors)
key_to_color = {key: next(color_cycle) for key in highlight_set}
for key in all_keys:
y = [row[key] for row in fi_values]
if key in key_to_color:
color = key_to_color[key]
label = key
z = 3
else:
color = 'lightgrey'
label = None
z = 1
ax.plot(x, y, color=color, label=label, linewidth=1, zorder=z)
for xv in [250000, 500000, 750000]:
ax.axvline(x=xv, color='grey', linestyle='--', linewidth=1)
ax.legend(loc='upper right')
plt.xlabel("Instances")
plt.ylabel("Feature importance")
plt.tight_layout()
plt.savefig(f"./results/plots/plot_fi_over_time.pdf", format="pdf", bbox_inches="tight")
def plot_auroc_and_nodes(auc_list, node_list):
x = np.arange(1, len(auc_list) + 1)
plt.rcParams.update({'font.size': 16})
fig, ax1 = plt.subplots(figsize=(8, 5))
color_auc = 'tab:blue'
ax1.plot(x[100:], auc_list[100:], color=color_auc, label='AUROC')
ax1.set_xlabel('Instances')
ax1.set_ylabel('AUROC')
ax1.tick_params(axis='y')
ax2 = ax1.twinx()
color_nodes = 'tab:red'
ax2.plot(x[100:], node_list[100:], color=color_nodes, label='#nodes')
ax2.set_ylabel('#nodes')
ax2.set_yscale("log")
ax2.tick_params(axis='y')
for xv in [250000, 500000, 750000]:
ax1.axvline(x=xv, color='grey', linestyle='--', linewidth=1)
lines, labels = ax1.get_legend_handles_labels()
lines2, labels2 = ax2.get_legend_handles_labels()
ax1.legend(lines + lines2, labels + labels2)
plt.tight_layout()
plt.savefig(f"./results/plots/auroc_nodecount_plot.pdf", format="pdf", bbox_inches="tight")
if __name__ == '__main__':
data_dir = "./results"
# plot_performances_all_data()
# plot_fi_importance_all_data(data_dir=data_dir)
make_latex_table(metric='Auroc', precision=2, data_dir=data_dir)
make_latex_table(metric='Avg Node Count', highlight_max=False, precision=2, data_dir=data_dir)
make_latex_table(metric='tradeoff', highlight_max=True, precision=3, data_dir=data_dir)
get_cridd(metric='tradeoff', data_dir=data_dir)
plot_tau_values(metric='Auroc', data_dir=data_dir)
plot_tau_values(metric='Avg Node Count', data_dir=data_dir)
plot_model_behavior()