Kaggle competition: M5 Forecasting - Accuracy
<SOS> metric: M5_WRMSSE Kaggle data source: m5-forecasting-accuracy<train_model>
cal = pd.read_csv('/kaggle/input/m5-forecasting-accuracy/calendar.csv')
 steval = pd.read_csv('/kaggle/input/m5-forecasting-accuracy/sales_train_evaluation.csv')
 price = pd.read_csv('/kaggle/input/m5-forecasting-accuracy/sell_prices.csv' )
lasso = make_pipeline(RobustScaler() , Lasso(alpha =0.0005, random_state=1))
lasso.fit(X_train_scaled, y_train)
 print('R-squared score(training): {:.3f}'.format(lasso.score(X_train_scaled, y_train)))
 print('R-squared score(test): {:.3f}'.format(lasso.score(X_test_scaled, y_test)))
<train_model>
id_list = sorted(list(set(steval['id'])))
 d_cols = [col for col in steval.columns if 'd_' in col]
x_1 = steval.loc[steval['id'] == id_list[0]].set_index('id')[d_cols].values[0][:200]
x_2 = steval.loc[steval['id'] == id_list[12]].set_index('id')[d_cols].values[0][300:500]
x_3 = steval.loc[steval['id'] == id_list[36]].set_index('id')[d_cols].values[0][600:800]
x_4 = steval.loc[steval['id'] == id_list[64]].set_index('id')[d_cols].values[0][1000:1200]
x_5 = steval.loc[steval['id'] == id_list[128]].set_index('id')[d_cols].values[0][1300:1500]
x_6 = steval.loc[steval['id'] == id_list[256]].set_index('id')[d_cols].values[0][1600:1800]
fig = make_subplots(rows=6, cols=1)
 fig.add_trace(go.Scatter(x=np.arange(len(x_1)) , y=x_1, showlegend=True,
mode='lines', name="1st sample",
marker=dict(color="cadetblue")) , row=1, col=1)
 fig.add_trace(go.Scatter(x=np.arange(len(x_2)) , y=x_2, showlegend=True,
mode='lines', name="2nd sample",
marker=dict(color="firebrick")) , row=2, col=1)
 fig.add_trace(go.Scatter(x=np.arange(len(x_3)) , y=x_3, showlegend=True,
mode='lines', name="3rd sample",
marker=dict(color="yellowgreen")) , row=3, col=1)
 fig.add_trace(go.Scatter(x=np.arange(len(x_4)) , y=x_4, showlegend=True,
mode='lines', name="4th sample",
marker=dict(color="orangered")) , row=4, col=1)
 fig.add_trace(go.Scatter(x=np.arange(len(x_5)) , y=x_5, showlegend=True,
mode='lines', name="5th sample",
marker=dict(color="mediumpurple")) , row=5, col=1)
 fig.add_trace(go.Scatter(x=np.arange(len(x_6)) , y=x_6, showlegend=True,
mode='lines', name="6th sample",
marker=dict(color="navy")) , row=6, col=1)
 fig.update_layout(height=1200, width=900, title_text="Observing some randomly chosen sales trend :")
 fig.show()
GBoost = GradientBoostingRegressor(n_estimators=3000, learning_rate=0.05,
max_depth=4, max_features='sqrt',
min_samples_leaf=15, min_samples_split=10,
loss='huber', random_state =5)
 GBoost.fit(X_train_final, y_train_final)
 y_pred = GBoost.predict(df_Test)
 y_pred<train_model>
for i in range(1942,1970):
col = 'd_' + str(i)
 steval[col] = 0
steval[col] = steval[col].astype(np.int16 )
regressor = RandomForestClassifier()
regressor.fit(X_train_scaled, y_train)
 print('R-squared score(training): {:.3f}'.format(regressor.score(X_train_scaled, y_train)))
 print('R-squared score(test): {:.3f}'.format(regressor.score(X_test_scaled, y_test)))
