-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
225 lines (171 loc) · 7.03 KB
/
model.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
import time,os,re,csv,sys,uuid,joblib
from datetime import date
from collections import defaultdict
import numpy as np
import pandas as pd
from sklearn import svm
from sklearn.model_selection import train_test_split, GridSearchCV
from sklearn.ensemble import RandomForestRegressor
from sklearn.metrics import mean_squared_error
from sklearn.preprocessing import StandardScaler, OneHotEncoder
from sklearn.pipeline import Pipeline
from logger import update_predict_log, update_train_log
from cslib import fetch_ts, engineer_features
## model specific variables (iterate the version and note with each change)
MODEL_DIR = "models"
MODEL_VERSION = 0.1
MODEL_VERSION_NOTE = "supervised learing model for time-series"
def _model_train(df,tag,test=False):
"""
example funtion to train model
The 'test' flag when set to 'True':
(1) subsets the data and serializes a test version
(2) specifies that the use of the 'test' log file
"""
## start timer for runtime
time_start = time.time()
X,y,dates = engineer_features(df)
if test:
n_samples = int(np.round(0.3 * X.shape[0]))
subset_indices = np.random.choice(np.arange(X.shape[0]),n_samples,
replace=False).astype(int)
mask = np.in1d(np.arange(y.size),subset_indices)
y=y[mask]
X=X[mask]
dates=dates[mask]
## Perform a train-test split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25,
shuffle=True, random_state=42)
## train a random forest model
param_grid_rf = {
'rf__criterion': ['squared_error','absolute_error'],
'rf__n_estimators': [10,15,20,25]
}
pipe_rf = Pipeline(steps=[('scaler', StandardScaler()),
('rf', RandomForestRegressor())])
grid = GridSearchCV(pipe_rf, param_grid=param_grid_rf, cv=5, n_jobs=-1)
grid.fit(X_train, y_train)
y_pred = grid.predict(X_test)
eval_rmse = round(np.sqrt(mean_squared_error(y_test,y_pred)))
## retrain using all data
grid.fit(X, y)
model_name = re.sub("\.","_",str(MODEL_VERSION))
if test:
saved_model = os.path.join(MODEL_DIR,
"test-{}-{}.joblib".format(tag,model_name))
print("... saving test version of model: {}".format(saved_model))
else:
saved_model = os.path.join(MODEL_DIR,
"sl-{}-{}.joblib".format(tag,model_name))
print("... saving model: {}".format(saved_model))
joblib.dump(grid,saved_model)
m, s = divmod(time.time()-time_start, 60)
h, m = divmod(m, 60)
runtime = "%03d:%02d:%02d"%(h, m, s)
## update log
update_train_log(tag,(str(dates[0]),str(dates[-1])),{'rmse':eval_rmse},runtime,
MODEL_VERSION, MODEL_VERSION_NOTE,test=True)
def model_train(data_dir,test=False):
"""
funtion to train model given a df
'mode' - can be used to subset data essentially simulating a train
"""
if not os.path.isdir(MODEL_DIR):
os.mkdir(MODEL_DIR)
if test:
print("... test flag on")
print("...... subseting data")
print("...... subseting countries")
## fetch time-series formatted data
ts_data = fetch_ts(data_dir)
## train a different model for each data sets
for country,df in ts_data.items():
if test and country not in ['all','united_kingdom']:
continue
_model_train(df,country,test=test)
def model_load(prefix='sl',data_dir=None,training=True):
"""
example funtion to load model
The prefix allows the loading of different models
"""
# set dir to data/cs-train
# csdir = os.path.join("..","data","cs-train")
if not data_dir:
data_dir = os.path.join("data","cs-train")
models = [f for f in os.listdir(os.path.join(".","models")) if re.search("sl",f)]
if len(models) == 0:
raise Exception("Models with prefix '{}' cannot be found did you train?".format(prefix))
all_models = {}
for model in models:
all_models[re.split("-",model)[1]] = joblib.load(os.path.join(".","models",model))
## load data
ts_data = fetch_ts(data_dir)
all_data = {}
for country, df in ts_data.items():
X,y,dates = engineer_features(df,training=training)
dates = np.array([str(d) for d in dates])
all_data[country] = {"X":X,"y":y,"dates": dates}
return(all_data, all_models)
def model_predict(country,year,month,day,all_models=None,test=False):
"""
example funtion to predict from model
"""
## start timer for runtime
time_start = time.time()
## load model if needed
if not all_models:
print("... no model found, loading model")
all_data,all_models = model_load(training=False)
## input checks
if country not in all_models.keys():
raise Exception("ERROR (model_predict) - model for country '{}' could not be found".format(country))
for d in [year,month,day]:
if re.search("\D",d):
raise Exception("ERROR (model_predict) - invalid year, month or day")
## load data
model = all_models[country]
data = all_data[country]
## check date
target_date = "{}-{}-{}".format(year,str(month).zfill(2),str(day).zfill(2))
print(target_date)
if target_date not in data['dates']:
raise Exception("ERROR (model_predict) - date {} not in range {}-{}".format(target_date,
data['dates'][0],
data['dates'][-1]))
date_indx = np.where(data['dates'] == target_date)[0][0]
query = data['X'].iloc[[date_indx]]
## sainty check
if data['dates'].shape[0] != data['X'].shape[0]:
raise Exception("ERROR (model_predict) - dimensions mismatch")
## make prediction and gather data for log entry
y_pred = model.predict(query)
y_proba = None
if 'predict_proba' in dir(model) and 'probability' in dir(model):
if model.probability == True:
y_proba = model.predict_proba(query)
m, s = divmod(time.time()-time_start, 60)
h, m = divmod(m, 60)
runtime = "%03d:%02d:%02d"%(h, m, s)
## update predict log
update_predict_log(country,y_pred,y_proba,target_date,
runtime, MODEL_VERSION, test=test)
return({'y_pred':y_pred,'y_proba':y_proba})
if __name__ == "__main__":
"""
basic test procedure for model.py
"""
## train the model
print("TRAINING MODELS")
data_dir = os.path.join("..","data","cs-train")
model_train(data_dir,test=True)
## load the model
print("LOADING MODELS")
all_data, all_models = model_load()
print("... models loaded: ",",".join(all_models.keys()))
## test predict
country='all'
year='2018'
month='01'
day='05'
result = model_predict(country,year,month,day)
print(result)