Kaggle competition: M5 Forecasting - Accuracy
<SOS> metric: M5_WRMSSE Kaggle data source: m5-forecasting-accuracy<categorify>
pd.set_option('max_columns', 50)
 plt.style.use('bmh')
 color_cycle = cycle(plt.rcParams['axes.prop_cycle'].by_key() ['color'] )
train["Type"] = train["Type"].replace({"A":0,"B":1,"C":2})
 test["Type"] = test["Type"].replace({"A":0,"B":1,"C":2} )<compute_test_metric>
osj = os.path.join
INPUT_DIR = '.. /input/m5-forecasting-accuracy/'
train = pd.read_csv(osj(INPUT_DIR, 'sales_train_validation.csv'))
price = pd.read_csv(osj(INPUT_DIR, 'sell_prices.csv'))
calender = pd.read_csv(osj(INPUT_DIR, 'calendar.csv'))
sample_submit = pd.read_csv(osj(INPUT_DIR, 'sample_submission.csv'))
def wmae(y_pred, targ, holiday_week):
sumOfWeights = 0
sumofCol_B_X_Col_E = 0
for i in range(0, len(y_pred)) :
weight = 0
if holiday_week[i]:
weight = 5
else:
weight = 1
Col_B_X_Col_E = abs(targ[i] - y_pred[i])*weight
sumOfWeights += weight
sumofCol_B_X_Col_E += Col_B_X_Col_E
WMAE = sumofCol_B_X_Col_E/sumOfWeights
return WMAE<groupby>
calender['date'] = pd.to_datetime(calender['date'] )
train.groupby("Type")["Weekly_Sales"].mean()<define_variables>
d_cols = [c for c in train.columns if 'd_' in c]
feature = ["Store","Dept","year","month","day","week","IsHoliday", "Size"]<prepare_x_and_y>
past_sales = train.set_index('id')[d_cols].T.merge(calender.set_index('d')['date'],
left_index=True,
right_index=True,
validate='1:1' ).set_index('date' )
X_train = train[feature]
X_test = test[feature]<prepare_x_and_y>
def make_total_sales_lowess(p):
total_sales = pd.DataFrame(p.sum(axis=1), columns=['total sales'])
 total_sales_noXmas = total_sales.drop(index=[dt.datetime(2011,12,25),
dt.datetime(2012,12,25),
dt.datetime(2013,12,25),
dt.datetime(2014,12,25),
dt.datetime(2015,12,25)])
 df = total_sales_noXmas
lowess = sm.nonparametric.lowess(df['total sales'], df.index, frac=.3)
 lowess_x = list(zip(*lowess)) [0]
lowess_y = list(zip(*lowess)) [1]
f = interp1d(lowess_x, lowess_y, bounds_error=False)
 new_lowess_x = df.index
new_lowess_y = f(new_lowess_x)
 total_sales_lowess = total_sales_noXmas