<define_search_space>
sales = pd.melt(steval, id_vars=['id', 'item_id', 'dept_id', 'cat_id', 'store_id', 'state_id'], var_name='d', value_name='sold' ).dropna()
<train_model>
sales = pd.merge(sales, cal, on='d', how='left')
 sales = pd.merge(sales, price, on=['store_id','item_id','wm_yr_wk'], how='left' )
regressor = RandomForestClassifier(bootstrap='False', max_depth=15, min_samples_leaf=4,n_estimators=900, verbose=5)
<load_from_csv>
d_id = dict(zip(sales.id.cat.codes, sales.id))
d_item_id = dict(zip(sales.item_id.cat.codes, sales.item_id))
d_dept_id = dict(zip(sales.dept_id.cat.codes, sales.dept_id))
d_cat_id = dict(zip(sales.cat_id.cat.codes, sales.cat_id))
d_store_id = dict(zip(sales.store_id.cat.codes, sales.store_id))
d_state_id = dict(zip(sales.state_id.cat.codes, sales.state_id))
<define_search_model>
sales.d = sales['d'].apply(lambda x: x.split('_')[1] ).astype(np.int16)
 cols = sales.dtypes.index.tolist()
types = sales.dtypes.values.tolist()
for i,type in enumerate(types):
if type.name == 'category':
sales[cols[i]] = sales[cols[i]].cat.codes
regressor=xgboost.XGBRegressor()<define_search_space>
sales.drop('date',axis=1,inplace=True )
<choose_model_class>
lags = [1,2,4,8,16,32]
for lag in lags:
sales['sold_lag_'+str(lag)] = sales.groupby(['id', 'item_id', 'dept_id', 'cat_id', 'store_id', 'state_id'],as_index=False)['sold'].shift(lag ).astype(np.float16 )
regressor = xgboost.XGBRegressor(base_score=0.25, booster='gbtree', colsample_bylevel=1,
colsample_bynode=1, colsample_bytree=1, gamma=0, gpu_id=-1,
importance_type='gain', interaction_constraints='',
learning_rate=0.1, max_delta_step=0, max_depth=2,
min_child_weight=1, missing=None, monotone_constraints='() ',
n_estimators=900, n_jobs=0, num_parallel_tree=1, random_state=0,
reg_alpha=0, reg_lambda=1, scale_pos_weight=1, subsample=1,
tree_method='exact', validate_parameters=1, verbosity=None )<train_model>
sales['item_sold_avg'] = sales.groupby('item_id')['sold'].transform('mean' ).astype(np.float16)
 sales['state_sold_avg'] = sales.groupby('state_id')['sold'].transform('mean' ).astype(np.float16)
 sales['store_sold_avg'] = sales.groupby('store_id')['sold'].transform('mean' ).astype(np.float16)
 sales['cat_sold_avg'] = sales.groupby('cat_id')['sold'].transform('mean' ).astype(np.float16)
 sales['dept_sold_avg'] = sales.groupby('dept_id')['sold'].transform('mean' ).astype(np.float16)
 sales['cat_dept_sold_avg'] = sales.groupby(['cat_id','dept_id'])['sold'].transform('mean' ).astype(np.float16)
 sales['store_item_sold_avg'] = sales.groupby(['store_id','item_id'])['sold'].transform('mean' ).astype(np.float16)
 sales['cat_item_sold_avg'] = sales.groupby(['cat_id','item_id'])['sold'].transform('mean' ).astype(np.float16)
 sales['dept_item_sold_avg'] = sales.groupby(['dept_id','item_id'])['sold'].transform('mean' ).astype(np.float16)
 sales['dept_store_sold_avg'] = sales.groupby(['dept_id','store_id'])['sold'].transform('mean' ).astype(np.float16)
 sales['store_cat_dept_sold_avg'] = sales.groupby(['store_id','cat_id','dept_id'])['sold'].transform('mean' ).astype(np.float16)
 sales['store_cat_item_sold_avg'] = sales.groupby(['store_id','cat_id','item_id'])['sold'].transform('mean' ).astype(np.float16)
regressor.fit(X_train_final,y_train_final )<load_from_csv>
id_list = sorted(list(set(sales['id'])))
 sold_avg_cols = [col for col in sales.columns if '_sold_avg' in col]
