-
Notifications
You must be signed in to change notification settings - Fork 0
/
demand_forecasting.py
365 lines (252 loc) · 10.7 KB
/
demand_forecasting.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
#####################################################
# Demand Forecasting
#####################################################
# Store Item Demand Forecasting Challenge
# https://www.kaggle.com/c/demand-forecasting-kernels-only
import time
import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
import seaborn as sns
import lightgbm as lgb
import warnings
pd.set_option('display.max_columns', None)
pd.set_option('display.width', 500)
warnings.filterwarnings('ignore')
def check_df(dataframe, head=5):
print("##################### Shape #####################")
print(dataframe.shape)
print("##################### Types #####################")
print(dataframe.dtypes)
print("##################### Head #####################")
print(dataframe.head(head))
print("##################### Tail #####################")
print(dataframe.tail(head))
print("##################### NA #####################")
print(dataframe.isnull().sum())
print("##################### Quantiles #####################")
print(dataframe.quantile([0, 0.05, 0.50, 0.95, 0.99, 1]).T)
########################
# Loading the data
########################
train = pd.read_csv('datasets/demand_forecasting/train.csv', parse_dates=['date'])
test = pd.read_csv('datasets/demand_forecasting/test.csv', parse_dates=['date'])
sample_sub = pd.read_csv('datasets/demand_forecasting/sample_submission.csv')
df = pd.concat([train, test], sort=False)
#####################################################
# EDA
#####################################################
df["date"].min(), df["date"].max()
check_df(df)
df[["store"]].nunique()
df[["item"]].nunique()
df.groupby(["store"])["item"].nunique()
df.groupby(["store", "item"]).agg({"sales": ["sum"]})
df.groupby(["store", "item"]).agg({"sales": ["sum", "mean", "median", "std"]})
df.head()
#####################################################
# FEATURE ENGINEERING
#####################################################
df.head()
def create_date_features(df):
df['month'] = df.date.dt.month
df['day_of_month'] = df.date.dt.day
df['day_of_year'] = df.date.dt.dayofyear
df['week_of_year'] = df.date.dt.weekofyear
df['day_of_week'] = df.date.dt.dayofweek
df['year'] = df.date.dt.year
df["is_wknd"] = df.date.dt.weekday // 4
df['is_month_start'] = df.date.dt.is_month_start.astype(int)
df['is_month_end'] = df.date.dt.is_month_end.astype(int)
return df
df = create_date_features(df)
df.groupby(["store", "item", "month"]).agg({"sales": ["sum", "mean", "median", "std"]})
########################
# Random Noise
########################
def random_noise(dataframe):
return np.random.normal(scale=1.6, size=(len(dataframe),))
########################
# Lag/Shifted Features
########################
df.sort_values(by=['store', 'item', 'date'], axis=0, inplace=True)
pd.DataFrame({"sales": df["sales"].values[0:10],
"lag1": df["sales"].shift(1).values[0:10],
"lag2": df["sales"].shift(2).values[0:10],
"lag3": df["sales"].shift(3).values[0:10],
"lag4": df["sales"].shift(4).values[0:10]})
df.groupby(["store", "item"])['sales'].head()
df.groupby(["store", "item"])['sales'].transform(lambda x: x.shift(1))
def lag_features(dataframe, lags):
for lag in lags:
dataframe['sales_lag_' + str(lag)] = dataframe.groupby(["store", "item"])['sales'].transform(
lambda x: x.shift(lag)) + random_noise(dataframe)
return dataframe
df = lag_features(df, [91, 98, 105, 112, 119, 126, 182, 364, 546, 728])
check_df(df)
########################
# Rolling Mean Features
########################
pd.DataFrame({"sales": df["sales"].values[0:10],
"roll2": df["sales"].shift(1).rolling(window=2).mean().values[0:10],
"roll3": df["sales"].shift(1).rolling(window=3).mean().values[0:10],
"roll5": df["sales"].shift(1).rolling(window=5).mean().values[0:10]})
def roll_mean_features(dataframe, windows):
for window in windows:
dataframe['sales_roll_mean_' + str(window)] = dataframe.groupby(["store", "item"])['sales']. \
transform(
lambda x: x.shift(1).rolling(window=window, min_periods=10, win_type="triang").mean()) + random_noise(
dataframe)
return dataframe
df = roll_mean_features(df, [365, 546])
########################
# Exponentially Weighted Mean Features
########################
pd.DataFrame({"sales": df["sales"].values[0:10],
"roll2": df["sales"].shift(1).rolling(window=2).mean().values[0:10],
"ewm099": df["sales"].shift(1).ewm(alpha=0.99).mean().values[0:10],
"ewm095": df["sales"].shift(1).ewm(alpha=0.95).mean().values[0:10],
"ewm07": df["sales"].shift(1).ewm(alpha=0.7).mean().values[0:10],
"ewm02": df["sales"].shift(1).ewm(alpha=0.1).mean().values[0:10]})
def ewm_features(dataframe, alphas, lags):
for alpha in alphas:
for lag in lags:
dataframe['sales_ewm_alpha_' + str(alpha).replace(".", "") + "_lag_" + str(lag)] = \
dataframe.groupby(["store", "item"])['sales'].transform(lambda x: x.shift(lag).ewm(alpha=alpha).mean())
return dataframe
alphas = [0.95, 0.9, 0.8, 0.7, 0.5]
lags = [91, 98, 105, 112, 180, 270, 365, 546, 728]
df = ewm_features(df, alphas, lags)
check_df(df)
########################
# One-Hot Encoding
########################
df = pd.get_dummies(df, columns=['store', 'item', 'day_of_week', 'month'])
check_df(df)
########################
# Converting sales to log(1+sales)
########################
df['sales'] = np.log1p(df["sales"].values)
check_df(df)
#####################################################
# Model
#####################################################
########################
# Custom Cost Function
########################
# MAE, MSE, RMSE, SSE
# MAE: mean absolute error
# MAPE: mean absolute percentage error
# SMAPE: Symmetric mean absolute percentage error (adjusted MAPE)
def smape(preds, target):
n = len(preds)
masked_arr = ~((preds == 0) & (target == 0))
preds, target = preds[masked_arr], target[masked_arr]
num = np.abs(preds - target)
denom = np.abs(preds) + np.abs(target)
smape_val = (200 * np.sum(num / denom)) / n
return smape_val
def lgbm_smape(preds, train_data):
labels = train_data.get_label()
smape_val = smape(np.expm1(preds), np.expm1(labels))
return 'SMAPE', smape_val, False
########################
# Time-Based Validation Sets
########################
train
test
# 2017'nin başına kadar (2016'nın sonuna kadar) train seti.
train = df.loc[(df["date"] < "2017-01-01"), :]
# 2017'nin ilk 3'ayı validasyon seti.
val = df.loc[(df["date"] >= "2017-01-01") & (df["date"] < "2017-04-01"), :]
cols = [col for col in train.columns if col not in ['date', 'id', "sales", "year"]]
Y_train = train['sales']
X_train = train[cols]
Y_val = val['sales']
X_val = val[cols]
Y_train.shape, X_train.shape, Y_val.shape, X_val.shape
########################
# LightGBM ile Zaman Serisi Modeli
########################
# LightGBM parameters
lgb_params = {'num_leaves': 10,
'learning_rate': 0.02,
'feature_fraction': 0.8,
'max_depth': 5,
'verbose': 0,
'num_boost_round': 15000,
'early_stopping_rounds': 200,
'nthread': -1}
# metric mae: l1, absolute loss, mean_absolute_error, regression_l1
# mse: l2, square loss, mean_squared_error, mse, regression_l2, regression
# rmse, root square loss, root_mean_squared_error, l2_root
# mape, MAPE loss, mean_absolute_percentage_error
# num_leaves: bir ağaçtaki maksimum yaprak sayısı
# learning_rate: shrinkage_rate, eta
# feature_fraction: rf'nin random subspace özelliği. her iterasyonda rastgele göz önünde bulundurulacak değişken sayısı.
# max_depth: maksimum derinlik
# num_boost_round: n_estimators, number of boosting iterations. En az 10000-15000 civarı yapmak lazım.
# early_stopping_rounds: validasyon setindeki metrik belirli bir early_stopping_rounds'da ilerlemiyorsa yani
# hata düşmüyorsa modellemeyi durdur.
# hem train süresini kısaltır hem de overfit'e engel olur.
# nthread: num_thread, nthread, nthreads, n_jobs
lgbtrain = lgb.Dataset(data=X_train, label=Y_train, feature_name=cols)
lgbval = lgb.Dataset(data=X_val, label=Y_val, reference=lgbtrain, feature_name=cols)
model = lgb.train(lgb_params, lgbtrain,
valid_sets=[lgbtrain, lgbval],
num_boost_round=lgb_params['num_boost_round'],
early_stopping_rounds=lgb_params['early_stopping_rounds'],
feval=lgbm_smape,
verbose_eval=100)
y_pred_val = model.predict(X_val, num_iteration=model.best_iteration)
smape(np.expm1(y_pred_val), np.expm1(Y_val))
########################
# Değişken Önem Düzeyleri
########################
def plot_lgb_importances(model, plot=False, num=10):
gain = model.feature_importance('gain')
feat_imp = pd.DataFrame({'feature': model.feature_name(),
'split': model.feature_importance('split'),
'gain': 100 * gain / gain.sum()}).sort_values('gain', ascending=False)
if plot:
plt.figure(figsize=(10, 10))
sns.set(font_scale=1)
sns.barplot(x="gain", y="feature", data=feat_imp[0:25])
plt.title('feature')
plt.tight_layout()
plt.show()
else:
print(feat_imp.head(num))
return feat_imp
plot_lgb_importances(model, num=200)
plot_lgb_importances(model, num=30, plot=True)
feat_imp = plot_lgb_importances(model, num=200)
importance_zero = feat_imp[feat_imp["gain"] == 0]["feature"].values
imp_feats = [col for col in cols if col not in importance_zero]
len(imp_feats)
########################
# Final Model
########################
train = df.loc[~df.sales.isna()]
Y_train = train['sales']
X_train = train[cols]
test = df.loc[df.sales.isna()]
X_test = test[cols]
lgb_params = {'num_leaves': 10,
'learning_rate': 0.02,
'feature_fraction': 0.8,
'max_depth': 5,
'verbose': 0,
'nthread': -1,
"num_boost_round": model.best_iteration}
lgbtrain_all = lgb.Dataset(data=X_train, label=Y_train, feature_name=cols)
final_model = lgb.train(lgb_params, lgbtrain_all, num_boost_round=model.best_iteration)
test_preds = final_model.predict(X_test, num_iteration=model.best_iteration)
########################
# Submission File
########################
test.head()
submission_df = test.loc[:, ["id", "sales"]]
submission_df['sales'] = np.expm1(test_preds)
submission_df['id'] = submission_df.id.astype(int)
submission_df.to_csv("submission_demand.csv", index=False)