-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptimizer.py
More file actions
351 lines (273 loc) · 12.1 KB
/
Copy pathoptimizer.py
File metadata and controls
351 lines (273 loc) · 12.1 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
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
## basic imports ##
import pandas as pd
import numpy as np
import os
import matplotlib.pyplot as plt
import math
# numpy imports
from numpy import load
from numpy import loadtxt
from numpy import nan
from numpy import isnan
from numpy import count_nonzero
from numpy import unique
from numpy import array
from sklearn.base import clone
# linear
from sklearn.linear_model import LinearRegression
from sklearn.linear_model import Lasso
from sklearn.linear_model import Ridge
from sklearn.linear_model import ElasticNet
from sklearn.linear_model import HuberRegressor
from sklearn.linear_model import LassoLars
from sklearn.linear_model import PassiveAggressiveRegressor
from sklearn.linear_model import SGDRegressor
# non linear
from sklearn.neighbors import KNeighborsRegressor
from sklearn.tree import DecisionTreeRegressor
from sklearn.tree import ExtraTreeRegressor
from sklearn.svm import SVR
from sklearn.ensemble import AdaBoostRegressor
from sklearn.ensemble import BaggingRegressor
from sklearn.ensemble import RandomForestRegressor
from sklearn.ensemble import ExtraTreesRegressor
from sklearn.ensemble import GradientBoostingRegressor
# for optimization
from sklearn.model_selection import RandomizedSearchCV
from sklearn.model_selection import GridSearchCV
from sklearn.model_selection import RepeatedKFold
from pprint import pprint
import yaml
from sklearn.model_selection import TimeSeriesSplit
# input selection
from sklearn.feature_selection import SelectFromModel
from sklearn.feature_selection import RFE
from sklearn.feature_selection import SequentialFeatureSelector
def rmse(predictions, targets):
return np.sqrt(((predictions - targets) ** 2).mean())/np.std(predictions)
def select_features_linear(model, train_input, train_target):
"""
"""
## plot prediction vs real data
# x_line = np.arange(0, test_target.size)
# plt.plot(x_line, test_target, prediction)
# plt.legend(('real', 'predicted'))
# plt.title(f'target: {train_target.name}')
# plt.show()
## evaluate inputs
#importance = np.abs(model.coef_)
feature_names = np.array(train_input.keys())
k = 10
#Feature importance from coefficients
# plt.bar(height=importance, x=feature_names)
# plt.title(f'target: {train_target.name}')
# plt.show()
# # Selecting features based on coefficients
# k = 6
# idx = np.argpartition(importance, -k)
# pruned_list = feature_names[idx[-k:]]
#Selecting features based on importance
# threshold = np.sort(importance)[-k]
# sfm = SelectFromModel(model, prefit=True)
# pruned_list = sfm.get_feature_names_out(feature_names)
# RFE
# selector = RFE(model)
# selector = selector.fit(train_input, train_target)
# pruned_list = selector.get_feature_names_out(feature_names)
## Selecting features with Sequential Feature Selection (uses cross-validation)
selector = SequentialFeatureSelector(
model, n_features_to_select='auto', tol=0.001, direction="backward", scoring='neg_root_mean_squared_error', cv=TimeSeriesSplit())
selector = selector.fit(train_input, train_target)
pruned_list = selector.get_feature_names_out(feature_names)
# save
dict_to_save = {"pruned_list": pruned_list.tolist()}
file_name = f"config_{train_target.name}.yml"
relative_path = "/hyperparams"
save_path = os.path.dirname(__file__) + relative_path
completeName = os.path.join(save_path, file_name)
with open(completeName, 'w') as yaml_file:
yaml.dump(dict_to_save, yaml_file, default_flow_style=False)
print(f"Features selected by SelectFromModel: {pruned_list}")
return pruned_list
## data pruning
def select_features_forest(model, train_input, train_target):
"""
Args:
model (sklearn-model-obj): fitted model
"""
## forests
## get importance of each input per target
# importances = fitted_model.feature_importances_
# std = np.std([tree.feature_importances_ for tree in fitted_model.estimators_], axis=0)
# # plot importance
# forest_importances = pd.Series(importances, index = test_input.keys())
# fig, ax = plt.subplots()
# forest_importances.plot.bar(yerr=std, ax=ax)
# ax.set_title(f"Feature importances using MDI for target {test_target.name} ")
# ax.set_ylabel("Mean decrease in impurity")
# fig.tight_layout()
# plt.show()
# forests
feature_names = np.array(train_input.keys())
importance = model.feature_importances_
std = np.std([tree.feature_importances_ for tree in model.estimators_], axis=0)
# Selecting features based on coefficients
k = 10
idx = np.argpartition(importance, -k)
pruned_list = feature_names[idx[-k:]].tolist()
# plot importance
forest_importances = pd.Series(importance, index = train_input.keys())
fig, ax = plt.subplots()
forest_importances.plot.bar(yerr=std, ax=ax)
ax.set_title(f"Feature importances using MDI for target {train_target.name} ")
ax.set_ylabel("Mean decrease in impurity")
fig.tight_layout()
plt.show()
print(f"Features selected by SelectFromModel: {pruned_list}")
# save
dict_to_save = {"pruned_list": pruned_list}
file_name = f"config_{train_target.name}.yml"
relative_path = "/hyperparams"
save_path = os.path.dirname(__file__) + relative_path
completeName = os.path.join(save_path, file_name)
with open(completeName, 'w') as yaml_file:
yaml.dump(dict_to_save, yaml_file, default_flow_style=False)
return pruned_list
## hyper paramters tuning
def create_search_space(hparams=None):
"""
get hyper paras based on model TODO
create a random hyper-parameter grid to sample from during fitting
explanation of hyperparams:
n_estimators = number of trees in the forest
max_features = max number of features considered for splitting a node
max_depth = max number of levels in each decision tree
min_samples_split = min number of data points placed in a node before the node is split
min_samples_leaf = min number of data points allowed in a leaf node
bootstrap = method for sampling data points (with or without replacement)
Returns: random_grid
with form {name of hyperparam: [discrete values or a distribution of values]}
"""
# Look at parameters used by our current forest
# print('Parameters currently in use:\n')
# pprint(rf.get_params())
random_dict = {}
# random_dict['alpha'] = [int(x) for x in np.arange(start = 0, stop = 20, step = 1)]
# random_dict['fit_intercept'] = [True,False]
# random_dict['l1_ratio'] = [int(x) for x in np.arange(start = 0, stop = 1, step = 0.1)]
# # random_dict['normalize'] = [True, False]
# random_dict['selection'] = ['cyclic', 'random']
random_dict["alpha"] = [int(x) for x in np.arange(start = 1, stop = 20, step = 1)]
random_dict['fit_intercept'] = [True,False]
random_dict['max_iter'] = [int(x) for x in np.linspace(start = 0, stop = 1500, num = 100)]
random_dict['positive'] = [True,False]
random_dict['selection'] = ['cyclic', 'random']
random_dict['warm_start'] = [True, False]
random_dict['tol'] = [0.0001, 0.001, 0.01, 0.1]
# random_dict['C'] = [int(x) for x in np.arange(start = 0, stop = 20, step = 1)]
# random_dict['fit_intercept'] = [True,False]
# random_dict['shuffle'] = [False]
# random_dict['max_iter'] = [int(x) for x in np.linspace(start = 0, stop = 1000, num = 100)]
# # random_dict['l1_ratio'] = [int(x) for x in np.arange(start = 0, stop = 1, step = 0.1)]
# random_dict['early_stopping'] = [True, False]
# random_dict['selection'] = ['cyclic', 'random']
# # random_dict['criterion'] = ["squared_error", "absolute_error", "friedman_mse", "poisson"]
# random_dict['n_estimators'] = [int(x) for x in np.linspace(start = 0, stop = 500, num = 50)]
# random_dict['max_features'] = [int(x) for x in np.arange(start = 0, stop = 20, step = 1)]
# random_dict['max_depth'] = [int(x) for x in np.linspace(start = 30, stop = 70, num = 5)]
# # random_dict['max_depth'] = random_dict['max_depth'].append(None)
# random_dict['min_samples_split'] = [int(x) for x in np.arange(start = 60, stop = 100, step = 2)]
# random_dict['min_samples_leaf'] = [int(x) for x in np.arange(start = 0, stop = 20, step = 1)]
# random_dict['bootstrap'] = [True, False]
return random_dict
def rs_hparams(model, train_input, train_target):
"""
random search on random_dict
use of RandomizedSearchCV()
Args:
model to tune
random_grid: filled with random values for hyperparas of model
Returns:
best_hparams
"""
# Use the random grid to search for best hyperparameters
#cv = RepeatedKFold(n_splits=10, n_repeats=3, random_state=1)
cv = TimeSeriesSplit()
scoring = 'neg_root_mean_squared_error' # TODO check this is correct rmse # which score to improve
# random search part -> sampling randomly from a distribution
random_hparam_dict = create_search_space()
# Random search of parameters, using pre-defined fold cross validation,
# search across 100 different combinations, and use all available cores
# Fit the random search model
rf_random = RandomizedSearchCV(estimator = model, param_distributions = random_hparam_dict,
n_iter = 100, cv = cv, verbose=2, random_state=42, n_jobs = -1, scoring=scoring)
rf_random.fit(train_input, train_target)
best_hparams = rf_random.best_params_
pprint(best_hparams)
# save
file_name = f"{train_target.name}_params_lasso_not_scaled.yml"
relative_path = "/hyperparams"
save_path = os.path.dirname(__file__) + relative_path
completeName = os.path.join(save_path, file_name)
with open(completeName, 'w') as yaml_file:
yaml.dump(best_hparams, yaml_file, default_flow_style=False)
return best_hparams
def gs_hparams(model, random_dict, cv,scoring, train_input, train_target):
"""
grid search
GridSearchCV
"""
grid_search = GridSearchCV(estimator = model, param_grid = random_dict,
cv = cv, n_jobs = -1, verbose = 2, scoring=scoring)
grid_search.fit(train_input, train_target)
best_hparams = grid_search.best_params_
return best_hparams
def rs_gs_hparams(model, train_input, train_target):
"""
tune hyperparameters of random forest -esque models
- random search on search space
- feed result of random search (as ranges) to grid search
Returns:
best_hparams (dict)
"""
# define cv obj
# define evaluation
cv = RepeatedKFold(n_splits=10, n_repeats=3, random_state=1)
scoring = rmse() # which score to improve
# random search part -> sampling randomly from a distribution
random_hparam_dict = create_search_space()
best_hparams = rs_hparams(model, random_hparam_dict, cv, scoring, train_input, train_target)
pprint(best_hparams)
# feed a smaller range of values to grid search.
# grid search part -> evaluates all combinations we define
# TODO automatically Create the parameter grid based on the results of random search
random_hparam_dict = create_search_space(best_hparams)
best_hparams = rs_hparams(model, random_hparam_dict, cv, scoring, train_input, train_target)
pprint(best_hparams)
# save best
# return tuned_model
return best_hparams
def get_tuned_model(model, best_hparams):
"""
get model that is tuned based on a best_hparams dict
Returns:
tuned_model
"""
# for param_name in best_hparams:
# model.param_name = best_hparams
model = model.set_params(**best_hparams)
# print('Parameters currently in use:\n')
# pprint(model.get_params())
return model
def run_model(model_name, fitted_models_targets, eval_input, targets_dict):
predictions = {}
for target_name in targets_dict.keys():
fitted_model = fitted_models_targets[target_name][model_name]
predictions[target_name] = fitted_model.predict(eval_input)
predictions = pd.DataFrame.from_dict(predictions)
print("predictions")
####
# inputs_dict, targets_dict = prep_data()
# eval_all_tars = iter_all_tars(inputs_dict, targets_dict)
# eval_all_tars = pd.DataFrame.from_dict(eval_all_tars)
# print(eval_all_tars)