-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_grid_search_pipeline.py
178 lines (123 loc) · 5.47 KB
/
run_grid_search_pipeline.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
from functools import reduce
import os
import numpy as np
import pandas as pd
import feature_engineering_functions as fe
import submission_functions as sm
from sklearn.model_selection import train_test_split
from sklearn.model_selection import GridSearchCV
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from xgboost.sklearn import XGBRegressor
from sklearn.ensemble import RandomForestRegressor
from sklearn.preprocessing import MinMaxScaler, OneHotEncoder, PolynomialFeatures
from statistics import mean
num_scaler = MinMaxScaler()
ohe = OneHotEncoder(sparse=False, handle_unknown='error', drop='first')
poly = PolynomialFeatures(degree=2, include_bias=False)
dir_path = os.path.dirname(os.path.realpath(__file__))
df = pd.read_csv(f'{dir_path}/data/train.csv', index_col=0, parse_dates=True)
target = 'count'
feature_configurations = [
{
'id': 'remove_rush_hour',
'rm': ['is_rush_hour'],
'drop_features': ['casual', 'registered'],
'numeric_values': ['atemp', 'humidity', 'windspeed', 'temp', 'hour', 'year', 'cos_hour', 'time_index'],
'cat_features': ['season', 'weather', 'month', 'day_of_week', 'time_of_day']
},
]
feature_results = []
for feature_config in feature_configurations:
X = df.drop(feature_config['drop_features'], axis=1).drop(target, axis=1)
y = df[target]
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42)
# x_train Alone
X_train = fe.engineer_df(X_train, feature_config['numeric_values'],
feature_config['cat_features'], num_scaler, poly, ohe, True, feature_config['rm'])
# x_test Alone
X_test = fe.engineer_df(X_test, feature_config['numeric_values'],
feature_config['cat_features'], num_scaler, poly, ohe, False, feature_config['rm'])
grid_searches = [
GridSearchCV(RandomForestRegressor(), {
'max_depth': [22,23, ],
'n_estimators': [95, 96, 97],
'n_jobs': [4]
}, cv=5, verbose=3),
GridSearchCV(LinearRegression(), {
'fit_intercept': [False, True],
'normalize': [True],
'n_jobs': [4]
}, cv=5, verbose=3),
GridSearchCV(XGBRegressor(), {
'booster': ['gbtree'],
'nthread':[4],
'max_depth': [5, 6, 7, 8, 9 ,10, 11 ,12]
}, cv=5, verbose=3
),
]
grid_results = []
for grid in grid_searches:
grid.fit(X_train, y_train)
result = {
'best_params': grid.best_params_,
'best_estimator': grid.best_estimator_,
'grid': grid,
'best_score': grid.best_score_,
'test_score': grid.score(X_test, y_test)
}
print(f'\n Grid tested', result)
grid_results.append(result)
print('\n The grid results are:', grid_results)
df_test = pd.read_csv(f'{dir_path}/data/test.csv',
index_col=0, parse_dates=True)
train_df = fe.engineer_df(
X, feature_config['numeric_values'], feature_config['cat_features'], num_scaler, poly, ohe, True, feature_config['rm'])
test_df = fe.engineer_df(df_test.copy(), feature_config['numeric_values'],
feature_config['cat_features'], num_scaler, poly, ohe, False, feature_config['rm'])
y = df[target]
for result in grid_results:
if(result['test_score'] > 0.80):
model = result['grid'].estimator
model.fit(train_df, np.ravel(y))
predictions = model.predict(test_df)
predictions = np.round(predictions, decimals=0)
predictions = np.where(predictions < 0, 0, predictions)
model_name = result['grid'].best_estimator_.__class__.__name__
submission = pd.DataFrame(
{
'datetime': test_df.index.values,
f'{model_name}_count': predictions.astype(int)
}
)
result['submission_df'] = submission
filename = f'{dir_path}/artifacts/bike_sharing_demand_{model_name}_submission.csv'
submission.to_csv(filename, index=False)
feature_results.append(
{
'feature_config': feature_config,
'grid_results': grid_results
}
)
best_feature_result = None
print('\n The feature results are', feature_results)
for feature_result in feature_results:
grid_results = feature_result['grid_results']
scores = list(map(lambda gr: gr['best_score'], grid_results))
average_score = mean(scores)
feature_result['score']= average_score
if best_feature_result is None or best_feature_result.get('score') is None or best_feature_result.get('score') < average_score:
best_feature_result = feature_result
print('\n The Best results are', best_feature_result)
grid_results = best_feature_result['grid_results']
good_enough = filter(lambda res: res.get(
'submission_df') is not None, grid_results)
submissions = list(map(lambda res: res['submission_df'], good_enough))
composed_df: pd.DataFrame = reduce(lambda x, y: x.merge(y), submissions)
composed_df = composed_df.set_index('datetime')
voted_df = composed_df.apply(sm.get_mode_or_mean, axis=1)
final_df = voted_df[['count']]
# This is saved in the same directory as your notebook
filename = f'{dir_path}/artifacts/bike_sharing_demand_composed_submission.csv'
final_df.to_csv(filename, index=True)
print('Saved file: ' + filename)