x_1 = sales.loc[sales['id'] == id_list[0]].set_index('id')[sold_avg_cols].values[0][:]
x_2 = sales.loc[sales['id'] == id_list[12]].set_index('id')[sold_avg_cols].values[0][:]
x_3 = sales.loc[sales['id'] == id_list[36]].set_index('id')[sold_avg_cols].values[0][:]
x_4 = sales.loc[sales['id'] == id_list[64]].set_index('id')[sold_avg_cols].values[0][:]
x_5 = sales.loc[sales['id'] == id_list[128]].set_index('id')[sold_avg_cols].values[0][:]
x_6 = sales.loc[sales['id'] == id_list[256]].set_index('id')[sold_avg_cols].values[0][:]
fig = make_subplots(rows=6, cols=1)
 fig.add_trace(go.Scatter(x=np.arange(len(x_1)) , y=x_1, showlegend=True,
mode='lines+markers', name="First sample",
marker=dict(color="cadetblue")) , row=1, col=1)
 fig.add_trace(go.Scatter(x=np.arange(len(x_2)) , y=x_2, showlegend=True,
mode='lines+markers', name="Second sample",
marker=dict(color="firebrick")) , row=2, col=1)
 fig.add_trace(go.Scatter(x=np.arange(len(x_3)) , y=x_3, showlegend=True,
mode='lines+markers', name="Third sample",
marker=dict(color="yellowgreen")) , row=3, col=1)
 fig.add_trace(go.Scatter(x=np.arange(len(x_4)) , y=x_4, showlegend=True,
mode='lines+markers', name="Fourth sample",
marker=dict(color="orangered")) , row=4, col=1)
 fig.add_trace(go.Scatter(x=np.arange(len(x_5)) , y=x_5, showlegend=True,
mode='lines+markers', name="Fifth sample",
marker=dict(color="mediumpurple")) , row=5, col=1)
 fig.add_trace(go.Scatter(x=np.arange(len(x_6)) , y=x_6, showlegend=True,
mode='lines+markers', name="Sixth sample",
marker=dict(color="navy")) , row=6, col=1)
 fig.update_layout(height=1500, width=900, title_text="Observing some randomly chosen '_sold_avg' trends after applying lag:")
 fig.show()
pred=pd.DataFrame(y_pred)
 samp = pd.read_csv('.. /input/house-prices-advanced-regression-techniques/test.csv')
 sub = pd.concat([samp['Id'],pred], axis=1)
 sub.columns=['Id','SalePrice']<save_to_csv>
sales['rolling_sold_mean'] = sales.groupby(['id', 'item_id', 'dept_id', 'cat_id', 'store_id', 'state_id'])['sold'].transform(lambda x: x.rolling(window=6 ).mean() ).astype(np.float16 )
sub.to_csv('My_sub1.csv',index=False )<set_options>
sales['expanding_sold_mean'] = sales.groupby(['id', 'item_id', 'dept_id', 'cat_id', 'store_id', 'state_id'])['sold'].transform(lambda x: x.expanding(2 ).mean() ).astype(np.float16 )
%matplotlib inline
%config InlineBackend.figure_format = 'retina'
pd.options.display.max_rows = 100<load_from_csv>
gc.collect()
train = pd.read_csv('/kaggle/input/house-prices-advanced-regression-techniques/train.csv')
 test = pd.read_csv('/kaggle/input/house-prices-advanced-regression-techniques/test.csv')
 sample_submission = pd.read_csv('/kaggle/input/house-prices-advanced-regression-techniques/sample_submission.csv' )<prepare_x_and_y>
sales['daily_avg_sold'] = sales.groupby(['id', 'item_id', 'dept_id', 'cat_id', 'store_id', 'state_id','d'])['sold'].transform('mean' ).astype(np.float16)
 sales['avg_sold'] = sales.groupby(['id', 'item_id', 'dept_id', 'cat_id', 'store_id', 'state_id'])['sold'].transform('mean' ).astype(np.float16)
 sales['selling_trend'] =(sales['daily_avg_sold'] - sales['avg_sold'] ).astype(np.float16)
 sales.drop(['daily_avg_sold','avg_sold'],axis=1,inplace=True )
