Kaggle competition: M5 Forecasting - Accuracy
<SOS> metric: M5_WRMSSE Kaggle data source: m5-forecasting-accuracy<find_best_params>
!pip install mlforecast
print(xgb_grid.best_score_)
 print(xgb_grid.best_params_ )<predict_on_test>
from copy import copy
from functools import partial
from pathlib import Path
import lightgbm as lgb
import numpy as np
import pandas as pd
from mlforecast.core import TimeSeries
from mlforecast.forecast import Forecast
from window_ops.rolling import rolling_mean
predictionsLinear = xgb_grid.predict(X_test )<normalization>
input_path = Path('.. /input/m5-preprocess/processed/')
 data = pd.read_parquet(input_path/'sales.parquet')
 data
plt.scatter(np.exp(yStandardScaler.inverse_transform(y_test)) ,np.exp(yStandardScaler.inverse_transform(predictionsLinear)) )<normalization>
prices = pd.read_parquet(input_path/'prices.parquet')
 prices
sns.distplot(( yStandardScaler.inverse_transform(y_test)-yStandardScaler.inverse_transform(predictionsLinear)) ,bins=50);<compute_test_metric>
cal = pd.read_parquet(input_path/'calendar.parquet')
 cal = cal.rename(columns={'date': 'ds'})
 cal.head()
evaluateModel('Light GBM',y_test,predictionsLinear )<split>
lgb_params = {
'objective': 'poisson',
'metric': 'rmse',
'force_row_wise': True,
'learning_rate': 0.075,
'bagging_freq': 1,
'bagging_fraction': 0.75,
'lambda_l2': 0.1,
'n_estimators': 1200,
'num_leaves': 128,
'min_data_in_leaf': 100,
}
model = lgb.LGBMRegressor(**lgb_params)
 model
X_train_nn, X_test_nn, y_train_nn, y_test_nn = train_test_split(X, y, test_size=0.3, random_state=42 )<train_model>
ts = TimeSeries(
 freq='D',
lags=[7, 28],
lag_transforms = {
7: [(rolling_mean, 7),(rolling_mean, 28)],
28: [(rolling_mean, 7),(rolling_mean, 28)],
},
date_features=['year', 'month', 'day', 'dayofweek', 'quarter', 'week'], 
)
 ts
print("X_train Shape: ",X_train_nn.shape)
 print("X_test Shape: ",X_test_nn.shape)
 print("y_train Shape: ",y_train_nn.shape)
 print("y_test Shape: ",y_test_nn.shape )<normalization>
fcst = Forecast(model, ts )
xStandardScaler = StandardScaler()
yStandardScaler = StandardScaler()
X_train_nn = xStandardScaler.fit_transform(X_train_nn)
 X_test_nn = xStandardScaler.transform(X_test_nn)
 y_train_nn = yStandardScaler.fit_transform(y_train_nn.ravel().reshape(-1, 1))
y_test_nn= yStandardScaler.transform(y_test_nn.ravel().reshape(-1, 1))<choose_model_class>
%%time
features_df = fcst.preprocess(
 data,
dropna=True,
keep_last_n=28+27,
static_features=['item_id', 'dept_id', 'cat_id', 'store_id', 'state_id'] 
)
 del data
model = Sequential()
model.add(Dense(256, input_dim=301))
model.add(LeakyReLU(alpha=.1))
model.add(Dropout (.1))
model.add(Dense(192, activation='relu'))
model.add(Dropout (.2))
model.add(Dense(128, activation='relu'))
model.add(Dropout (.1))
model.add(Dense(64, activation='relu'))
model.add(Dropout (.2))
model.add(Dense(32, activation='relu'))
model.add(Dropout (.1))
model.add(Dense(16, activation='relu'))
model.add(Dense(1))
model.summary()<choose_model_class>
np.random.seed(11)
 train_mask = np.random.rand(features_df.shape[0])< 0.95
train, valid = features_df[train_mask], features_df[~train_mask]
X_train, y_train = train.drop(columns=['ds', 'y']), train.y
X_valid, y_valid = valid.drop(columns=['ds', 'y']), valid.y
del features_df, train, valid
model.compile(loss='mean_squared_error',optimizer='adam',metrics=['mse'] )<train_model>
%time fcst.model.fit(X_train, y_train, eval_set=[(X_train, y_train),(X_valid, y_valid)], verbose=20 )
history = model.fit(X_train_nn,y_train_nn,epochs=100,batch_size=32,callbacks=mcp,validation_data=(X_test_nn,y_test_nn))<load_pretrained>
def my_predict_fn(model, new_x, features_order, alpha):
new_x = new_x.reset_index()
new_x = new_x.merge(cal)
 new_x = new_x.merge(prices)
 new_x = new_x.sort_values('unique_id')
 new_x = new_x[features_order]
predictions = model.predict(new_x)
 return alpha * predictions
model = load_model('model.h5' )<predict_on_test>
fcst.ts.num_threads
predictionsNeural = model.predict(X_test_nn )<compute_train_metric>
%%time
alphas = [1.028, 1.023, 1.018]
preds = None
for alpha in alphas:
alpha_preds = fcst.predict(28, my_predict_fn, alpha=alpha)
 alpha_preds = alpha_preds.set_index('ds', append=True)
 if preds is None:
preds = 1 / 3 * alpha_preds
else:
preds += 1 / 3 * alpha_preds
preds
modelComp = dict()
modelComp['Name'] = 'Neural Network'
modelComp['MAE'] = metrics.mean_absolute_error(yStandardScaler.inverse_transform(y_test_nn),
yStandardScaler.inverse_transform(predictionsNeural))
modelComp['MSE'] = metrics.mean_squared_error(yStandardScaler.inverse_transform(y_test_nn),
yStandardScaler.inverse_transform(predictionsNeural))
modelComp['RMSE'] = np.sqrt(metrics.mean_squared_error(yStandardScaler.inverse_transform(y_test_nn),
yStandardScaler.inverse_transform(predictionsNeural)))
 print('MAE:', modelComp['MAE'])
 print('MSE:', modelComp['MSE'])
 print('RMSE:', modelComp['RMSE'])
 ModelCompList.append(modelComp )<prepare_output>
wide = preds.reset_index().pivot_table(index='unique_id', columns='ds')
 wide.columns = [f'F{i+1}' for i in range(28)]
wide.columns.name = None
wide.index.name = 'id'
wide
<load_from_csv><EOS> 
sample_sub = pd.read_csv(
 '.. /input/m5-forecasting-accuracy/sample_submission.csv', index_col='id' 
)
 sample_sub.update(wide)
 np.testing.assert_allclose(sample_sub.sum().sum() , preds['y_pred'].sum())
 sample_sub.to_csv('submission.csv' )