-
Notifications
You must be signed in to change notification settings - Fork 112
/
Copy pathmodule.py
385 lines (333 loc) · 17.2 KB
/
module.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
# Install packages from jupyter Notebook
# !python -m pip install --user --upgrade pip
# !pip install tqdm
# !conda config --add channels conda-forge
# !conda install --yes hmmlearn
# !pip install hmmlearn
# Auto reload
# %reload_ext autoreload
# %autoreload 2
# System related and data input controls
import os
import glob
from urllib.request import urlopen
from io import BytesIO
from zipfile import ZipFile
os.system('pip install pandas-datareader')
os.system('pip install missingno')
os.system('pip install xgboost')
os.system('pip install lightgbm')
os.system('pip install arch')
# Ignore the warnings
import warnings
warnings.filterwarnings('always')
warnings.filterwarnings('ignore')
# datasets
import pandas_datareader.data as web
from statsmodels import datasets
from sklearn import datasets
# Data manipulation, visualization and useful functions
import numpy as np # vectors and matrices
import pandas as pd # tables and data manipulations
pd.options.display.float_format = '{:,.2f}'.format # output format
pd.options.display.max_rows = 10 # display row numbers
pd.options.display.max_columns = 20 # display column numbers
from patsy import dmatrix
from itertools import product # iterative combinations
from tqdm import tqdm # excution time
import matplotlib.pyplot as plt # plots
import matplotlib.dates as mdates
import matplotlib.mlab as mlab
from matplotlib.ticker import FuncFormatter
from matplotlib.ticker import StrMethodFormatter
from matplotlib.ticker import PercentFormatter
import seaborn as sns # plots
import missingno as msno # plots
# Modeling algorithms
# General(Statistics/Econometrics)
from sklearn import preprocessing
import statsmodels
import statsmodels.api as sm
import statsmodels.tsa.api as smt
import statsmodels.formula.api as smf
from statsmodels.stats.outliers_influence import variance_inflation_factor
from scipy import stats
from scipy.stats import norm
# Regression
from sklearn.linear_model import LinearRegression, Ridge, Lasso, ElasticNet
from sklearn.kernel_ridge import KernelRidge
from sklearn.neighbors import KNeighborsRegressor
from sklearn.svm import SVR
from sklearn.tree import DecisionTreeRegressor
from sklearn.ensemble import RandomForestRegressor, BaggingRegressor, GradientBoostingRegressor, AdaBoostRegressor
from xgboost import XGBRegressor
from lightgbm import LGBMRegressor
# Classification
from sklearn.linear_model import LogisticRegression
from sklearn.neighbors import KNeighborsClassifier
from sklearn.svm import LinearSVC, SVC
from sklearn.tree import DecisionTreeClassifier
from sklearn.naive_bayes import GaussianNB
from sklearn.ensemble import RandomForestClassifier, GradientBoostingClassifier
# Time series
from statsmodels.tsa.api import SimpleExpSmoothing, Holt, ExponentialSmoothing
import arch
## for Python 3.6: Anaconda3-5.2.0-Windows-x86_64.exe
## from pyramid.arima import auto_arima
# Model selection
from sklearn.model_selection import train_test_split,cross_validate
from sklearn.model_selection import KFold
from sklearn.model_selection import GridSearchCV
# Evaluation metrics
# for regression
from sklearn.metrics import mean_squared_log_error, mean_squared_error, r2_score, mean_absolute_error
# for classification
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score
### Feature engineering of default
def non_feature_engineering(raw):
if 'datetime' in raw.columns:
raw['datetime'] = pd.to_datetime(raw['datetime'])
raw['DateTime'] = pd.to_datetime(raw['datetime'])
if raw.index.dtype == 'int64':
raw.set_index('DateTime', inplace=True)
# bring back
# if raw.index.dtype != 'int64':
# raw.reset_index(drop=False, inplace=True)
raw = raw.asfreq('H', method='ffill')
raw_nfe = raw.copy()
return raw_nfe
# raw_rd = non_feature_engineering(raw_all)
### Feature engineering of all
def feature_engineering(raw):
if 'datetime' in raw.columns:
raw['datetime'] = pd.to_datetime(raw['datetime'])
raw['DateTime'] = pd.to_datetime(raw['datetime'])
if raw.index.dtype == 'int64':
raw.set_index('DateTime', inplace=True)
raw = raw.asfreq('H', method='ffill')
result = sm.tsa.seasonal_decompose(raw['count'], model='additive')
Y_trend = pd.DataFrame(result.trend)
Y_trend.fillna(method='ffill', inplace=True)
Y_trend.fillna(method='bfill', inplace=True)
Y_trend.columns = ['count_trend']
Y_seasonal = pd.DataFrame(result.seasonal)
Y_seasonal.fillna(method='ffill', inplace=True)
Y_seasonal.fillna(method='bfill', inplace=True)
Y_seasonal.columns = ['count_seasonal']
pd.concat([raw, Y_trend, Y_seasonal], axis=1).isnull().sum()
if 'count_trend' not in raw.columns:
if 'count_seasonal' not in raw.columns:
raw = pd.concat([raw, Y_trend, Y_seasonal], axis=1)
Y_count_Day = raw[['count']].rolling(24).mean()
Y_count_Day.fillna(method='ffill', inplace=True)
Y_count_Day.fillna(method='bfill', inplace=True)
Y_count_Day.columns = ['count_Day']
Y_count_Week = raw[['count']].rolling(24*7).mean()
Y_count_Week.fillna(method='ffill', inplace=True)
Y_count_Week.fillna(method='bfill', inplace=True)
Y_count_Week.columns = ['count_Week']
if 'count_Day' not in raw.columns:
raw = pd.concat([raw, Y_count_Day], axis=1)
if 'count_Week' not in raw.columns:
raw = pd.concat([raw, Y_count_Week], axis=1)
Y_diff = raw[['count']].diff()
Y_diff.fillna(method='ffill', inplace=True)
Y_diff.fillna(method='bfill', inplace=True)
Y_diff.columns = ['count_diff']
if 'count_diff' not in raw.columns:
raw = pd.concat([raw, Y_diff], axis=1)
raw['temp_group'] = pd.cut(raw['temp'], 10)
raw['Year'] = raw.datetime.dt.year
raw['Quater'] = raw.datetime.dt.quarter
raw['Quater_ver2'] = raw['Quater'] + (raw.Year - raw.Year.min()) * 4
raw['Month'] = raw.datetime.dt.month
raw['Day'] = raw.datetime.dt.day
raw['Hour'] = raw.datetime.dt.hour
raw['DayofWeek'] = raw.datetime.dt.dayofweek
raw['count_lag1'] = raw['count'].shift(1)
raw['count_lag2'] = raw['count'].shift(2)
raw['count_lag1'].fillna(method='bfill', inplace=True)
raw['count_lag2'].fillna(method='bfill', inplace=True)
if 'Quater' in raw.columns:
if 'Quater_Dummy' not in ['_'.join(col.split('_')[:2]) for col in raw.columns]:
raw = pd.concat([raw, pd.get_dummies(raw['Quater'],
prefix='Quater_Dummy', drop_first=True)], axis=1)
del raw['Quater']
raw_fe = raw.copy()
return raw_fe
# raw_fe = feature_engineering(raw_all)
### duplicate previous year values to next one
def feature_engineering_year_duplicated(raw, target):
raw_fe = raw.copy()
for col in target:
raw_fe.loc['2012-01-01':'2012-02-28', col] = raw.loc['2011-01-01':'2011-02-28', col].values
raw_fe.loc['2012-03-01':'2012-12-31', col] = raw.loc['2011-03-01':'2011-12-31', col].values
step = (raw.loc['2011-03-01 00:00:00', col] - raw.loc['2011-02-28 23:00:00', col])/25
step_value = np.arange(raw.loc['2011-02-28 23:00:00', col]+step, raw.loc['2011-03-01 00:00:00', col], step)
step_value = step_value[:24]
raw_fe.loc['2012-02-29', col] = step_value
return raw_fe
# target = ['count_trend', 'count_seasonal', 'count_Day', 'count_Week', 'count_diff']
# raw_fe = feature_engineering_year_duplicated(raw_fe, target)
### modify lagged values of X_test
def feature_engineering_lag_modified(Y_test, X_test, target):
X_test_lm = X_test.copy()
for col in target:
X_test_lm[col] = Y_test.shift(1).values
X_test_lm[col].fillna(method='bfill', inplace=True)
X_test_lm[col] = Y_test.shift(2).values
X_test_lm[col].fillna(method='bfill', inplace=True)
return X_test_lm
# target = ['count_lag1', 'count_lag2']
# X_test_fe = feature_engineering_lag_modified(Y_test_fe, X_test_fe, target)
### Data split of cross sectional
def datasplit_cs(raw, Y_colname, X_colname, test_size, random_seed=123):
X_train, X_test, Y_train, Y_test = train_test_split(raw[X_colname], raw[Y_colname], test_size=test_size, random_state=random_seed)
print('X_train:', X_train.shape, 'Y_train:', Y_train.shape)
print('X_test:', X_test.shape, 'Y_test:', Y_test.shape)
return X_train, X_test, Y_train, Y_test
# X_train, X_test, Y_train, Y_test = datasplit_cs(raw_fe, Y_colname, X_colname, 0.2)
### Data split of time series
def datasplit_ts(raw, Y_colname, X_colname, criteria):
raw_train = raw.loc[raw.index < criteria,:]
raw_test = raw.loc[raw.index >= criteria,:]
Y_train = raw_train[Y_colname]
X_train = raw_train[X_colname]
Y_test = raw_test[Y_colname]
X_test = raw_test[X_colname]
print('Train_size:', raw_train.shape, 'Test_size:', raw_test.shape)
print('X_train:', X_train.shape, 'Y_train:', Y_train.shape)
print('X_test:', X_test.shape, 'Y_test:', Y_test.shape)
return X_train, X_test, Y_train, Y_test
# X_train, X_test, Y_train, Y_test = datasplit_ts(raw_fe, Y_colname, X_colname, '2012-07-01')
### scaling of X_train and X_test by X_train_scaler
def feature_engineering_scaling(scaler, X_train, X_test):
# preprocessing.MinMaxScaler()
# preprocessing.StandardScaler()
# preprocessing.RobustScaler()
# preprocessing.Normalizer()
scaler = scaler
scaler_fit = scaler.fit(X_train)
X_train_scaling = pd.DataFrame(scaler_fit.transform(X_train),
index=X_train.index, columns=X_train.columns)
X_test_scaling = pd.DataFrame(scaler_fit.transform(X_test),
index=X_test.index, columns=X_test.columns)
return X_train_scaling, X_test_scaling
# X_train_feRS, X_test_feRS = feature_engineering_scaling(preprocessing.Normalizer(), X_train_feR, X_test_feR)
### extract non-multicollinearity variables by VIF
def feature_engineering_XbyVIF(X_train, num_variables):
vif = pd.DataFrame()
vif['VIF_Factor'] = [variance_inflation_factor(X_train.values, i)
for i in range(X_train.shape[1])]
vif['Feature'] = X_train.columns
X_colname_vif = vif.sort_values(by='VIF_Factor', ascending=True)['Feature'][:num_variables].values
return X_colname_vif
# X_colname_vif = feature_engineering_XbyVIF(X_train_femm, 10)
# X_colname_vif
### Evaluation of 1 pair of set
def evaluation(Y_real, Y_pred, graph_on=False):
loss_length = len(Y_real.values.flatten()) - len(Y_pred)
if loss_length != 0:
Y_real = Y_real[loss_length:]
if graph_on == True:
pd.concat([Y_real, pd.DataFrame(Y_pred, index=Y_real.index, columns=['prediction'])], axis=1).plot(kind='line', figsize=(20,6),
xlim=(Y_real.index.min(),Y_real.index.max()),
linewidth=3, fontsize=20)
plt.title('Time Series of Target', fontsize=20)
plt.xlabel('Index', fontsize=15)
plt.ylabel('Target Value', fontsize=15)
MAE = abs(Y_real.values.flatten() - Y_pred).mean()
MSE = ((Y_real.values.flatten() - Y_pred)**2).mean()
MAPE = (abs(Y_real.values.flatten() - Y_pred)/Y_real.values.flatten()*100).mean()
Score = pd.DataFrame([MAE, MSE, MAPE], index=['MAE', 'MSE', 'MAPE'], columns=['Score']).T
Residual = pd.DataFrame(Y_real.values.flatten() - Y_pred, index=Y_real.index, columns=['Error'])
return Score, Residual
# Score_tr, Residual_tr = evaluation(Y_train, pred_tr_reg1, graph_on=True)
### Evaluation of train/test pairs
def evaluation_trte(Y_real_tr, Y_pred_tr, Y_real_te, Y_pred_te, graph_on=False):
Score_tr, Residual_tr = evaluation(Y_real_tr, Y_pred_tr, graph_on=graph_on)
Score_te, Residual_te = evaluation(Y_real_te, Y_pred_te, graph_on=graph_on)
Score_trte = pd.concat([Score_tr, Score_te], axis=0)
Score_trte.index = ['Train', 'Test']
return Score_trte, Residual_tr, Residual_te
# Score_reg1, Resid_tr_reg1, Resid_te_reg1 = evaluation_trte(Y_train, pred_tr_reg1, Y_test, pred_te_reg1, graph_on=True)
### Error analysis
def stationarity_adf_test(Y_Data, Target_name):
if len(Target_name) == 0:
Stationarity_adf = pd.Series(sm.tsa.stattools.adfuller(Y_Data)[0:4],
index=['Test Statistics', 'p-value', 'Used Lag', 'Used Observations'])
for key, value in sm.tsa.stattools.adfuller(Y_Data)[4].items():
Stationarity_adf['Critical Value(%s)'%key] = value
Stationarity_adf['Maximum Information Criteria'] = sm.tsa.stattools.adfuller(Y_Data)[5]
Stationarity_adf = pd.DataFrame(Stationarity_adf, columns=['Stationarity_adf'])
else:
Stationarity_adf = pd.Series(sm.tsa.stattools.adfuller(Y_Data[Target_name])[0:4],
index=['Test Statistics', 'p-value', 'Used Lag', 'Used Observations'])
for key, value in sm.tsa.stattools.adfuller(Y_Data[Target_name])[4].items():
Stationarity_adf['Critical Value(%s)'%key] = value
Stationarity_adf['Maximum Information Criteria'] = sm.tsa.stattools.adfuller(Y_Data[Target_name])[5]
Stationarity_adf = pd.DataFrame(Stationarity_adf, columns=['Stationarity_adf'])
return Stationarity_adf
def stationarity_kpss_test(Y_Data, Target_name):
if len(Target_name) == 0:
Stationarity_kpss = pd.Series(sm.tsa.stattools.kpss(Y_Data)[0:3],
index=['Test Statistics', 'p-value', 'Used Lag'])
for key, value in sm.tsa.stattools.kpss(Y_Data)[3].items():
Stationarity_kpss['Critical Value(%s)'%key] = value
Stationarity_kpss = pd.DataFrame(Stationarity_kpss, columns=['Stationarity_kpss'])
else:
Stationarity_kpss = pd.Series(sm.tsa.stattools.kpss(Y_Data[Target_name])[0:3],
index=['Test Statistics', 'p-value', 'Used Lag'])
for key, value in sm.tsa.stattools.kpss(Y_Data[Target_name])[3].items():
Stationarity_kpss['Critical Value(%s)'%key] = value
Stationarity_kpss = pd.DataFrame(Stationarity_kpss, columns=['Stationarity_kpss'])
return Stationarity_kpss
def error_analysis(Y_Data, Target_name, X_Data, graph_on=False):
for x in Target_name:
Target_name = x
X_Data = X_Data.loc[Y_Data.index]
if graph_on == True:
##### Error Analysis(Plot)
Y_Data['RowNum'] = Y_Data.reset_index().index
# Stationarity(Trend) Analysis
sns.set(palette="muted", color_codes=True, font_scale=2)
sns.lmplot(x='RowNum', y=Target_name, data=Y_Data, fit_reg='True', size=5.2, aspect=2, ci=99, sharey=True)
del Y_Data['RowNum']
# Normal Distribution Analysis
figure, axes = plt.subplots(figsize=(12,8))
sns.distplot(Y_Data[Target_name], norm_hist='True', fit=stats.norm, ax=axes)
# Lag Analysis
length = int(len(Y_Data[Target_name])/10)
figure, axes = plt.subplots(1, 4, figsize=(12,3))
pd.plotting.lag_plot(Y_Data[Target_name], lag=1, ax=axes[0])
pd.plotting.lag_plot(Y_Data[Target_name], lag=5, ax=axes[1])
pd.plotting.lag_plot(Y_Data[Target_name], lag=10, ax=axes[2])
pd.plotting.lag_plot(Y_Data[Target_name], lag=50, ax=axes[3])
# Autocorrelation Analysis
figure, axes = plt.subplots(2,1,figsize=(12,5))
sm.tsa.graphics.plot_acf(Y_Data[Target_name], lags=100, use_vlines=True, ax=axes[0])
sm.tsa.graphics.plot_pacf(Y_Data[Target_name], lags=100, use_vlines=True, ax=axes[1])
##### Error Analysis(Statistics)
# Checking Stationarity
# Null Hypothesis: The Time-series is non-stationalry
Stationarity_adf = stationarity_adf_test(Y_Data, Target_name)
Stationarity_kpss = stationarity_kpss_test(Y_Data, Target_name)
# Checking of Normality
# Null Hypothesis: The residuals are normally distributed
Normality = pd.DataFrame([stats.shapiro(Y_Data[Target_name])],
index=['Normality'], columns=['Test Statistics', 'p-value']).T
# Checking for Autocorrelation
# Null Hypothesis: Autocorrelation is absent
Autocorrelation = pd.concat([pd.DataFrame(sm.stats.diagnostic.acorr_ljungbox(Y_Data[Target_name], lags=[1,5,10,50])[0], columns=['Test Statistics']),
pd.DataFrame(sm.stats.diagnostic.acorr_ljungbox(Y_Data[Target_name], lags=[1,5,10,50])[1], columns=['p-value'])], axis=1).T
Autocorrelation.columns = ['Autocorr(lag1)', 'Autocorr(lag5)', 'Autocorr(lag10)', 'Autocorr(lag50)']
# Checking Heteroscedasticity
# Null Hypothesis: Error terms are homoscedastic
Heteroscedasticity = pd.DataFrame([sm.stats.diagnostic.het_goldfeldquandt(Y_Data[Target_name], X_Data.values, alternative='two-sided')],
index=['Heteroscedasticity'], columns=['Test Statistics', 'p-value', 'Alternative']).T
Score = pd.concat([Stationarity_adf, Stationarity_kpss, Normality, Autocorrelation, Heteroscedasticity], join='outer', axis=1)
index_new = ['Test Statistics', 'p-value', 'Alternative', 'Used Lag', 'Used Observations',
'Critical Value(1%)', 'Critical Value(5%)', 'Critical Value(10%)', 'Maximum Information Criteria']
Score.reindex(index_new)
return Score
# error_analysis(Resid_tr_reg1[1:], ['Error'], X_train, graph_on=True)