-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_FE_nfft_krr_iterate_Nfeat.py
356 lines (299 loc) · 13.2 KB
/
run_FE_nfft_krr_iterate_Nfeat.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
"""
Author: Theresa Wagner <theresa.wagner@math.tu-chemnitz.de>
Corresponding publication:
"Fast Evaluation of Additive Kernels: Feature Arrangement, Fourier Methods and Kernel Derivatives"
by T. Wagner, F. Nestler, M. Stoll (2024)
"""
####################################################################################
####################################################################################
"""
Compare the RMSE, window setup time and time for fitting and predicting the model
with the corresponding windows for different feature arrangement techniques and strategies,
different values Nfeat and fixed dmax=3.
"""
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from FE_nfft_kernel_ridge import GridSearch
#####################
## CHOOSE PARAMETERS
# kernel parameter ell
ell = 1
# regularization parameter beta
beta = 1
###############################
# type of prediction task
pred_type = "regression"
# kernel type
kernel = "gaussian"
## GridSearch with NFFTKernelRidge
param_grid_nfft = {
"sigma": [0.01, 0.1, 1, 10, 100],
"beta": [0.01, 0.1, 1, 10, 100],
}
## GridSearch with sklearn KRR
param_grid_krr = {
"gamma": [1/((0.01)**2), 1/((0.1)**2), 1/((1)**2), 1/((10)**2), 1/((100)**2)],
"alpha": [0.01, 0.1, 1, 10, 100],
}
#####################
#####################
# choose data set
#data = "keggundir"
data = "protein"
#N = 1000
#######################
if data == "keggundir": # 26 features
# https://archive.ics.uci.edu/ml/datasets/KEGG+Metabolic+Reaction+Network+(Undirected)
# read dataset
df = pd.read_csv('/data/KEGGUndir.txt', sep=",", header=None)
# same preprocessing as Jonathan Wenger in "Posterior and Computational Uncertainty in Gaussian Processes"
df.drop(df[df[4] == "?"].index, inplace=True)
df[4] = df[4].astype(float)
df.drop(df[df[21] > 1].index, inplace=True)
df.drop(columns=[10], inplace=True)
X = df.iloc[:,1:-1]
y = df.iloc[:,-1]
X = X.to_numpy()
Y = y.to_numpy()
# split data in train and test split
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, train_size=0.5, random_state=42)
# =============================================================================
# X_train = X_train[:N,:]
# Y_train = Y_train[:N]
# X_test = X_test[:N,:]
# Y_test = Y_test[:N]
# =============================================================================
###################
# https://scikit-learn.org/stable/auto_examples/compose/plot_transformed_target.html
# Transform outputs -> use mean of Y_train only to prevent train-test-contamination
Y_train = np.log1p(Y_train)
Y_train_mean = np.mean(Y_train, axis=0)
Ytrain = Y_train - Y_train_mean
Y_test = np.log1p(Y_test)
Ytest = Y_test - Y_train_mean
###################
elif data == "protein": # 9 features
# https://archive.ics.uci.edu/dataset/265/physicochemical+properties+of+protein+tertiary+structure
# read dataset
df = pd.read_csv('/data/protein.csv', sep=",", header=0)
X = df.iloc[:,1:]
y = df.iloc[:,0]
X = X.to_numpy()
Y = y.to_numpy()
# split data in train and test split
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, train_size=0.5, random_state=42)
# =============================================================================
# X_train = X_train[:N,:]
# Y_train = Y_train[:N]
# X_test = X_test[:N,:]
# Y_test = Y_test[:N]
# =============================================================================
###################
# https://scikit-learn.org/stable/auto_examples/compose/plot_transformed_target.html
# Transform outputs -> use mean of Y_train only to prevent train-test-contamination
Y_train = np.log1p(Y_train)
Y_train_mean = np.mean(Y_train, axis=0)
Ytrain = Y_train - Y_train_mean
Y_test = np.log1p(Y_test)
Ytest = Y_test - Y_train_mean
###################
print("\nDataset:", data)
print("--------------\nShape train data:", X_train.shape)
print("Shape test data:", X_test.shape)
###################
####################################################################################
#########
# Z-score normalize data
scaler = StandardScaler()
X_fit = scaler.fit(X_train)
X_train = X_fit.transform(X_train)
X_test = X_fit.transform(X_test)
######################################################################################
######################################################################################
# choose dmax
dmax = 3
# iterate over Nfeat
if data == "keggundir":
Nfeat = [9, 18, 26]
elif data == "protein":
Nfeat = [3, 6, 9]
# feature grouping methods
fg_consecdistr = ["dt", "mis", "fisher", "reliefFfilt", "reliefFwrap", "lasso", "elastic_net", "fc_cc"]
fg_direct = ["lasso", "elastic_net"]
fg_single = ["fc_cc"]
# other feature grouping methods
fg_other = ["consec"]
# initialize keys for dicts
keys = [i+"_consec" for i in fg_consecdistr] + [i+"_distr" for i in fg_consecdistr] + [i+"_direct" for i in fg_direct] + [i+"_single" for i in fg_single] + fg_other
print("keys:", keys)
# initialize dicts with results, windows and time for determining windows, and total time
fg_dict_res = {k: [] for k in keys}
fg_dict_wind = {k: [] for k in keys}
fg_dict_time = {k: [] for k in keys}
fg_dict_total_time = {k: [] for k in keys}
# initialize dict for saving best fg method and corresponding results
fg_best = {}
for Nf in Nfeat:
print("############################")
print("Solving for Nfeat = ", Nf)
print("############################")
######################################################################################
print("\nGridSearch for NFFTKernelRidge:")
print("-----------------------------------")
# initialize list with results yielded with fg methods
fg_res = []
# initialize list with windows yielded by fg methods
fg_wind = []
# initialize list with time needed for determining windows
fg_time = []
# initialize list with total time
fg_total_time = []
######################################################################################
# initialize NFFTKernelRidge model
model = GridSearch(classifier="NFFTKernelRidge", param_grid=param_grid_nfft, balance=False, norm=None, Nfg=1000, threshold=0.0, Nfeat=Nf, dmax=dmax, pred_type=pred_type, kernel=kernel)
# loop over feature grouping methods based on importance scores for fgmode="consec"
for method in fg_consecdistr:
results = model.tune(X_train, Ytrain, X_test, Ytest, window_scheme=method, fgmode="consec")
print("\n########################")
print("FG via %s" %method)
print("Windows:", results[7])
print("Time wind:", results[8])
print("Best Parameters:", results[0])
print("Best Result:", results[1])
print("Best Runtime Fit:", results[2])
print("Best Runtime Predict:", results[3])
print("Mean Runtime Fit:", results[4])
print("Mean Runtime Predict:", results[5])
print("Mean Total Runtime:", results[6])
print("########################\n")
fg_dict_res[method+"_consec"].append(results[1])
fg_dict_wind[method+"_consec"].append(results[7])
fg_dict_time[method+"_consec"].append(results[8])
fg_dict_total_time[method+"_consec"].append(results[6])
fg_res.append(results[1])
fg_wind.append(results[7])
fg_time.append(results[8])
fg_total_time.append(results[6])
# loop over feature grouping methods based on importance scores for fgmode="distr"
for method in fg_consecdistr:
results = model.tune(X_train, Ytrain, X_test, Ytest, window_scheme=method, fgmode="distr")
print("\n########################")
print("FG via %s" %method)
print("Windows:", results[7])
print("Time wind:", results[8])
print("Best Parameters:", results[0])
print("Best Result:", results[1])
print("Best Runtime Fit:", results[2])
print("Best Runtime Predict:", results[3])
print("Mean Runtime Fit:", results[4])
print("Mean Runtime Predict:", results[5])
print("Mean Total Runtime:", results[6])
print("########################\n")
fg_dict_res[method+"_distr"].append(results[1])
fg_dict_wind[method+"_distr"].append(results[7])
fg_dict_time[method+"_distr"].append(results[8])
fg_dict_total_time[method+"_distr"].append(results[6])
fg_res.append(results[1])
fg_wind.append(results[7])
fg_time.append(results[8])
fg_total_time.append(results[6])
# loop over feature grouping methods based on importance scores for fgmode="direct"
for method in fg_direct:
results = model.tune(X_train, Ytrain, X_test, Ytest, window_scheme=method, fgmode="direct")
print("\n########################")
print("FG via %s" %method)
print("Windows:", results[7])
print("Time wind:", results[8])
print("Best Parameters:", results[0])
print("Best Result:", results[1])
print("Best Runtime Fit:", results[2])
print("Best Runtime Predict:", results[3])
print("Mean Runtime Fit:", results[4])
print("Mean Runtime Predict:", results[5])
print("Mean Total Runtime:", results[6])
print("########################\n")
fg_dict_res[method+"_direct"].append(results[1])
fg_dict_wind[method+"_direct"].append(results[7])
fg_dict_time[method+"_direct"].append(results[8])
fg_dict_total_time[method+"_direct"].append(results[6])
fg_res.append(results[1])
fg_wind.append(results[7])
fg_time.append(results[8])
fg_total_time.append(results[6])
# loop over feature grouping methods based on importance scores for fgmode="single"
for method in fg_single:
results = model.tune(X_train, Ytrain, X_test, Ytest, window_scheme=method, fgmode="single")
print("\n########################")
print("FG via %s" %method)
print("Windows:", results[7])
print("Time wind:", results[8])
print("Best Parameters:", results[0])
print("Best Result:", results[1])
print("Best Runtime Fit:", results[2])
print("Best Runtime Predict:", results[3])
print("Mean Runtime Fit:", results[4])
print("Mean Runtime Predict:", results[5])
print("Mean Total Runtime:", results[6])
print("########################\n")
fg_dict_res[method+"_single"].append(results[1])
fg_dict_wind[method+"_single"].append(results[7])
fg_dict_time[method+"_single"].append(results[8])
fg_dict_total_time[method+"_single"].append(results[6])
fg_res.append(results[1])
fg_wind.append(results[7])
fg_time.append(results[8])
fg_total_time.append(results[6])
# loop over other feature grouping methods
for method in fg_other:
results = model.tune(X_train, Ytrain, X_test, Ytest, window_scheme=method)
print("\n########################")
print("FG via %s" %method)
print("Windows:", results[7])
print("Time wind:", results[8])
print("Best Parameters:", results[0])
print("Best Result:", results[1])
print("Best Runtime Fit:", results[2])
print("Best Runtime Predict:", results[3])
print("Mean Runtime Fit:", results[4])
print("Mean Runtime Predict:", results[5])
print("Mean Total Runtime:", results[6])
print("########################\n")
fg_dict_res[method].append(results[1])
fg_dict_wind[method].append(results[7])
fg_dict_time[method].append(results[8])
fg_dict_total_time[method].append(results[6])
fg_res.append(results[1])
fg_wind.append(results[7])
fg_time.append(results[8])
fg_total_time.append(results[6])
######################################################################################
######################################################################################
print("\nResults for dmax = ", dmax, ", Nfeat = ", Nf)
print("-----------------\n")
print("fg_wind:", fg_wind)
print("fg_res:", fg_res)
print("fg_time:", fg_time)
print("fg_total_time:", fg_total_time)
best_idx = np.argmin(fg_res)
best_res = np.min(fg_res)
best_wind = fg_wind[best_idx]
best_time = fg_time[best_idx]
best_total_time = fg_total_time[best_idx]
best = list(fg_dict_res.keys())[best_idx]
if best in list(fg_dict_wind.keys()):
print("best method: %s" %best, best_res, best_wind)
else:
print("best method: %s" %best, best_res)
# save best method and corresponding results for N
fg_best[dmax,Nf] = [best, best_res, best_wind, best_time, best_total_time]
#####################################################################################
print("\nRESULTS:")
print("----------\n")
print("data:", data)
print("fg_dict_res:", fg_dict_res)
print("fg_dict_wind:", fg_dict_wind)
print("fg_dict_time:", fg_dict_time)
print("fg_dict_total_time:", fg_dict_total_time)
print("\nfg_best:", fg_best)