y_test = sample_submission.SalePrice<prepare_x_and_y>
sales = sales[sales['d']>=32]
train_target = train.SalePrice<drop_column>
gc.collect()
train = train.drop(['Id', 'SalePrice'], axis=1)
 test = test.drop(['Id'],axis=1 )<concatenate>
sales.to_pickle('salesdata.pkl')
 del sales
data = pd.concat([train, test] )<define_variables>
gc.collect()
cat_feat = list(data.dtypes[data.dtypes == object].index )<categorify>
data = pd.read_pickle('salesdata.pkl')
 validation = data[(data['d']>=1914)&(data['d']<1942)][['id','d','sold']]
test = data[data['d']>=1942][['id','d','sold']]
eval_prediction = test['sold']
validation_prediction = validation['sold']
data[cat_feat] = data[cat_feat].fillna('nan')
 num_feat = [f for f in data if f not in(cat_feat + ['Id', 'SalePrice'])]<count_unique_values>
gc.collect()
cat_nunique = data[cat_feat].nunique()
print(cat_nunique)
 cat_feat = list(cat_nunique[cat_nunique < 30].index )<categorify>
gc.collect()
dummy_data = pd.get_dummies(data[cat_feat], columns=cat_feat)
 dummy_cols = list(set(dummy_data))
dummy_data = dummy_data[dummy_cols]
data = pd.concat([data[num_feat].fillna(-999),
dummy_data], axis=1)
<split>
X_train, y_train = df[df['d']<1914].drop('sold',axis=1), df[df['d']<1914]['sold']
X_valid, y_valid = df[(df['d']>=1914)&(df['d']<1942)].drop('sold',axis=1), df[(df['d']>=1914)&(df['d']<1942)]['sold']
X_test = df[df['d']>=1942].drop('sold',axis=1 )
train = data.iloc[:train.shape[0], :]
test = data.iloc[train.shape[0]:, :]<prepare_x_and_y>
gc.collect()
X_train = train
y_train = train_target
X_test = test
y_test = sample_submission.SalePrice<train_model>
%%time
valgrid = {'n_estimators':hp.quniform('n_estimators', 900, 1500, 100),
'learning_rate':hp.quniform('learning_rate', 0.01, 0.4, 0.01),
'max_depth':hp.quniform('max_depth', 3,10,1),
'num_leaves':hp.quniform('num_leaves', 25,100,25),
'subsample':hp.quniform('subsample', 0.5, 0.9, 0.1),
'colsample_bytree':hp.quniform('colsample_bytree', 0.5, 0.9, 0.1),
'min_child_weight':hp.quniform('min_child_weight', 100, 500, 100)
 }
def objective(params):
params = {'n_estimators': int(params['n_estimators']),
'learning_rate': params['learning_rate'],
'max_depth': int(params['max_depth']),
'num_leaves': int(params['num_leaves']),
'subsample': params['subsample'],
'colsample_bytree': params['colsample_bytree'],
'min_child_weight': params['min_child_weight']}
lgb_a = lgb(**params)
 score = cross_val_score(lgb_a, X_train, y_train, cv=2, n_jobs=-1 ).mean()
return score
bestP = fmin(fn= objective, space= valgrid, max_evals=20, rstate=np.random.RandomState(123), algo=tpe.suggest )
params = {'n_estimators': 130,
'learning_rate': 0.01,
'max_depth': 3,
'min_child_weight': 1,
'subsample': 1,
'colsample_bytree': 1,
'objective': 'reg:linear',
'n_jobs': 4}
clf_xgb = xgb.XGBRegressor(**params)
 clf_xgb.fit(X_train, y_train, eval_metric='mae', eval_set=[(X_train, y_train),(X_test, y_test)] )<import_modules>
gc.collect()
from hyperopt import hp, fmin, tpe, STATUS_OK, Trials<train_on_grid>
print(bestP)
 print('N estimators:', int(bestP['n_estimators']))
