-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path2_model.py
More file actions
166 lines (142 loc) · 6.1 KB
/
2_model.py
File metadata and controls
166 lines (142 loc) · 6.1 KB
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
import pandas as pd
import numpy as np
import subprocess
import sys
import pathlib
import time
import os
start_time = time.time()
def checkForPackages(package):
reqs = subprocess.check_output([sys.executable, '-m', 'pip', 'freeze'])
installed_packages = [r.decode().split('==')[0].lower() for r in reqs.split()]
if not package in installed_packages:
print("Installing", package, "...")
subprocess.check_call([sys.executable, "-m", "pip", "install", package])
modelpackages = {'orbit': 'orbit-ml', 'pmdarima': 'pmdarima'}
for package in modelpackages:
try:
import package
except:
checkForPackages(modelpackages[package])
from orbit.models.dlt import ETSFull, DLTMAP, DLTFull
from orbit.models.lgt import LGTMAP, LGTFull, LGTAggregated
from orbit.diagnostics.plot import plot_predicted_data, plot_predicted_components
from pmdarima.arima import auto_arima
## Instantiate Variables
dirPath = str(pathlib.Path().resolve())
dataPath = dirPath + "/data/"
rawDataPath = dataPath + 'train.csv.zip'
imgPath = dirPath + "/img/"
date_col = 'date'
response_col = 'sales'
## Generate Functions
def preprocessData(df, date_col, response_col):
# Convert from daily data into monthly data
df[date_col] = df[date_col].apply(lambda x: str(x)[:-3])
df = df.groupby(date_col)[response_col].sum().reset_index()
df[date_col] = pd.to_datetime(df[date_col], format='%Y/%m')
return df.sort_values(by=date_col)
def trainTestestSplit(df, test_size=12):
train = df[:-test_size]
test = df[-test_size:]
return train, test
def mape(actual, pred):
actual, pred = np.array(actual), np.array(pred)
return np.mean(np.abs((actual - pred) / actual)) * 100
def runTheModel(df_train, df_test, model, modName, date_col, response_col, decompose=False, imgPath=imgPath):
if not 'ARIMA' in modName:
model.fit(df_train)
if not decompose:
pred = model.predict(df_test[[date_col]]).prediction.unique()
else:
pred = model.predict(df_train, decompose=True)
vis = plot_predicted_components(pred, date_col, is_visible=False,
plot_components=['prediction', 'trend', 'seasonality'],
path=imgPath)
return vis, pred
else:
pred = model.predict(df_test.shape[0])
error = mape(df_test[response_col], pred)
prediction_df = pd.DataFrame.from_dict({'Date': df_test[date_col], 'Actual': df_test[response_col], 'Prediction': pred, 'MAPE': error, 'Model': modName})
# print(prediction_df.head())
return prediction_df
def getModelDict(train):
modelDict = {'ETSFull': ETSFull(
response_col=response_col,
date_col=date_col,
seasonality=12,
seed=8888
),
'DLTMAP_Linear': DLTMAP(
response_col=response_col,
date_col=date_col,
seasonality=12,
seed=8888
),
'DLTMAP_LogLin': DLTMAP(
response_col=response_col,
date_col=date_col,
seasonality=12,
seed=8888,
global_trend_option='loglinear'
),
'DLTMAP_Logistic': DLTMAP(
response_col=response_col,
date_col=date_col,
seasonality=12,
seed=8888,
global_trend_option='logistic'
),
'LGTMAP': LGTMAP(
response_col=response_col,
date_col=date_col,
seasonality=12,
seed=8888
), # Commented out because the results were too poor
'LGTFull': LGTFull(
response_col=response_col,
date_col=date_col,
seasonality=12,
seed=8888,
),
'LGTAggregation': LGTAggregated(
response_col=response_col,
date_col=date_col,
seasonality=12,
seed=8888,
),
'DLTFull': DLTFull(
response_col=response_col,
date_col=date_col,
seasonality=12,
seed=8888,
num_warmup=5000,
),
'ARIMA': auto_arima(train[[response_col]],
m=12,
seasonal=True
) }
return modelDict
## Load and Process Data
if __name__ == '__main__':
df = pd.read_csv(rawDataPath)
df = preprocessData(df, date_col, response_col)
train, test = trainTestestSplit(df)
models = getModelDict(train, test)
# Run the models
predictions = []
for mod in models:
print('running', mod)
predictions.append(runTheModel(train, test, models[mod], mod, date_col, response_col))
# Create dataframe of all results
full_df = pd.concat(predictions)
full_df['Rank'] = full_df.MAPE.rank(method='dense')
# Create dataframe of model rankings
ranking_df = full_df[['Model', 'Rank', 'MAPE']].drop_duplicates().sort_values(by='Rank')
# Write data
full_df.to_csv(dataPath + 'model_output.csv', index=False)
ranking_df.to_csv(dataPath + 'ranking.csv', index=False)
df.to_csv(dataPath + 'procssed_data.csv', index=False)
# train.to_csv(dirpath + "train.csv", index=False)
# test.to_csv(dirpath + 'test.csv', index=False)
print("Application took:", time.time() - start_time, "to run.")