total_sales_lowess['lowess'] = new_lowess_y
total_sales_lowess['total sales-lowess'] = total_sales_lowess['total sales'] - total_sales_lowess['lowess']
return total_sales_lowess
y_train = train["Weekly_Sales"]<split>
total_sales_lowess = make_total_sales_lowess(past_sales )
X_train1, X_test1, y_train1, y_test1 = train_test_split(X_train,y_train, test_size=0.2 )<choose_model_class>
submission = pd.read_csv('.. /input/m5-three-shades-of-dark-darker-magic/submission_v1.csv' )
%%time
model = RandomForestRegressor(n_estimators=10,n_jobs=4)
<train_model>
teams_now = 3215
teams_before = 3186
submission.loc[submission['id'].str.contains('CA_1'), 'F1'] *= 1.009
submission.loc[submission['id'].str.contains('CA_2'), 'F1'] *= 1.02
submission.loc[submission['id'].str.contains('CA_3'), 'F1'] *= 1.009
submission.loc[submission['id'].str.contains('CA_4'), 'F1'] *= 1.011
submission.loc[submission['id'].str.contains('TX_1'), 'F1'] *= 1.01
submission.loc[submission['id'].str.contains('TX_2'), 'F1'] *= 1.011
submission.loc[submission['id'].str.contains('TX_3'), 'F1'] *= 1.011
submission.loc[submission['id'].str.contains('WI_1'), 'F1'] *= 1.015
submission.loc[submission['id'].str.contains('WI_2'), 'F1'] *= 1.02
submission.loc[submission['id'].str.contains('WI_3'), 'F1'] *= 1.015
submission['F1'].sum()
model.fit(X_train1,y_train1 )<data_type_conversions>
magic = submission['F2'].sum()
japan_emergency = 119
submission.loc[submission['id'].str.contains('CA_1'), 'F2'] *= 0.994
submission.loc[submission['id'].str.contains('CA_2'), 'F2'] *= 0.998
submission.loc[submission['id'].str.contains('CA_3'), 'F2'] *= 0.994
submission.loc[submission['id'].str.contains('CA_4'), 'F2'] *= 0.996
submission.loc[submission['id'].str.contains('TX_1'), 'F2'] *= 0.995
submission.loc[submission['id'].str.contains('TX_2'), 'F2'] *= 0.996
submission.loc[submission['id'].str.contains('TX_3'), 'F2'] *= 0.996
submission.loc[submission['id'].str.contains('WI_1'), 'F2'] *= 0.998
submission.loc[submission['id'].str.contains('WI_2'), 'F2'] *= 1
submission.loc[submission['id'].str.contains('WI_3'), 'F2'] *= 0.998
submission['F2'].sum()
holidays = X_test1['IsHoliday'].to_numpy()
y_test1 = y_test1.to_numpy()<predict_on_test>
submission.loc[submission['id'].str.contains('CA_1'), 'F3'] *= 0.994
submission.loc[submission['id'].str.contains('CA_2'), 'F3'] *= 0.998
submission.loc[submission['id'].str.contains('CA_3'), 'F3'] *= 0.994
submission.loc[submission['id'].str.contains('CA_4'), 'F3'] *= 0.996
submission.loc[submission['id'].str.contains('TX_1'), 'F3'] *= 0.995
submission.loc[submission['id'].str.contains('TX_2'), 'F3'] *= 0.996
submission.loc[submission['id'].str.contains('TX_3'), 'F3'] *= 0.996
submission.loc[submission['id'].str.contains('WI_1'), 'F3'] *= 1
submission.loc[submission['id'].str.contains('WI_2'), 'F3'] *= 1.005
submission.loc[submission['id'].str.contains('WI_3'), 'F3'] *= 1
submission['F3'].sum()
result = model.predict(X_test1 )<compute_test_metric>
sex_constant = 1.0073
submission.loc[submission['id'].str.contains('CA_1'), 'F4'] *= 0.98
submission.loc[submission['id'].str.contains('CA_2'), 'F4'] *= 1.02
submission.loc[submission['id'].str.contains('CA_3'), 'F4'] *= 0.98
submission.loc[submission['id'].str.contains('CA_4'), 'F4'] *= 1
submission.loc[submission['id'].str.contains('TX_1'), 'F4'] *= 0.99
submission.loc[submission['id'].str.contains('TX_2'), 'F4'] *= 1
submission.loc[submission['id'].str.contains('TX_3'), 'F4'] *= 1
submission.loc[submission['id'].str.contains('WI_1'), 'F4'] *= 1.00
submission.loc[submission['id'].str.contains('WI_2'), 'F4'] *= 1.02
submission.loc[submission['id'].str.contains('WI_3'), 'F4'] *= 1.00
submission['F4'].sum()
WMAE = wmae(result, y_test1, holidays)
 print(WMAE )<find_best_params>
color_of_the_night = 995874
color_of_the_night /= 1000000
submission.loc[submission['id'].str.contains('CA_1'), 'F5'] *= 0.995
submission.loc[submission['id'].str.contains('CA_2'), 'F5'] *= 1
submission.loc[submission['id'].str.contains('CA_3'), 'F5'] *= 0.995
submission.loc[submission['id'].str.contains('CA_4'), 'F5'] *= 0.998
submission.loc[submission['id'].str.contains('TX_1'), 'F5'] *= 0.996
submission.loc[submission['id'].str.contains('TX_2'), 'F5'] *= 0.998
submission.loc[submission['id'].str.contains('TX_3'), 'F5'] *= 0.998
submission.loc[submission['id'].str.contains('WI_1'), 'F5'] *= 1
submission.loc[submission['id'].str.contains('WI_2'), 'F5'] *= 1.01
submission.loc[submission['id'].str.contains('WI_3'), 'F5'] *= 1
submission['F5'].sum()
grid_search.best_params_<import_modules>
injury = 1.000376
submission.loc[submission['id'].str.contains('CA_1'), 'F6'] *= 1.0002
submission.loc[submission['id'].str.contains('CA_2'), 'F6'] *= 1.001
submission.loc[submission['id'].str.contains('CA_3'), 'F6'] *= 1.0002
submission.loc[submission['id'].str.contains('CA_4'), 'F6'] *= 1.001
submission.loc[submission['id'].str.contains('TX_1'), 'F6'] *= 1.000
submission.loc[submission['id'].str.contains('TX_2'), 'F6'] *= 1.001
submission.loc[submission['id'].str.contains('TX_3'), 'F6'] *= 1.001
submission.loc[submission['id'].str.contains('WI_1'), 'F6'] *= 1.001
submission.loc[submission['id'].str.contains('WI_2'), 'F6'] *= 1.01
submission.loc[submission['id'].str.contains('WI_3'), 'F6'] *= 1.001
submission['F6'].sum()
import xgboost as xgb<prepare_x_and_y>
np.random.seed(198505)
 correction = np.random.randint(1000000)/1000000
