Kaggle competition: M5 Forecasting - Uncertainty
<SOS> metric: WeightedRowwisePinballLoss Kaggle data source: m5-forecasting-uncertainty<predict_on_test>
import pandas as pd, numpy as np
from matplotlib import pyplot as plt
import scipy.stats as stats
pred_tr, pred_te = extract_predictions(X_tr, X_te, grid, y_tr )<create_dataframe>
pd.options.display.max_columns = 50
def create_df_pred(grid):
df_pred = pd.DataFrame(grid.cv_results_)
 columns = [c for c in df_pred.columns if 'time' not in c
and 'split' not in c
and 'rank' not in c
and c!='params']
df_pred = df_pred[columns].sort_values('mean_test_score', ascending=False)
 df_pred['param_estimator__alpha'] = df_pred['param_estimator__alpha'].astype('float')
 df_pred['param_scaler'] = df_pred['param_scaler'].astype('str')
 return df_pred<predict_on_test>
best = pd.read_csv(".. /input/m5-accuracy-submission-file/submission.csv")
 best.head()
df_pred = create_df_pred(grid ).sort_values('mean_test_score')
 df_pred.head()<prepare_x_and_y>
sales = pd.read_csv(".. /input/m5-forecasting-uncertainty/sales_train_evaluation.csv")
 sales.head()
target = 'age'
tidx = 0
scale_values = [1,
1,
1,
1]
X_tr, X_te, y_tr = load_dataset_and_scale(target, scale_values=scale_values )<train_on_grid>
sub = best.merge(sales[["id", "item_id", "dept_id", "cat_id", "store_id", "state_id"]], on = "id")
 sub["_all_"] = "Total"
sub.shape
estimator = Ridge(tol=1e-3)
 alphas = np.logspace(1, 5, 21)
 grid = create_grid(model_metric, alphas=alphas, estimator=estimator,
cv=5, scaler_targets=scaler_targets, tidx=tidx)
 _ = grid.fit(X_tr, y_tr)
 print('Dataset scales used: ', scale_values)
 print("Best score at: %f using %s" %(grid.best_score_, grid.best_params_))<predict_on_test>
qs = np.array([0.005,0.025,0.165,0.25, 0.5, 0.75, 0.835, 0.975, 0.995])
 qs.shape
def get_ratios(coef=0.15):
qs2 = np.log(qs/(1-qs)) *coef
ratios = stats.norm.cdf(qs2)
 ratios /= ratios[4]
ratios = pd.Series(ratios, index=qs)
 return ratios.round(3)
 level_coef_dict = {"id": get_ratios(coef=0.3), "item_id": get_ratios(coef=0.15),
"dept_id": get_ratios(coef=0.08), "cat_id": get_ratios(coef=0.07),
"store_id": get_ratios(coef=0.08), "state_id": get_ratios(coef=0.07), "_all_": get_ratios(coef=0.05), 
("state_id", "item_id"): get_ratios(coef=0.19),("state_id", "dept_id"): get_ratios(coef=0.1), 
("store_id","dept_id"): get_ratios(coef=0.11),("state_id", "cat_id"): get_ratios(coef=0.08), 
("store_id","cat_id"): get_ratios(coef=0.1)
 }
pred_tr, pred_te = extract_predictions(X_tr, X_te, grid, y_tr )<predict_on_test>
def quantile_coefs(q, level):
ratios = level_coef_dict[level]
return ratios.loc[q].values
def get_group_preds(pred, level):
df = pred.groupby(level)[cols].sum()
q = np.repeat(qs, len(df))
df = pd.concat([df]*9, axis=0, sort=False)
 df.reset_index(inplace = True)
 df[cols] *= quantile_coefs(q, level)[:, None]
if level != "id":
df["id"] = [f"{lev}_X_{q:.3f}_validation" for lev, q in zip(df[level].values, q)]
else:
df["id"] = [f"{lev.replace('_validation', '')}_{q:.3f}_validation" for lev, q in zip(df[level].values, q)]
df = df[["id"]+list(cols)]
return df
def get_couple_group_preds(pred, level1, level2):
df = pred.groupby([level1, level2])[cols].sum()
q = np.repeat(qs, len(df))
df = pd.concat([df]*9, axis=0, sort=False)
 df.reset_index(inplace = True)
 df[cols] *= quantile_coefs(q,(level1, level2)) [:, None]
df["id"] = [f"{lev1}_{lev2}_{q:.3f}_validation" for lev1,lev2, q in
zip(df[level1].values,df[level2].values, q)]
df = df[["id"]+list(cols)]
return df
levels = ["id", "item_id", "dept_id", "cat_id", "store_id", "state_id", "_all_"]
couples = [("state_id", "item_id"),("state_id", "dept_id"),("store_id","dept_id"), 
("state_id", "cat_id"),("store_id","cat_id")]
cols = [f"F{i}" for i in range(1, 29)]
df = []
for level in levels :
df.append(get_group_preds(sub, level))
for level1,level2 in couples:
df.append(get_couple_group_preds(sub, level1, level2))
df = pd.concat(df, axis=0, sort=False)
 df.reset_index(drop=True, inplace=True)
 df2 = df.copy()
df_pred = create_df_pred(grid ).sort_values('mean_test_score', ascending=False)
 df_pred.head()<train_on_grid>
def quantile_coefs(q, level):
ratios = level_coef_dict[level]
return ratios.loc[q].values
def get_group_preds(pred, level):
df = pred.groupby(level)[cols].sum()
q = np.repeat(qs, len(df))
df = pd.concat([df]*9, axis=0, sort=False)
 df.reset_index(inplace = True)
 df[cols] *= quantile_coefs(q, level)[:, None]
if level != "id":
df["id"] = [f"{lev}_X_{q:.3f}_evaluation" for lev, q in zip(df[level].values, q)]
else:
df["id"] = [f"{lev.replace('_evaluation', '')}_{q:.3f}_evaluation" for lev, q in zip(df[level].values, q)]
df = df[["id"]+list(cols)]
return df
def get_couple_group_preds(pred, level1, level2):
df = pred.groupby([level1, level2])[cols].sum()
q = np.repeat(qs, len(df))
df = pd.concat([df]*9, axis=0, sort=False)
 df.reset_index(inplace = True)
 df[cols] *= quantile_coefs(q,(level1, level2)) [:, None]
df["id"] = [f"{lev1}_{lev2}_{q:.3f}_evaluation" for lev1,lev2, q in
zip(df[level1].values,df[level2].values, q)]
df = df[["id"]+list(cols)]
return df
levels = ["id", "item_id", "dept_id", "cat_id", "store_id", "state_id", "_all_"]
couples = [("state_id", "item_id"),("state_id", "dept_id"),("store_id","dept_id"), 
("state_id", "cat_id"),("store_id","cat_id")]
cols = [f"F{i}" for i in range(1, 29)]
df = []
for level in levels :
df.append(get_group_preds(sub, level))
for level1,level2 in couples:
df.append(get_couple_group_preds(sub, level1, level2))
df = pd.concat(df, axis=0, sort=False)
 df.reset_index(drop=True, inplace=True )
<define_variables><EOS> 
df = pd.concat([df2,df] , axis=0, sort=False)
 df.reset_index(drop=True, inplace=True)
 df.loc[df.index < len(df.index)//2, "id"] = df.loc[df.index < len(df.index)//2, "id"].str.replace("_evaluation$", "_validation")
 df.shape
df.head()
df.to_csv("submission.csv", index = False )