-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathplot_simulation_general.py
238 lines (226 loc) · 8.03 KB
/
plot_simulation_general.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
import sys
import os
import argparse
import pickle
import numpy as np
import matplotlib
matplotlib.use('Agg')
from matplotlib import pyplot as plt
import seaborn as sns
import pandas as pd
import csv
from common import process_params, make_params
METHOD_DICT = {
"spinn": "SPINN",
"ridge_nn": "Ridge-only NN",
"oracle_nn": "Oracle NN",
"spam": "SpAM",
"lasso": "Lasso",
"GAM multivariate": "GAM multivariate",
"GAM univariate": "GAM univariate",
"trees": "Random forest",
}
def parse_args(args):
parser = argparse.ArgumentParser("Plot for the main simulations")
parser.add_argument(
'--result-folder',
type=str,
default="simulation_univar_additive")
parser.add_argument(
'--spinn-template',
type=str,
#default="_output/seed_%d/n_train_%d/fitted_spinn.pkl")
default="_output/seed_%d/n_train_%d/fitted_spinn.pkl")
parser.add_argument(
'--file-template',
type=str,
default="_output/seed_%d/n_train_%d/fitted_%s.csv")
parser.add_argument(
'--methods',
type=str,
default="spam,lasso,spinn,gam,trees,ridge_nn,oracle_nn")
parser.add_argument(
'--max-relevant-idx',
type=int,
default=6)
parser.add_argument(
'--n-trains',
type=str,
#default=make_params([125, 250, 500, 1000]))
default=make_params([125, 250, 500, 1000, 2000, 4000, 8000]))
parser.add_argument(
'--seeds',
type=str,
default=make_params(range(2,22)))
parser.add_argument(
'--out-plot',
type=str,
default="_output/plot_simulation_mse.png")
parser.add_argument(
'--out-weight-plot',
type=str,
default="_output/plot_simulation_weights.png")
parser.add_argument(
'--show-legend',
action='store_true')
parser.set_defaults()
args = parser.parse_args(args)
args.methods = args.methods.split(",")
args.seeds = process_params(args.seeds, int)
args.n_trains = process_params(args.n_trains, int)
return args
def read_method_result(res_file, method):
with open(res_file, "r") as f:
print(res_file)
reader = csv.reader(f)
header = next(reader)
if method == "trees":
row = next(reader)
return [(method, float(row[1]), np.nan)]
elif method == "gam":
results = []
for row in reader:
results.append((row[0], float(row[1]), 6))
return results
elif method == "spam":
for row in reader:
if "test" in row[0]:
mse = float(row[1])
elif "zero" in row[0]:
num_nonzero = int(float(row[1]))
return [(method, mse, num_nonzero)]
elif method == "lasso":
for row in reader:
if row[0] == "test_loss" or row[0] == "Lasso" or row[0] == "Lasso test accuracy":
mse = float(row[1])
elif row[0] == "num_nonzero" or row[0] == "Lasso nonzero elems":
num_nonzero = int(float(row[1]))
return [(method, mse, num_nonzero)]
elif method == "spinn" or method == "spinn_new":
for row in reader:
if row[0] == "test_loss":
mse = float(row[1])
elif row[0] == "num_nonzero":
num_nonzero = int(float(row[1]))
return [(method, mse, num_nonzero)]
elif method == "ridge_nn":
for row in reader:
if row[0] == "test_loss":
mse = float(row[1])
elif row[0] == "num_nonzero":
num_nonzero = int(float(row[1]))
return [(method, mse, num_nonzero)]
elif method == "oracle_nn":
for row in reader:
if row[0] == "test_loss":
mse = float(row[1])
elif row[0] == "num_nonzero":
num_nonzero = int(float(row[1]))
return [(method, mse, num_nonzero)]
else:
raise ValueError("what method?! %s" % method)
def plot_mse(args):
all_results = {
"method": [],
"mse": [],
"n_train": [],
"seed": []}
for seed in args.seeds:
for n_train in args.n_trains:
for method in args.methods:
res_file = os.path.join(args.result_folder,
args.file_template % (
seed,
n_train,
method))
try:
results = read_method_result(res_file, method)
except:
print("cant find", res_file)
continue
for method_str, mse, num_nonzero in results:
if method_str == "spinn" and num_nonzero == 0:
continue
all_results["method"].append(METHOD_DICT[method_str])
all_results["mse"].append(float(mse))
all_results["seed"].append(seed)
all_results["n_train"].append(int(n_train))
results_df = pd.DataFrame(all_results)
print("RESULTS")
pivot_df = results_df[results_df['n_train'] == 500].pivot(index='seed', columns='method', values='mse')
print(pivot_df)
plt.clf()
if args.show_legend:
plt.figure(figsize=(5,5))
else:
plt.figure(figsize=(5,4))
hue_order = list(sorted(list(set(all_results["method"]))))
print(hue_order)
sns_plt = sns.pointplot(
x="n_train",
y="mse",
hue="method",
data=results_df,
hue_order=hue_order,
linestyles=["--" if ("GAM" in h or "Oracle" in h) else "-" for h in hue_order],
markers=["o" if ("GAM" in h or "Oracle" in h) else "v" for h in hue_order])
sns_plt.set_yscale('log')
plt.xlabel("Number of observations")
plt.ylabel("Mean squared error")
if args.show_legend:
plt.legend(loc='upper center', bbox_to_anchor=(0.5, -0.25), ncol=3)
plt.tight_layout()
else:
plt.legend(loc='upper center', bbox_to_anchor=(0.5, -0.25), ncol=3)
plt.savefig(os.path.join(args.result_folder, args.out_plot))
def plot_weights(args):
all_results = {
"relevant": [],
"weight": [],
"n_train": [],
"seed": []}
for seed in args.seeds:
for n_train in args.n_trains:
res_file = os.path.join(args.result_folder,
args.spinn_template % (
seed,
n_train))
with open(res_file, "rb") as f:
spinn_res = pickle.load(f)
num_p = spinn_res["model_params"].coefs[0].shape[0]
norm_nonzero_inputs = [
np.linalg.norm(spinn_res["model_params"].coefs[0][i,:], ord=2)
for i in range(num_p)
]
results = [
[True, np.sum(norm_nonzero_inputs[:args.max_relevant_idx])],
[False, np.sum(norm_nonzero_inputs[args.max_relevant_idx:])],
]
for relevant, mean_weight in results:
all_results["relevant"].append(relevant)
all_results["weight"].append(mean_weight)
all_results["seed"].append(seed)
all_results["n_train"].append(int(n_train))
results_df = pd.DataFrame(all_results)
#print("RESULTS")
#print(results_df)
plt.clf()
plt.figure(figsize=(5,4))
sns_plt = sns.pointplot(
x="n_train",
y="weight",
hue="relevant",
data=results_df,
linestyles=["-", "--"],
markers=['o','v'])
sns_plt.set_yscale('log')
plt.xlabel("Number of observations")
plt.ylabel("Log (L2 norm of irrelevant weights)")
plt.tight_layout()
plt.savefig(os.path.join(args.result_folder, args.out_weight_plot))
def main(args=sys.argv[1:]):
args = parse_args(args)
plot_mse(args)
plot_weights(args)
if __name__ == "__main__":
main()