submission.loc[submission['id'].str.contains('CA_1'), 'F7'] *= 0.994
submission.loc[submission['id'].str.contains('CA_2'), 'F7'] *= 1
submission.loc[submission['id'].str.contains('CA_3'), 'F7'] *= 0.994
submission.loc[submission['id'].str.contains('CA_4'), 'F7'] *= 0.998
submission.loc[submission['id'].str.contains('TX_1'), 'F7'] *= 0.995635
submission.loc[submission['id'].str.contains('TX_2'), 'F7'] *= 0.998
submission.loc[submission['id'].str.contains('TX_3'), 'F7'] *= 0.998
submission.loc[submission['id'].str.contains('WI_1'), 'F7'] *= 1
submission.loc[submission['id'].str.contains('WI_2'), 'F7'] *= 1.01
submission.loc[submission['id'].str.contains('WI_3'), 'F7'] *= 1
submission['F7'].sum()
data_dmatrix = xgb.DMatrix(data=X_train,label=y_train )<choose_model_class>
submission.loc[submission['id'].str.contains('CA_1'), 'F8'] *= 0.996
submission.loc[submission['id'].str.contains('CA_2'), 'F8'] *= 1
submission.loc[submission['id'].str.contains('CA_3'), 'F8'] *= 0.996
submission.loc[submission['id'].str.contains('CA_4'), 'F8'] *= 0.998
submission.loc[submission['id'].str.contains('TX_1'), 'F8'] *= 0.9988
submission.loc[submission['id'].str.contains('TX_2'), 'F8'] *= 1
submission.loc[submission['id'].str.contains('TX_3'), 'F8'] *= 1
submission.loc[submission['id'].str.contains('WI_1'), 'F8'] *= 1
submission.loc[submission['id'].str.contains('WI_2'), 'F8'] *= 1.01
submission.loc[submission['id'].str.contains('WI_3'), 'F8'] *= 1
submission['F8'].sum()
xg_reg = xgb.XGBRegressor(objective ='reg:linear', colsample_bytree = 0.3, learning_rate = 0.1, 
<predict_on_test>
submission.loc[submission['id'].str.contains('CA_1'), 'F11'] *= 1
submission.loc[submission['id'].str.contains('CA_2'), 'F11'] *= 1
submission.loc[submission['id'].str.contains('CA_3'), 'F11'] *= 1
submission.loc[submission['id'].str.contains('CA_4'), 'F11'] *= 1
submission.loc[submission['id'].str.contains('TX_1'), 'F11'] *= 1
submission.loc[submission['id'].str.contains('TX_2'), 'F11'] *= 1
submission.loc[submission['id'].str.contains('TX_3'), 'F11'] *= 1
submission.loc[submission['id'].str.contains('WI_1'), 'F11'] *= 1
submission.loc[submission['id'].str.contains('WI_2'), 'F11'] *= 1
submission.loc[submission['id'].str.contains('WI_3'), 'F11'] *= 1
submission['F11'].sum()
xg_reg.fit(X_train1,y_train1)
 result = xg_reg.predict(X_test1 )<compute_test_metric>
for i in range(9,20):
if i!=11:
submission.loc[submission['id'].str.contains('CA_1'), 'F'+str(i)] *= 1
submission.loc[submission['id'].str.contains('CA_2'), 'F'+str(i)] *= 1.015
submission.loc[submission['id'].str.contains('CA_3'), 'F'+str(i)] *= 1
submission.loc[submission['id'].str.contains('CA_4'), 'F'+str(i)] *= 1.013
submission.loc[submission['id'].str.contains('TX_1'), 'F'+str(i)] *= 1
submission.loc[submission['id'].str.contains('TX_2'), 'F'+str(i)] *= 1.013
submission.loc[submission['id'].str.contains('TX_3'), 'F'+str(i)] *= 1.013
submission.loc[submission['id'].str.contains('WI_1'), 'F'+str(i)] *= 1.015
submission.loc[submission['id'].str.contains('WI_2'), 'F'+str(i)] *= 1.02
submission.loc[submission['id'].str.contains('WI_3'), 'F'+str(i)] *= 1.015
print(submission['F'+str(i)].sum() )
WMAE = wmae(result, y_test1, holidays)
 print(WMAE )<train_model>
for i in range(20,29):
submission.loc[submission['id'].str.contains('CA_1'), 'F'+str(i)] *= 1.015
submission.loc[submission['id'].str.contains('CA_2'), 'F'+str(i)] *= 1.03
submission.loc[submission['id'].str.contains('CA_3'), 'F'+str(i)] *= 1.015
submission.loc[submission['id'].str.contains('CA_4'), 'F'+str(i)] *= 1.025
submission.loc[submission['id'].str.contains('TX_1'), 'F'+str(i)] *= 1.02
submission.loc[submission['id'].str.contains('TX_2'), 'F'+str(i)] *= 1.025
submission.loc[submission['id'].str.contains('TX_3'), 'F'+str(i)] *= 1.025
submission.loc[submission['id'].str.contains('WI_1'), 'F'+str(i)] *= 1.03
submission.loc[submission['id'].str.contains('WI_2'), 'F'+str(i)] *= 1.04
submission.loc[submission['id'].str.contains('WI_3'), 'F'+str(i)] *= 1.03
print(submission['F'+str(i)].sum() )
<load_from_csv><EOS> 
submission.to_csv('submission.csv', index=False )