-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcross_validate_routes.py
394 lines (325 loc) · 12.5 KB
/
cross_validate_routes.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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
import os
import pandas as pd
import numpy as np
import psycopg2
from sklearn.ensemble import GradientBoostingRegressor
from sklearn.model_selection import train_test_split, KFold
from sklearn.metrics import mean_squared_error
from train_all_routes import get_route_dir_list
import multiprocessing
def cross_validate_routes():
route_dir_list = get_route_dir_list()
for i, route_dir in enumerate(route_dir_list):
print("starting process for {} - #{} out of {}".format(
route_dir, i, len(route_dir_list)))
get_best_route_params(route_dir)
def get_best_route_params(route_dir):
'''
INPUT
------
route_dir - unique route direction id
OUTPUT
-------
updated cv database with optimal depth and alpha for the route_dir
'''
db_name = os.environ["RDS_NAME"]
user = os.environ["RDS_USER"]
key = os.environ["RDS_KEY"]
host = os.environ["RDS_HOST"]
port = os.environ["RDS_PORT"]
conn = psycopg2.connect(dbname=db_name,
user=user,
password=key,
host=host,
port=port)
#you can always change these grid paramters
n_folds = 6
tree_depths = [3, 5, 7, 9]
#don't cross validate alphas - in the future, you can train a model
#with different alphas to give the user predictions based on
#a choosen quantile
#alphas = [0.7, 0.75, 0.8, 0.85, 0.9]
n_estimators = 1500
print("getting tree params")
tree_params = get_route_params(route_dir, tree_depths,
n_folds, n_estimators)
print("cross validate for max depth")
#FYI - this runs quickest on a big EC2 instance with many processors
n_pools = multiprocessing.cpu_count() - 2
pool1 = multiprocessing.Pool(n_pools)
cv_depth_result = pool1.map(crossval_one_depth, tree_params)
pool1.close()
'''
print("cross validate for alpha")
#BEWARE! this number is dependent on a big EC2 instance
pool2 = multiprocessing.Pool(n_pools)
cv_alpha_result = pool2.map(crossval_one_alpha, alpha_params)
pool2.close()
'''
print("find best depth")
best_depth = find_best_depth(cv_depth_result, n_estimators, n_folds,
tree_depths)
'''
print("find best alpha")
best_alpha = find_best_alpha(cv_alpha_result, n_estimators, n_folds,
alphas)
'''
print("update cv database")
update_cv_database(conn, best_depth, route_dir)
#during testing phase return some params to quality check
#return cv_depth_result, cv_alpha_result, best_depth, best_alpha
def get_route_params(route_dir, tree_depths, n_folds,
n_estimators):
'''This is a function to process user input into the model format
INPUT
-------
route_dir - route and direction unique id
OUTPUT
-------
tree_params
alpha_params - no longer using alpha
'''
db_name = os.environ["RDS_NAME"]
user = os.environ["RDS_USER"]
key = os.environ["RDS_KEY"]
host = os.environ["RDS_HOST"]
port = os.environ["RDS_PORT"]
conn = psycopg2.connect(dbname=db_name,
user=user,
password=key,
host=host,
port=port)
cur = conn.cursor()
route_id = route_dir.split("_")[0]
direction = route_dir.split("_")[1]
model_col_list = ['route_dir_stop','stop_sequence','month', 'day', 'hour','dow','delay']
select_string = column_list_to_string(model_col_list)
query = '''
select {}
from updates
where route_id = {}
and direction_id = {}
and time_pct < '2018-01-23'
'''.format(select_string, route_id, direction)
print('getting historical route information')
cur.execute(query)
query_list = cur.fetchall()
result = pd.DataFrame(query_list, columns=model_col_list)
y = (result.iloc[:,-1].values)/60
result = result.drop('delay', axis=1)
dummy_col = ['route_dir_stop','month', 'day', 'hour','dow']
result_dummies = pd.get_dummies(result,columns=dummy_col)
all_column_list = list(result_dummies.columns)
all_columns_str = column_list_to_string(all_column_list)
X = result_dummies.values
#change CV params as necessary
tree_params = make_tree_params(n_estimators, tree_depths, n_folds,
X, y)
#alpha_params = make_alpha_params(n_estimators, alphas, n_folds, X, y)
cur.close()
conn.close()
return tree_params
def make_tree_params(n_estimators, tree_depths, n_folds, X, y):
"""
Create a list of parameters to input into crossval_one for parallelization.
Input
------
tree_depths : List of tree_depths to gridsearch across
n_folds : The number of folds to use in K-fold CV
X : Full dataset, a numpy array
y : Labels, a numpy array
Output
------
params : A list containing tuples (td, k, X_train, y_train, X_test, y_test)
The length of params will be len(tree_depths) * n_folds
"""
n_estimators = n_estimators
params = []
kf = KFold(n_splits=n_folds, shuffle=True, random_state=1)
for td in tree_depths:
for k, (train_idxs, test_idxs) in enumerate(kf.split(X)):
X_train, y_train = X[train_idxs, :], y[train_idxs]
X_test, y_test = X[test_idxs, :], y[test_idxs]
params.append((n_estimators, td, k, X_train, y_train, X_test, y_test))
return params
def make_alpha_params(n_estimators, alphas, n_folds, X, y):
"""
Create a list of parameters to input into crossval_one for parallelization.
Input
------
alphas : List of alphas to gridsearch across
n_folds : The number of folds to use in K-fold CV
X : Full dataset, a numpy array
y : Labels, a numpy array
Output
------
params : A list containing tuples (alpha, k, X_train, y_train, X_test, y_test)
The length of params will be len(alphas) * n_folds
"""
n_estimators = n_estimators
params = []
kf = KFold(n_splits=n_folds, shuffle=True, random_state=1)
for alpha in alphas:
for k, (train_idxs, test_idxs) in enumerate(kf.split(X)):
X_train, y_train = X[train_idxs, :], y[train_idxs]
X_test, y_test = X[test_idxs, :], y[test_idxs]
params.append((n_estimators, alpha, k, X_train, y_train, X_test, y_test))
return params
def crossval_one_depth(params):
"""
Perform one fold of cross-validation with one tree depth
Input
------
params : The output of make_params
Output
------
td : The tree depth at the current stage
k : The current cross-validation fold (can be used to map back to X and y from params)
test_scores : A list, the model loss at each stage
model : The model trained on the given parameters
"""
(n_estimators, td, k, X_train, y_train, X_test, y_test) = params
test_errors = []
mse_losses = []
model = GradientBoostingRegressor(loss='quantile',
n_estimators=n_estimators,
max_depth=td, learning_rate=0.015,
subsample=0.5,
random_state=128)
model.fit(X_train, y_train)
for j, y_pred in enumerate(model.staged_predict(X_test)):
test_errors.append(model.loss_(y_test, y_pred))
mse_losses.append(mean_squared_error(y_test, y_pred))
return td, k, test_errors, mse_losses
def crossval_one_alpha(params):
"""
Perform one fold of cross-validation with one tree depth
Input
------
params : The output of make_params
Output
------
alpha : The alpha at the current stage
k : The current cross-validation fold (can be used to map back to X and y from params)
test_scores : A list, the model loss at each stage
model : The model trained on the given parameters
"""
(n_estimators, alpha, k, X_train, y_train, X_test, y_test) = params
test_errors = []
mse_losses = []
model = GradientBoostingRegressor(loss='quantile',
n_estimators=n_estimators,
max_depth=5, learning_rate=0.015,
subsample=0.5, alpha=alpha,
random_state=128)
model.fit(X_train, y_train)
for j, y_pred in enumerate(model.staged_predict(X_test)):
test_errors.append(model.loss_(y_test, y_pred))
mse_losses.append(mean_squared_error(y_test, y_pred))
return alpha, k, test_errors, mse_losses
def find_best_depth(cv_depth_result, n_estimators, n_folds, tree_depths):
'''
INPUT
-------
n_estimators = number of estimators choosen
k_folds = number of cv k-folds
tree_depths = list of tree depths
OUTPUT
-------
optimal tree depth value
'''
n_estimators = n_estimators
k_folds = n_folds
tree_depths = tree_depths
n_trees = len(tree_depths)
k_error_list = []
for tree_idx in range(n_trees):
error_arr = np.zeros(n_estimators)
for k in range(k_folds):
idx = k + (k_folds*tree_idx)
#this is picking best test_error change to 3 if you want mse
error_arr += np.array(cv_depth_result[idx][2])
k_error_list.append(min(error_arr/k_folds))
k_error_arr = np.array(k_error_list)
best_depth = tree_depths[np.argmin(k_error_arr)]
return best_depth
def find_best_alpha(cv_alpha_result, n_estimators, n_folds, alphas):
'''
INPUT
-------
n_estimators = number of estimators choosen
k_folds = number of cv k-folds
alphas = list of alphas
OUTPUT
-------
optimal alpha value
'''
n_estimators = n_estimators
k_folds = n_folds
alpha_list = alphas
n_alphas = len(alpha_list)
k_error_list = []
for alpha_idx in range(n_alphas):
error_arr = np.zeros(n_estimators)
for k in range(k_folds):
idx = k + (k_folds*alpha_idx)
#this is picking best test_error change to 3 if you want mse
error_arr += np.array(cv_alpha_result[idx][2])
k_error_list.append(min(error_arr/k_folds))
k_error_arr = np.array(k_error_list)
best_alpha = alpha_list[np.argmin(k_error_arr)]
return best_alpha
def column_list_to_string(list):
column_str = ''
for i, col in enumerate(list):
if i == 0:
column_str += str(col)
else:
column_str += ","+str(col)
return column_str
def update_cv_database(conn, best_depth, route_dir):
cur = conn.cursor()
cur.execute("UPDATE model_params "
"SET best_depth = (%s),"
"c_validated = 'true' "
"WHERE route_dir = (%s)",
(best_depth, route_dir))
conn.commit()
def plot_tree_depth_cv(ax, cv_depth_result, n_estimators,
n_folds, tree_depths):
n_estimators = n_estimators
k_folds = n_folds
tree_depths = tree_depths
n_trees = len(tree_depths)
k_error_list = []
x = np.arange(1,n_estimators+1,1)
for tree_idx in range(n_trees):
error_arr = np.zeros(n_estimators)
for k in range(k_folds):
idx = k + (k_folds*tree_idx)
error_arr += np.array(cv_depth_result[idx][2])
k_error_list.append(min(error_arr/k_folds))
ax.plot(x, (error_arr/k_folds), label="tree depth = {}".format(tree_depths[tree_idx]))
k_error_arr = np.array(k_error_list)
ax.legend(fontsize=15)
ax.set_xlabel("n_estimators", fontsize=15)
ax.set_ylabel("Test Error", fontsize=15)
def plot_alpha_cv(ax, cv_alpha_result, n_estimators, n_folds, alphas):
alpha_list = alphas
n_alphas = len(alpha_list)
k_error_list = []
x = np.arange(1,n_estimators+1,1)
for alpha_idx in range(n_alphas):
error_arr = np.zeros(n_estimators)
for k in range(k_folds):
idx = k + (k_folds*alpha_idx)
error_arr += np.array(cv_alpha_result[idx][2])
k_error_list.append(min(error_arr/k_folds))
ax.plot(x, (error_arr/k_folds),
label="alpha = {}".format(alpha_list[alpha_idx]))
k_error_arr = np.array(k_error_list)
ax.legend(fontsize=15)
ax.set_xlabel("n_estimators", fontsize=15)
ax.set_ylabel("Test Error", fontsize=15)
if __name__ == "__main__":
cross_validate_routes()