-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtree.py
455 lines (373 loc) · 15.7 KB
/
tree.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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
"""
Tree based models.
**Available routines:**
- class ``RandomForest``: Builds Random Forest model using cross validation.
- class ``XGBoost``: Builds XGBoost model using cross validation.
Credits
-------
::
Authors:
- Diptesh
- Madhu
Date: Jan 15, 2022
"""
# pylint: disable=invalid-name
# pylint: disable=W0511,R0902,R0903,R0913,C0413
from typing import List, Dict, Any
import re
import sys
from inspect import getsourcefile
from os.path import abspath
import pandas as pd
import numpy as np
import sklearn.ensemble as rf
import xgboost as xgb
from scipy.stats import norm
from sklearn.metrics import classification_report
from sklearn.model_selection import RandomizedSearchCV
from sklearn.model_selection import TimeSeriesSplit as ts_split
from statsmodels.tsa.stattools import pacf
from statsmodels.tsa.seasonal import seasonal_decompose
path = abspath(getsourcefile(lambda: 0))
path = re.sub(r"(.+\/)(.+.py)", "\\1", path)
sys.path.insert(0, path)
import metrics # noqa: F841
class Tree():
"""Parent class for tree based models."""
def __init__(self,
df: pd.DataFrame,
y_var: str,
x_var: List[str] = None,
method: str = "regression",
k_fold: int = 5,
param: Dict = None,
ts_param: Dict = None):
"""Initialize variables."""
self.y_var = y_var
self.x_var = x_var
self.df = df
self.method = method
if self.method in ("classify", "regression"):
self.df = self.df.reset_index(drop=True)
self.k_fold = k_fold
self.seed = 1
if self.method == "timeseries":
self.ts_param = ts_param
if self.ts_param is None:
self.ts_param = {}
self.ts_param["threshold"] = 0.05
self.ts_param["max_lag"] = 20
self.ts_param["ts_x_var"] = None
self.ts_param["ts_lag_var"] = None
self._ts_data_transform()
self.k_fold = ts_split(n_splits=self.k_fold)\
.split(X=self.ts_df[self.y_var])
self.model = None
self.model_summary = None
self.param = param
self.best_params_ = self._fit()
self._compute_metrics()
def _compute_metrics(self):
"""Compute commonly used metrics to evaluate the model."""
if self.method in ("classify", "regression"):
y = self.df.loc[:, self.y_var].values.tolist()
y_hat = list(self.model.predict(self.df[self.x_var]))
elif self.method == "timeseries":
y = self.ts_df.loc[:, self.y_var].values.tolist()
y_hat = list(self.model.predict(
self.ts_df[self.ts_param["ts_x_var"]]))
if self.method in ("regression", "timeseries"):
model_summary = {"rsq": np.round(metrics.rsq(y, y_hat), 3),
"mae": np.round(metrics.mae(y, y_hat), 3),
"mape": np.round(metrics.mape(y, y_hat), 3),
"rmse": np.round(metrics.rmse(y, y_hat), 3)}
model_summary["mse"] = np.round(model_summary["rmse"] ** 2, 3)
if self.method == "classify":
class_report = classification_report(y,
y_hat,
output_dict=True,
zero_division=0)
model_summary = class_report["weighted avg"]
model_summary["accuracy"] = class_report["accuracy"]
model_summary = {key: round(model_summary[key], 3)
for key in model_summary}
self.model_summary = model_summary
def _ts_data_transform(self):
"""Transform input data with significant lag variables."""
# Determine seasonality and return seaonal lag
decomposition = seasonal_decompose(self.df[self.y_var],
model="additive")
_seasonal = decomposition.seasonal
freq = _seasonal.value_counts()
self.ts_param["seasonality"] = \
int(np.ceil(len(self.df) / freq.iloc[0]))
# Determine significant lags
df = self.df.copy(deep=True)
df = df[self.y_var]
df = pd.DataFrame({"lag": list(range(self.ts_param["max_lag"]+1)),
"pacf": pacf(df,
nlags=self.ts_param["max_lag"],
method='ols')})
df["thres_val"] = \
(np.round(norm.ppf(1 - (self.ts_param["threshold"] / 2)), 2)
/ (len(self.df) ** 0.5))
df["pacf_sig"] = np.where((df['pacf'] >= df["thres_val"])
| (df['pacf'] <= - df["thres_val"]),
1, 0)
df = df.where(df['pacf_sig'] > 0)
df = df.dropna()
self.ts_param["ts_lag_var"] = df['lag'].astype(int).to_list()
self.ts_param["ts_lag_var"].append(self.ts_param["seasonality"])
self.ts_param["ts_lag_var"] = \
[x for x in self.ts_param["ts_lag_var"] if x != 0]
self.ts_param["ts_lag_var"] = list(set(self.ts_param["ts_lag_var"]))
self.ts_df = pd.DataFrame(self.df.loc[:, self.y_var])
# TODO: Add integration test
if len(self.ts_param["ts_lag_var"]) == 0: # pragma: no cover
self.ts_param["ts_lag_var"] = [1]
for lag in self.ts_param["ts_lag_var"]:
self.ts_df.loc[:, "lag_" + str(lag)] = \
self.ts_df[self.y_var].shift(lag)
if self.x_var is not None:
self.ts_df = self.ts_df.join(self.df[self.x_var])
self.ts_df = self.ts_df.dropna()
self.ts_param["ts_x_var"] = list(self.ts_df.columns)
self.ts_param["ts_x_var"].remove(self.y_var)
def _fit(self) -> Dict[str, Any]: # pragma: no cover
"""Fit model."""
return self.best_params_
def _ts_predict(self,
x_predict: pd.DataFrame = None,
n_interval: int = 1) -> pd.DataFrame:
"""Predict values for time series."""
if self.x_var is None:
df_op = [-1.0] * n_interval
df_op = pd.DataFrame(df_op)
df_op.columns = [self.y_var]
else:
df_op = x_predict.copy(deep=True)
df_op[self.y_var] = -1.0
lst_lag_val = self.df[self.y_var].tolist()
for i, _ in enumerate(df_op):
df_pred_x = pd.DataFrame(df_op.iloc[i]).T
for j, _ in enumerate(self.ts_param["ts_lag_var"]):
df_pred_x["lag_" + str(self.ts_param["ts_lag_var"][j])] \
= lst_lag_val[len(lst_lag_val)
- self.ts_param["ts_lag_var"][j]]
df_pred_x = pd.DataFrame(df_pred_x)
y_hat = self.model.predict(df_pred_x[self.ts_param["ts_x_var"]])
df_op.iloc[i, df_op.columns.get_loc(self.y_var)] = y_hat[0]
lst_lag_val.append(y_hat[0])
return df_op
def predict(self,
x_predict: pd.DataFrame = None,
n_interval: int = 1) -> pd.DataFrame:
"""Predict values."""
if self.method == "timeseries":
df_op = self._ts_predict(x_predict, n_interval)
else:
df_op = x_predict.copy(deep=True)
y_hat = self.model.predict(x_predict)
df_op.insert(loc=0, column=self.y_var, value=y_hat)
return df_op
class RandomForest(Tree):
"""Random forest module.
Objective:
- Build
`Random forest <https://en.wikipedia.org/wiki/Random_forest>`_
model and determine optimal k
Parameters
----------
df : pandas.DataFrame
Pandas dataframe containing the `y_var` and `x_var`
y_var : str
Dependant variable
x_var : List[str]
Independant variables
method : str, optional
Can be either `classify`, `timeseries` or `regression`
(the default is regression)
k_fold : int, optional
Number of cross validations folds (the default is 5)
threshold : float, optional
Threshold to identify significant lag values (the default is 0.05)
param : dict, optional
Random forest parameters (the default is None).
In case of None, the parameters will default to::
bootstrap: [True]
max_depth: [1, len(x_var)]
n_estimators: [1000]
max_features: ["sqrt", "auto"]
min_samples_leaf: [2, 5]
ts_param : dict, optional
Random forest parameters (the default is None).
In case of None, the parameters will default to::
threshold: 0.05
max_lag: 20
ts_x_var: None
ts_lag_var: None
seasonality: None
Returns
-------
model : object
Final optimal model.
best_params_ : Dict
Best parameters amongst the given parameters.
model_summary : Dict
Model summary containing key metrics like R-squared, RMSE, MSE, MAE,
MAPE for regression and Accuracy, Precision, Recall, F1 score for
classification.
Methods
-------
predict
Example
-------
>>> mod = RandomForest(df=df_ip, y_var="y", x_var=["x1", "x2", "x3"])
>>> df_op = mod.predict(x_predict)
"""
def _fit(self) -> Dict[str, Any]:
"""Fit RandomForest model."""
if self.param is None:
self.param = {"bootstrap": [True],
"n_estimators": [100]}
if self.method == "classify":
self.param["max_features"] = ["sqrt"]
self.param["min_samples_leaf"] = [2]
self.param["max_depth"] = list(range(1, len(self.x_var)))
elif self.method == "regression":
self.param["max_features"] \
= [int(np.ceil(len(self.x_var) / 3))]
self.param["min_samples_leaf"] = [5]
self.param["max_depth"] \
= list(range(1, len(self.x_var)))
elif self.method == "timeseries":
self.param["max_features"] \
= [int(np.ceil(len(self.ts_param["ts_x_var"]) / 3))]
self.param["min_samples_leaf"] = [5]
self.param["max_depth"] = \
list(range(1, len(self.ts_param["ts_x_var"])))
if self.method == "classify":
tmp_model = rf.RandomForestClassifier(oob_score=True,
random_state=self.seed)
elif self.method in ("regression", "timeseries"):
tmp_model = rf.RandomForestRegressor(oob_score=True,
random_state=self.seed)
gs = RandomizedSearchCV(estimator=tmp_model,
param_distributions=self.param,
n_jobs=-1,
verbose=0,
refit=True,
n_iter=3,
return_train_score=True,
cv=self.k_fold)
if self.method == "timeseries":
gs_op = gs.fit(self.ts_df[self.ts_param["ts_x_var"]],
self.ts_df[self.y_var])
elif self.method in ("regression", "classify"):
gs_op = gs.fit(self.df[self.x_var],
self.df[self.y_var])
self.model = gs_op
return gs_op.best_params_
class XGBoost(Tree):
"""XGBoost module.
Objective:
- Build
`XGBoost <https://en.wikipedia.org/wiki/XGBoost>`_
model and determine optimal k
Parameters
----------
df : pandas.DataFrame
Pandas dataframe containing the `y_var` and `x_var`
y_var : str
Dependant variable
x_var : List[str]
Independant variables
method : str, optional
Can be either `classify`, `timeseries` or `regression`
(the default is regression)
k_fold : int, optional
Number of cross validations folds (the default is 5)
threshold : float, optional
Threshold to identify significant lag values (the default is 0.05)
param : dict, optional
XGBoost parameters (the default is None).
In case of None, the parameters will default to::
n_estimators: [100]
learning_rate: [0.01, 0.1, 0.2, 0.3]
subsample: [0.5, 0.75, 1.0]
colsample_bytree: [0.5, 1.0]
min_child_weight: [0.5, 1.0, 3.0]
max_depth: [int(len(self.x_var) * 0.8]
objective: ["reg:squarederror", "binary:logistic"]
ts_param : dict, optional
Random forest time series parameters (the default is None).
In case of None, the parameters will default to::
threshold: 0.05
max_lag: 20
ts_x_var: None
ts_lag_var: None
seasonlity: None
Returns
-------
model : object
Final optimal model.
best_params_ : Dict
Best parameters amongst the given parameters.
model_summary : Dict
Model summary containing key metrics like R-squared, RMSE, MSE, MAE,
MAPE for regression and Accuracy, Precision, Recall, F1 score for
classification.
Methods
-------
predict
Example
-------
>>> mod = XGBoost(df=df_ip, y_var="y", x_var=["x1", "x2", "x3"])
>>> df_op = mod.predict(x_predict)
"""
def _fit(self) -> Dict[str, Any]:
"""Fit XGBoost model."""
if self.param is None:
self.param = {"n_estimators": [100],
"learning_rate": [0.01, 0.1, 0.2, 0.3],
"subsample": [0.5, 0.75, 1.0],
"colsample_bytree": [0.5, 1.0],
"min_child_weight": [0.5, 1.0, 3.0]}
if self.method == "timeseries":
self.param["max_depth"] = \
[int(len(self.ts_param["ts_x_var"]) * 0.8)]
elif self.method in ("regression", "classify"):
self.param["max_depth"] = [int(len(self.x_var) * 0.8)]
if self.method == "classify":
self.param["objective"] = ["binary:logistic"]
elif self.method in ("regression", "timeseries"):
self.param["objective"] = ["reg:squarederror"]
if self.method == "classify":
tmp_model = xgb.XGBClassifier(n_jobs=1,
verbosity=0,
silent=True,
random_state=self.seed,
seed=self.seed)
elif self.method in ("regression", "timeseries"):
tmp_model = xgb.XGBRegressor(n_jobs=1,
verbosity=0,
silent=True,
random_state=self.seed,
seed=self.seed)
gs = RandomizedSearchCV(estimator=tmp_model,
param_distributions=self.param,
n_jobs=-1,
verbose=0,
refit=True,
n_iter=10,
return_train_score=True,
cv=self.k_fold,
random_state=self.seed)
if self.method == "timeseries":
gs_op = gs.fit(self.ts_df[self.ts_param["ts_x_var"]],
self.ts_df[self.y_var])
elif self.method in ("regression", "classify"):
gs_op = gs.fit(self.df[self.x_var],
self.df[self.y_var])
self.model = gs_op
return gs_op.best_params_