-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain_hypertune.py
217 lines (178 loc) · 8.05 KB
/
main_hypertune.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
'''
Hyptertuning models
'''
from src import feat, mod
import optuna
res_results = {}
##############################################
# Setting up data and variable sets
train_dat = feat.get_data(split_pred=True)
# Model fit with weights
weight_cats = tuple([False])
# Model fit with categorical variables
cat_cats = (True,False)
# Lat/Lon included in model
xy_cats = {'no': [],
'both': ['latitude','longitude'],
'lat': ['latitude'],
'lon': ['longitude']}
xy_keys = tuple(xy_cats.keys())
# Region variables
region_cats = {'both': ['region','cluster'],
'reg': ['region'],
'clust': ['cluster']}
reg_keys = tuple(region_cats.keys())
# Elevation Variables
ele_cats = {'max_dif':['maxe','dife'],
'all_var':['maxe','dife','elevation','stde'],
'ele_std':['elevation','stde'],
'ele_dif':['elevation','dife'],
'max_std':['maxe','stde'] }
ele_keys = tuple(ele_cats.keys())
# Spatial Lag Variables
sl_cats = {#'lag100': ['severity_100','logDensity_100','count_100'],
#'lag300': ['severity_300','logDensity_300','count_300'],
#'lag1000': ['severity_1000','logDensity_1000','count_1000'],
'lagNone': []}
sl_keys = tuple(sl_cats.keys())
# Sat imagery data
sat_cats = {'sat500': ['imtype','prop_lake_500','r_500','g_500','b_500'],
'sat1000': ['imtype','prop_lake_1000','r_1000','g_1000','b_1000'],
'sat2500': ['imtype','prop_lake_2500','r_2500','g_2500','b_2500'],
'sat500_1000': ['imtype','prop_lake_500','r_500','g_500','b_500', 'prop_lake_1000','r_1000','g_1000','b_1000'],
'sat500_2500': ['imtype','prop_lake_500','r_500','g_500','b_500', 'prop_lake_2500','r_2500','g_2500','b_2500'],
'sat1000_2500': ['imtype','prop_lake_1000','r_1000','g_1000','b_1000', 'prop_lake_2500','r_2500','g_2500','b_2500']}
sat_keys = tuple(sat_cats.keys())
##############################################
##############################################
# LightBoost hyperparameter
def objective_lgb(trial):
param = {
"n_estimators": trial.suggest_int("n_estimators", 20, 600, 10),
"max_depth": trial.suggest_int("max_depth", 2, 10),
"ele_set": trial.suggest_categorical("ele_vars", ele_keys),
"xy_set": trial.suggest_categorical("xy_set", xy_keys),
"sl_set": trial.suggest_categorical("sl_set", sl_keys),
"reg_set": trial.suggest_categorical("reg_set", reg_keys),
"weight": trial.suggest_categorical("weight", weight_cats),
"cat_type": trial.suggest_categorical("cat_type", cat_cats),
"sat_set": trial.suggest_categorical("sat_set", sat_keys),
}
# Setting the different variables
ov = region_cats[param['reg_set']]
#if 'imtype' in sat_cats[param['sat_set']]:
# ov.append('imtype')
cv = ele_cats[param['ele_set']] + xy_cats[param['xy_set']]
cv += sl_cats[param['sl_set']]
cv += sat_cats[param['sat_set']]
rm = mod.RegMod(ord_vars=ov,
dum_vars=None,
dat_vars=['date'],
ide_vars=cv,
weight = 'split_pred',
y='severity',
mod = mod.LGBMRegressor(n_estimators=round(param['n_estimators']),
max_depth=param['max_depth']))
avg_rmse = rm.met_eval(train_dat,ret=True,weight=param['weight'],cat=param['cat_type'])
return avg_rmse
study_lgb = optuna.create_study(direction="minimize")
study_lgb.optimize(objective_lgb, n_trials=300) # 150
trial_lgb = study_lgb.best_trial
res_results['lgb'] = trial_lgb
#print(f"Best Average RMSE LightBoost {trial_lgb.value}")
# Best Average RMSE LightBoost 0.7550269937373841
#print("Best Params")
#print(trial_lgb.params)
# {'n_estimators': 380, 'max_depth': 5, 'ele_vars': 'ele_std', 'xy_set': 'both', 'reg_set': 'reg', 'weight': True, 'cat_type': True}
##############################################
##############################################
# XGBoost hyperparameter
def objective_xgb(trial):
param = {
"n_estimators": trial.suggest_int("n_estimators", 20, 600, 10),
"max_depth": trial.suggest_int("max_depth", 2, 10),
"ele_set": trial.suggest_categorical("ele_vars", ele_keys),
"xy_set": trial.suggest_categorical("xy_set", xy_keys),
"sl_set": trial.suggest_categorical("sl_set", sl_keys),
"reg_set": trial.suggest_categorical("reg_set", reg_keys),
"weight": trial.suggest_categorical("weight", weight_cats),
"sat_set": trial.suggest_categorical("sat_set", sat_keys),
}
# Setting the different variables
ov = region_cats[param['reg_set']]
#if 'imtype' in sat_cats[param['sat_set']]:
# ov.append('imtype')
cv = ele_cats[param['ele_set']] + xy_cats[param['xy_set']]
cv += sl_cats[param['sl_set']]
cv += sat_cats[param['sat_set']]
rm = mod.RegMod(ord_vars=ov,
dum_vars=None,
dat_vars=['date'],
ide_vars=cv,
weight = 'split_pred',
y='severity',
mod = mod.XGBRegressor(n_estimators=round(param['n_estimators']),
max_depth=param['max_depth'])
)
avg_rmse = rm.met_eval(train_dat,ret=True,weight=param['weight'])
return avg_rmse
study_xgb = optuna.create_study(direction="minimize")
study_xgb.optimize(objective_xgb, n_trials=300)
trial_xgb = study_xgb.best_trial
res_results['xgb'] = trial_xgb
#print(f"Best Average RMSE XGBoost {trial_xgb.value}")
# Best Average RMSE XGBoost 0.7703229517195069
#print("Best Params")
#print(trial_xgb.params)
# {'n_estimators': 220, 'max_depth': 9, 'ele_vars': 'ele_std', 'xy_set': 'both', 'reg_set': 'both', 'weight': True}
##############################################
##############################################
# CatBoost hyperparameter
def objective_cat(trial):
param = {
"n_estimators": trial.suggest_int("n_estimators", 20, 600, 10),
"max_depth": trial.suggest_int("max_depth", 2, 10),
"ele_set": trial.suggest_categorical("ele_vars", ele_keys),
"xy_set": trial.suggest_categorical("xy_set", xy_keys),
"sl_set": trial.suggest_categorical("sl_set", sl_keys),
"reg_set": trial.suggest_categorical("reg_set", reg_keys),
"weight": trial.suggest_categorical("weight", weight_cats),
"cat_type": trial.suggest_categorical("cat_type", cat_cats),
"sat_set": trial.suggest_categorical("sat_set", sat_keys),
}
# Setting the different variables
ov = region_cats[param['reg_set']]
#if 'imtype' in sat_cats[param['sat_set']]:
# ov.append('imtype')
cv = ele_cats[param['ele_set']] + xy_cats[param['xy_set']]
cv += sl_cats[param['sl_set']]
cv += sat_cats[param['sat_set']]
rm = mod.RegMod(ord_vars=ov,
dum_vars=None,
dat_vars=['date'],
ide_vars=cv,
weight = 'split_pred',
y='severity',
mod = mod.CatBoostRegressor(iterations=round(param['n_estimators']),
depth=param['max_depth'],
allow_writing_files=False,
verbose=False)
)
avg_rmse = rm.met_eval(train_dat,ret=True,weight=param['weight'],cat=param['cat_type'])
return avg_rmse
study_cat = optuna.create_study(direction="minimize")
study_cat.optimize(objective_cat, n_trials=300)
trial_cat = study_cat.best_trial
res_results['cat'] = trial_cat
#print(f"Best Average RMSE CatBoost {trial_cat.value}")
#Best Average RMSE CatBoost 0.7526218612441193
#print("Best Params")
#print(trial_cat.params)
# {'n_estimators': 550, 'max_depth': 9, 'ele_vars': 'ele_std', 'xy_set': 'both', 'reg_set': 'reg', 'weight': False, 'cat_type': False}
##############################################
# Printing Results
print('\n\nTRIAL RESULTS\n\n')
for m,t in res_results.items():
print(f"Best Average RMSE {m} {t.value}")
print("Best Params")
print(t.params)