print('Learning rate:', bestP['learning_rate'])
 print('Subsample:', bestP['subsample'])
 print('Colsample bytree:', bestP['colsample_bytree'])
 print('Max depth:', int(bestP['max_depth']))
print('Num leaves:', int(bestP['num_leaves']))
print('Min child weight:', int(bestP['min_child_weight']))
def score(params):
params['max_depth'] = int(params['max_depth'])
 params['n_jobs'] = -1
print("Training with params : ", params)
 clf = xgb.XGBRegressor(**params)
 clf.fit(X_train, y_train)
 y_pred_xgb_test = clf.predict(X_test)
 mae = mean_absolute_error(y_test, y_pred_xgb_test)
 result = {'loss': mae, 'status': STATUS_OK}
print('TEST ROC AUC: {0:.4f}'.format(mae))
return result
space = {
'max_depth' : hp.choice('max_depth', range(1, 15)) ,
'n_estimators': hp.choice('n_estimators', range(100, 1000)) ,
'eta': hp.quniform('eta', 0.025, 0.5, 0.10),
'max_depth': hp.choice('max_depth', np.arange(1, 14, dtype=int)) ,
'min_child_weight': hp.quniform('min_child_weight', 1, 6, 0.5),
'subsample': hp.quniform('subsample', 0.5, 1, 0.05),
'gamma': hp.quniform('gamma', 0.5, 1, 0.05),
'colsample_bytree': hp.quniform('colsample_bytree', 0.5, 1, 0.05),
'eval_metric': 'mae',
'objective': 'reg:linear',
'nthread': 4,
'booster': 'gbtree',
'tree_method': 'exact',
'silent': 1
}
trials = Trials()
best = fmin(score, space, algo=tpe.suggest, trials=trials, max_evals=20 )<find_best_score>
%%time
model = lightgbm.LGBMRegressor(
 n_estimators = int(bestP['n_estimators']),
learning_rate = bestP['learning_rate'],
subsample = bestP['subsample'],
colsample_bytree = bestP['colsample_bytree'],
max_depth = int(bestP['max_depth']),
num_leaves = int(bestP['num_leaves']),
min_child_weight = int(bestP['min_child_weight']))
print('Prediction for Store: {}**'.format(d_store_id[store]))
model.fit(X_train, y_train, eval_set=[(X_train,y_train),(X_valid,y_valid)], eval_metric='rmse', verbose=20, early_stopping_rounds=20)
 validation_prediction[X_valid.index] = model.predict(X_valid)
 eval_prediction[X_test.index] = model.predict(X_test)
 filename = 'model'+str(d_store_id[store])+'.pkl'
trials.best_trial<train_model>
joblib.dump(model, filename)
 del model, X_train, y_train, X_valid, y_valid
gc.collect()
clf_xgb = xgb.XGBRegressor(**best)
 clf_xgb.fit(X_train, y_train, eval_metric='mae', eval_set=[(X_train, y_train),(X_test, y_test)] )<predict_on_test>
%%time
validation = steval[['id']+['d_' + str(i)for i in range(1914,1942)]]
validation['id']=pd.read_csv('/kaggle/input/m5-forecasting-accuracy/sales_train_validation.csv' ).id
validation.columns=['id'] + ['F' + str(i + 1)for i in range(28)]
test['sold'] = eval_prediction
evaluation = test[['id','d','sold']]
evaluation = pd.pivot(evaluation, index='id', columns='d', values='sold' ).reset_index()
evaluation.columns=['id'] + ['F' + str(i + 1)for i in range(28)]
evaluation.id = evaluation.id.map(d_id )
predict = clf_xgb.predict(X_test )<save_to_csv>
gc.collect()
submission = pd.DataFrame({
"Id": sample_submission["Id"],
"SalePrice": predict
})
 submission.to_csv('submission.csv', index=False )<import_modules>
submit = pd.concat([validation,evaluation] ).reset_index(drop=True)
 submit.head()
import numpy as np
import pandas as pd
from PIL import Image
import os
from random import shuffle
import matplotlib.pyplot as plt
import random<define_variables>
print("Generating CSV file")
 submit.to_csv('submission.csv',index=False)
 print("Submission Successful" )