-
Notifications
You must be signed in to change notification settings - Fork 0
/
hackathon.py
307 lines (247 loc) · 9.82 KB
/
hackathon.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
import sys
import pandas as pd
import numpy as np
import datetime
from sklearn.preprocessing import OrdinalEncoder
from sklearn.pipeline import Pipeline
from cyclic_boosting import binning, flags, CBPoissonRegressor, observers, common_smoothers
from cyclic_boosting.smoothing.onedim import SeasonalSmoother, IsotonicRegressor
from cyclic_boosting.plots import plot_analysis
from IPython import embed
def plot_CB(filename, plobs, binner):
for i, p in enumerate(plobs):
plot_analysis(
plot_observer=p,
file_obj=filename + "_{}".format(i), use_tightlayout=False,
binners=[binner]
)
def eval_results(yhat_mean, y):
mad = np.nanmean(np.abs(y - yhat_mean))
print('MAD: {}'.format(mad))
mse = np.nanmean(np.square(y - yhat_mean))
print('MSE: {}'.format(mse))
mape = np.nansum(np.abs(y - yhat_mean)) / np.nansum(y)
print('MAPE: {}'.format(mape))
smape = 100. * np.nanmean(np.abs(y - yhat_mean) / ((np.abs(y) + np.abs(yhat_mean)) / 2.))
print('SMAPE: {}'.format(smape))
md = np.nanmean(y - yhat_mean)
print('MD: {}'.format(md))
mean_y = np.nanmean(y)
print('mean(y): {}'.format(mean_y))
def get_events(df):
for event in [
'Christmas',
'Easter',
'Labour_Day',
'German_Unity',
'Other_Holiday',
'Local_Holiday_0',
'Local_Holiday_1',
'Local_Holiday_2'
]:
for event_date in df['DATE'][df['EVENT'] == event].unique():
for event_days in range(-10, 11):
df.loc[df['DATE'] == pd.to_datetime(event_date) + datetime.timedelta(days=event_days), event] = event_days
return df
def prepare_data(df):
df['DATE'] = pd.to_datetime(df['DATE'])
df['dayofweek'] = df['DATE'].dt.dayofweek
df['dayofyear'] = df['DATE'].dt.dayofyear
df['month'] = df['DATE'].dt.month
df['dayofmonth'] = df['DATE'].dt.day
df['td'] = (df['DATE'] - df['DATE'].min()).dt.days
df['price_ratio'] = df['SALES_PRICE'] / df['NORMAL_PRICE']
df['price_ratio'].fillna(1, inplace=True)
df['price_ratio'].clip(0, 1, inplace=True)
df.loc[df['price_ratio'] == 1., 'price_ratio'] = np.nan
df = get_events(df)
enc = OrdinalEncoder(handle_unknown='use_encoded_value', unknown_value=np.nan)
df[['L_ID', 'P_ID', 'PG_ID_1', 'PG_ID_2', 'PG_ID_3']] = enc.fit_transform(df[['L_ID', 'P_ID', 'PG_ID_1', 'PG_ID_2', 'PG_ID_3']])
return df
def fill_gaps(df):
df_dates = pd.DataFrame(
{
"DATE": pd.date_range(start=df['DATE'].min(), end=df['DATE'].max()),
}
)
df = df_dates.merge(df, on="DATE", how="left")
df['SALES'].fillna(0, inplace=True)
defaults = {
'PG_ID_1': df['PG_ID_1'].iloc[0],
'PG_ID_2': df['PG_ID_2'].iloc[0],
'PG_ID_3': df['PG_ID_3'].iloc[0],
'NORMAL_PRICE': df['NORMAL_PRICE'].iloc[0],
'SALES_AREA': df['SALES_AREA'].iloc[0],
'SCHOOL_HOLIDAY': 0.0,
'PROMOTION_TYPE': 0.0,
'SALES_PRICE': df['NORMAL_PRICE'].iloc[0]
}
df.fillna(value=defaults, inplace=True)
return df
def fill_zeros(df):
df = df.groupby(['L_ID', 'P_ID']).apply(fill_gaps)
df = df.drop(columns=['L_ID', 'P_ID']).reset_index()
return df
def feature_properties():
fp = {}
fp['P_ID'] = flags.IS_UNORDERED
fp['PG_ID_1'] = flags.IS_UNORDERED
fp['PG_ID_2'] = flags.IS_UNORDERED
fp['PG_ID_3'] = flags.IS_UNORDERED
fp['L_ID'] = flags.IS_UNORDERED
fp['dayofweek'] = flags.IS_ORDERED
fp['month'] = flags.IS_ORDERED
fp['dayofyear'] = flags.IS_CONTINUOUS | flags.IS_LINEAR
fp['dayofmonth'] = flags.IS_CONTINUOUS
fp['price_ratio'] = flags.IS_CONTINUOUS | flags.HAS_MISSING | flags.MISSING_NOT_LEARNED
fp['PROMOTION_TYPE'] = flags.IS_ORDERED
fp['SCHOOL_HOLIDAY'] = flags.IS_ORDERED
fp['Christmas'] = flags.IS_ORDERED | flags.HAS_MISSING | flags.MISSING_NOT_LEARNED
fp['Easter'] = flags.IS_ORDERED | flags.HAS_MISSING | flags.MISSING_NOT_LEARNED
fp['Labour_Day'] = flags.IS_ORDERED | flags.HAS_MISSING | flags.MISSING_NOT_LEARNED
fp['German_Unity'] = flags.IS_ORDERED | flags.HAS_MISSING | flags.MISSING_NOT_LEARNED
fp['Other_Holiday'] = flags.IS_ORDERED | flags.HAS_MISSING | flags.MISSING_NOT_LEARNED
fp['Local_Holiday_0'] = flags.IS_ORDERED | flags.HAS_MISSING | flags.MISSING_NOT_LEARNED
fp['Local_Holiday_1'] = flags.IS_ORDERED | flags.HAS_MISSING | flags.MISSING_NOT_LEARNED
fp['Local_Holiday_2'] = flags.IS_ORDERED | flags.HAS_MISSING | flags.MISSING_NOT_LEARNED
fp['NORMAL_PRICE'] = flags.IS_CONTINUOUS
fp['td'] = flags.IS_CONTINUOUS | flags.IS_LINEAR
return fp
def cb_model():
fp = feature_properties()
explicit_smoothers = {('dayofyear',): SeasonalSmoother(order=3),
('price_ratio',): IsotonicRegressor(increasing=False),
('NORMAL_PRICE',): IsotonicRegressor(increasing=False),
}
features = [
'dayofweek',
'L_ID',
'PG_ID_1',
'PG_ID_2',
'PG_ID_3',
'P_ID',
'PROMOTION_TYPE',
'price_ratio',
'dayofyear',
'month',
'dayofmonth',
'SCHOOL_HOLIDAY',
'Christmas',
'Easter',
'Labour_Day',
'German_Unity',
'Other_Holiday',
'Local_Holiday_0',
'Local_Holiday_1',
'Local_Holiday_2',
('L_ID', 'td'),
('P_ID', 'td'),
('P_ID', 'L_ID'),
('L_ID', 'dayofweek'),
('PG_ID_1', 'dayofweek'),
('PG_ID_2', 'dayofweek'),
('PG_ID_3', 'dayofweek'),
('P_ID', 'dayofweek'),
('L_ID', 'PG_ID_1', 'dayofweek'),
('L_ID', 'PG_ID_2', 'dayofweek'),
('L_ID', 'PG_ID_3', 'dayofweek'),
('SCHOOL_HOLIDAY', 'dayofweek'),
('SCHOOL_HOLIDAY', 'L_ID', 'dayofweek'),
('SCHOOL_HOLIDAY', 'PG_ID_3', 'dayofweek'),
('SCHOOL_HOLIDAY', 'L_ID', 'PG_ID_3', 'dayofweek'),
('L_ID', 'dayofmonth'),
('PG_ID_3', 'dayofmonth'),
('L_ID', 'PG_ID_3', 'dayofmonth'),
('L_ID', 'dayofyear'),
('PG_ID_3', 'dayofyear'),
('P_ID', 'dayofyear'),
('L_ID', 'PG_ID_3', 'dayofyear'),
('L_ID', 'Christmas'),
('L_ID', 'Easter'),
('L_ID', 'Labour_Day'),
('L_ID', 'German_Unity'),
('L_ID', 'Local_Holiday_0'),
('L_ID', 'Local_Holiday_1'),
('PG_ID_3', 'Christmas'),
('PG_ID_3', 'Easter'),
('PG_ID_3', 'Labour_Day'),
('PG_ID_3', 'German_Unity'),
('PG_ID_3', 'Local_Holiday_0'),
('PG_ID_3', 'Local_Holiday_1'),
('P_ID', 'Christmas'),
('P_ID', 'Easter'),
('P_ID', 'Labour_Day'),
('P_ID', 'German_Unity'),
('P_ID', 'Local_Holiday_0'),
('P_ID', 'Local_Holiday_1'),
('L_ID', 'PG_ID_3', 'Christmas'),
('L_ID', 'PG_ID_3', 'Easter'),
('L_ID', 'PG_ID_3', 'Labour_Day'),
('L_ID', 'PG_ID_3', 'German_Unity'),
('L_ID', 'PG_ID_3', 'Local_Holiday_0'),
('L_ID', 'PG_ID_3', 'Local_Holiday_1'),
('PROMOTION_TYPE', 'dayofweek'),
('price_ratio', 'dayofweek'),
('P_ID', 'PROMOTION_TYPE'),
('P_ID', 'price_ratio'),
'NORMAL_PRICE',
]
plobs = [observers.PlottingObserver(iteration=1), observers.PlottingObserver(iteration=-1)]
est = CBPoissonRegressor(
feature_properties=fp,
feature_groups=features,
observers=plobs,
maximal_iterations=50,
smoother_choice=common_smoothers.SmootherChoiceGroupBy(
use_regression_type=True,
use_normalization=False,
explicit_smoothers=explicit_smoothers),
)
binner = binning.BinNumberTransformer(n_bins=100, feature_properties=fp)
ml_est = Pipeline([("binning", binner), ("CB", est)])
return ml_est
def training(X, y):
CB_est = cb_model()
CB_est.fit(X, y)
plot_CB('analysis_CB_mean_iterlast', [CB_est[-1].observers[0], CB_est[-1].observers[-1]], CB_est[-2])
del X
return CB_est
def inference(X, ml_est_mean):
yhat_mean = ml_est_mean.predict(X)
del X
return yhat_mean
def main(args):
df_train = pd.read_parquet("blueyonder-pyconpydata-2023/train_BY_hackathon_final.parquet.gzip")
df_test = pd.read_parquet("blueyonder-pyconpydata-2023/test_BY_hackathon_without_sales_final.parquet.gzip")
# fill zeros
df_train = fill_zeros(df_train)
df_test['SALES'] = np.nan
df = pd.concat([df_train, df_test], ignore_index=True)
df = prepare_data(df)
df_train = df.loc[df['DATE']<='2022-03-31']
df_test = df.loc[df['DATE']>'2022-03-31']
# cut out anomalies
df_train = df_train.loc[df_train['SALES'] >= 0]
df_train = df_train.loc[df_train['SALES'] < 1000]
y_train = np.asarray(df_train['SALES'])
X_train = df_train.drop(columns='SALES')
X_test = df_test.drop(columns='SALES')
CB_est = training(X_train.copy(), y_train)
X_train['yhat'] = inference(X_train.copy(), CB_est)
# in-sample evaluation
X_train['y'] = y_train
eval_results(X_train['yhat'], X_train['y'])
X_test['Predicted'] = inference(X_test.copy(), CB_est)
X_test = X_test[['Id', 'Predicted']]
X_test.to_csv("CB_master_submission.csv", index=False)
# out-of-sample evaluation
X_test.reset_index(drop=True, inplace=True)
df_y_test = pd.read_parquet("blueyonder-pyconpydata-2023/test_BY_hackathon_results_final.parquet.gzip")
df_y_test.reset_index(drop=True, inplace=True)
df_y_test['Predicted'] = X_test['Predicted']
eval_results(df_y_test['Predicted'], df_y_test['Expected'])
eval_results(df_y_test.loc[df_y_test['Usage'] == 'Public', 'Predicted'], df_y_test.loc[df_y_test['Usage'] == 'Public', 'Expected'])
eval_results(df_y_test.loc[df_y_test['Usage'] == 'Private', 'Predicted'], df_y_test.loc[df_y_test['Usage'] == 'Private', 'Expected'])
embed()
if __name__ == "__main__":
main(sys.argv[1:])