Kaggle competition: COVID19 Global Forecasting (Week 4)
lgb_train = lgb.Dataset(X_train, y_train)
 lgb_eval = lgb.Dataset(X_test, y_test)

params = {
'n_jobs': -1,
'boosting_type': 'gbdt',
"objective" : "regression",
"metric" : "rmse",
"num_leaves" : 64,
"learning_rate" : 0.005,
"bagging_fraction" : 0.7,
"feature_fraction" : 0.5,
"bagging_frequency" : 6,
"bagging_seed" : 42,
"verbosity" : 1,
"seed": 42,
}
evals_result = {}
model = lgb.train(params, lgb_train, 5000,
valid_sets=[lgb_eval],
early_stopping_rounds=1500,
verbose_eval=50,
evals_result=evals_result )<predict_on_test>
PATH ='/kaggle/input/covid19-global-forecasting-week-4'
datatrain = pd.read_csv(f'{PATH}/train.csv')
 datatest = pd.read_csv(f'{PATH}/test.csv' )
pred = model.predict(df_eval_test[feature_cols] )<prepare_output>
date = pd.to_datetime(datatrain["Date"])
 datet = pd.to_datetime(datatest["Date"])
 print(date)
df_eval_test['pred_qty'] = pred<prepare_output>
ldate = int(len(date))
ldatet = int(len(datet))
print("Length of training- date is", ldate)
 print("Length of test- date is", ldatet )
predictions = df_eval_test[['id', 'date', 'pred_qty']]
predictions = pd.pivot(predictions, index = 'id', columns = 'date', values = 'pred_qty' ).reset_index()
predictions<drop_column>
m = []
d = []
for i in range(0,ldate):
dx =(date[i].strftime("%d"))
mx =(date[i].strftime("%m"))
m.append(int(mx))
d.append(int(dx))
mt = []
dt = []
for i in range(0,ldatet):
dtx =(datet[i].strftime("%d"))
mtx =(datet[i].strftime("%m"))
mt.append(int(mtx))
dt.append(int(dtx))
predictions = predictions.drop(predictions.columns[1], axis=1 )<rename_columns>
train = datatrain
test = datatest
predictions.columns = ['id'] + ['F' + str(i + 1)for i in range(28)] 
<prepare_x_and_y>
train.insert(6,"Month",m,False)
 train.insert(7,"Day",d,False)
 test.insert(4,"Month",mt,False)
 test.insert(5,"Day",dt,False )
x = 2744099 + 1 - 853720
df_val = df_eval[x:]<create_dataframe>
print("Datatrain")
 traindays = datatrain['Date'].nunique()
print("Number of Country_Region: ", datatrain['Country_Region'].nunique())
 print("Number of Province_State: ", datatrain['Province_State'].nunique())
 print("Number of Days: ", traindays)
 notrain = datatrain['Id'].nunique()
print("Number of datapoints in train:", notrain)
 lotrain = int(notrain/traindays)
 print("L Trains:", lotrain)
predictions_v = df_val[['id', 'date', 'qty']]
predictions_v = pd.pivot(predictions_v, index = 'id', columns = 'date', values = 'qty' ).reset_index()<feature_engineering>
print("Datatest")
 testdays = datatest['Date'].nunique()
print("Number of Days: ", testdays)
 notest = datatest['ForecastId'].nunique()
print("Number of datapoints in test:", notest)
 lotest = int(notest/testdays)
 print("L Test:", lotest)
predictions_v['id'] = predictions['id'].apply(lambda x: x.replace('evaluation', 'validation'))<rename_columns>
zt = datet[0]
daycount = []
for i in range(0,lotrain):
for j in range(1,traindays+1):
daycount.append(j)
predictions_v.columns = ['id'] + ['F' + str(i + 1)for i in range(28)]<concatenate>
for i in range(traindays):
if(zt == date[i]):
zx = i
print(zx)
 daytest = []
for i in range(0,lotest):
for j in range(1,testdays+1):
jr = zx + j
daytest.append(jr)
predictions_concat = pd.concat([predictions, predictions_v], axis=0 )<save_to_csv>
train.insert(8,"DayCount",daycount,False)
 test.insert(6,"DayCount",daytest,False )
predictions_concat.to_csv('submission.csv', index=False )<load_from_csv>
traincount = int(len(train["Date"]))
testcount = int(len(test["Date"]))
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' )<prepare_x_and_y>
train.Province_State = train.Province_State.fillna(0)
 empty = 0
for i in range(0,traincount):
if(train.Province_State[i] == empty):
train.Province_State[i] = train.Country_Region[i]
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()<categorify>
test.Province_State = test.Province_State.fillna(0)
 empty = 0
for i in range(0,testcount):
if(test.Province_State[i] == empty):
test.Province_State[i] = test.Country_Region[i]
for i in range(1942,1970):
col = 'd_' + str(i)
 steval[col] = 0
steval[col] = steval[col].astype(np.int16 )<categorify>
label = preprocessing.LabelEncoder()
train.Country_Region = label.fit_transform(train.Country_Region)
 train.Province_State = label.fit_transform(train.Province_State )
sales = pd.melt(steval, id_vars=['id', 'item_id', 'dept_id', 'cat_id', 'store_id', 'state_id'], var_name='d', value_name='sold' ).dropna()<merge>
test.Country_Region = label.fit_transform(test.Country_Region)
 test.Province_State = label.fit_transform(test.Province_State)
sales = pd.merge(sales, cal, on='d', how='left')
 sales = pd.merge(sales, price, on=['store_id','item_id','wm_yr_wk'], how='left' )<define_variables>
X = np.c_[train["Province_State"], train["Country_Region"], train["DayCount"], train["Month"], train["Day"]]
Xt = np.c_[test["Province_State"], test["Country_Region"], test["DayCount"], test["Month"], test["Day"]]
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))<data_type_conversions>
Y1 = train["ConfirmedCases"]
Y2 = train["Fatalities"]
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<drop_column>
def rmsle(y_true, y_pred):
return mean_squared_log_error(y_true, y_pred)**(1/2);
sales.drop('date',axis=1,inplace=True )<categorify>
Regressor_Concases = XGBRegressor(n_estimators = 1000, gamma = 0, learning_rate = 0.05,
random_state = 42 , max_depth = 20)
 Regressor_fatalities = XGBRegressor(n_estimators = 1000 , gamma = 0, learning_rate = 0.05,
random_state = 42 , max_depth = 20 )
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 )<data_type_conversions>
Regressor_Concases.fit(X,Y1.ravel())
 A1 = Regressor_Concases.predict(X)
 A1=abs(A1)
 Y1=abs(Y1)
 B1 = mean_squared_error(A1,Y1)
 B2 = mean_squared_log_error(A1,Y1)
 print("-------Confirmed Case---------s")
 print("Training - Mean Squared Error is: ",B1)
 print("Training - Mean Squared LOG Error is: ",B2)
 print("Training - ROOT Mean Squared LOG Error is: ",rmsle(A1,Y1))
display(A1.shape)
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)
<prepare_x_and_y>
ypred = Regressor_Concases.predict(Xt)
 ypred = pd.DataFrame({'ConfirmedCases' : ypred})
 ypred = round(ypred)
 ypred.head(20 )
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()<data_type_conversions>
Regressor_fatalities.fit(X,Y2.ravel())
 ypred2= Regressor_fatalities.predict(Xt)
 A2 = Regressor_fatalities.predict(X)
 A2 = np.round(A2)
 A2=abs(A2)
 Y2=abs(Y2)
 C1 = mean_squared_error(A2,Y2)
 C2 = mean_squared_log_error(A2,Y2)
 print("--------Fatalities-------")
 print("Training - Mean Squared Error is: ",C1)
 print("Training - Mean Squared LOG Error is: ",C2)
 print("Training - ROOT Mean Squared LOG Error is: ",rmsle(A2,Y2))
display(A2.shape )
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 )<data_type_conversions>
ypred2 = pd.DataFrame({'Fatalities' : ypred2})
 ypred2 = round(ypred2)
 ypred2.head(20 )
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 )<set_options>
final_pred = pd.DataFrame()
forecast = test["ForecastId"]
gc.collect()<data_type_conversions>
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 )<filter>
final_pred.insert(0,"ForecastId",forecast,False)
 final_pred.insert(1,"ConfirmedCases",ypred,False)
 final_pred.insert(2, "Fatalities",ypred2,False )
sales = sales[sales['d']>=32]<set_options>
Regressor_Concases = XGBRegressor(n_estimators = 1000, gamma = 0, learning_rate = 0.5,
random_state = 42 ,max_depth = 20)
 Regressor_fatalities = XGBRegressor(n_estimators = 1000 , gamma = 0, learning_rate = 0.5,
random_state = 42 , max_depth = 20)
gc.collect()<load_pretrained>
Regressor_Concases.fit(X,Y1.ravel())
 A1 = Regressor_Concases.predict(X)
 A1=abs(A1)
 Y1=abs(Y1)
 B1 = mean_squared_error(A1,Y1)
 B2 = mean_squared_log_error(A1,Y1)
 print("-------Confirmed Case---------s")
 print("Training - Mean Squared Error is: ",B1)
 print("Training - Mean Squared LOG Error is: ",B2)
 print("Training - ROOT Mean Squared LOG Error is: ",rmsle(A1,Y1))
display(A1.shape)
 ypred = Regressor_Concases.predict(Xt)
 ypred = pd.DataFrame({'ConfirmedCases' : ypred})
 ypred = round(ypred)
 ypred.head(20)
 Regressor_fatalities.fit(X,Y2.ravel())
 ypred2= Regressor_fatalities.predict(Xt)
 A2 = Regressor_fatalities.predict(X)
 A2 = np.round(A2)
 A2=abs(A2)
 Y2=abs(Y2)
 C1 = mean_squared_error(A2,Y2)
 C2 = mean_squared_log_error(A2,Y2)
 print("--------Fatalities-------")
 print("Training - Mean Squared Error is: ",C1)
 print("Training - Mean Squared LOG Error is: ",C2)
 print("Training - ROOT Mean Squared LOG Error is: ",rmsle(A2,Y2))
display(A2.shape)
 ypred2 = pd.DataFrame({'Fatalities' : ypred2})
 ypred2 = round(ypred2)
 ypred2.head(20)
sales.to_pickle('salesdata.pkl')
 del sales<set_options>
Regressor_Concases = XGBRegressor(n_estimators = 1000, gamma = 0, learning_rate = 0.01,
random_state = 42 , max_depth = 20)
 Regressor_fatalities = XGBRegressor(n_estimators = 1000 , gamma = 0, learning_rate = 0.01,
random_state = 42 , max_depth = 20)
 Regressor_Concases.fit(X,Y1.ravel())
 A1 = Regressor_Concases.predict(X)
 A1=abs(A1)
 Y1=abs(Y1)
 B1 = mean_squared_error(A1,Y1)
 B2 = mean_squared_log_error(A1,Y1)
 print("-------Confirmed Case---------s")
 print("Training - Mean Squared Error is: ",B1)
 print("Training - Mean Squared LOG Error is: ",B2)
 print("Training - ROOT Mean Squared LOG Error is: ",rmsle(A1,Y1))
display(A1.shape)
 ypred = Regressor_Concases.predict(Xt)
 ypred = pd.DataFrame({'ConfirmedCases' : ypred})
 ypred = round(ypred)
 ypred.head(20)
 Regressor_fatalities.fit(X,Y2.ravel())
 ypred2= Regressor_fatalities.predict(Xt)
 A2 = Regressor_fatalities.predict(X)
 A2 = np.round(A2)
 A2=abs(A2)
 Y2=abs(Y2)
 C1 = mean_squared_error(A2,Y2)
 C2 = mean_squared_log_error(A2,Y2)
 print("--------Fatalities-------")
 print("Training - Mean Squared Error is: ",C1)
 print("Training - Mean Squared LOG Error is: ",C2)
 print("Training - ROOT Mean Squared LOG Error is: ",rmsle(A2,Y2))
display(A2.shape)
 ypred2 = pd.DataFrame({'Fatalities' : ypred2})
 ypred2 = round(ypred2)
 ypred2.head(20 )
gc.collect()<categorify>
Regressor_Concases = XGBRegressor(n_estimators = 1000, gamma = 0, learning_rate = 0.99,
random_state = 42 , max_depth = 20)
 Regressor_fatalities = XGBRegressor(n_estimators = 1000 , gamma = 0, learning_rate = 0.99,
random_state = 42 , max_depth = 20)
 Regressor_Concases.fit(X,Y1.ravel())
 A1 = Regressor_Concases.predict(X)
 A1=abs(A1)
 Y1=abs(Y1)
 B1 = mean_squared_error(A1,Y1)
 B2 = mean_squared_log_error(A1,Y1)
 print("-------Confirmed Case---------s")
 print("Training - Mean Squared Error is: ",B1)
 print("Training - Mean Squared LOG Error is: ",B2)
 print("Training - ROOT Mean Squared LOG Error is: ",rmsle(A1,Y1))
display(A1.shape)
 ypred = Regressor_Concases.predict(Xt)
 ypred = pd.DataFrame({'ConfirmedCases' : ypred})
 ypred = round(ypred)
 ypred.head(20)
 Regressor_fatalities.fit(X,Y2.ravel())
 ypred2= Regressor_fatalities.predict(Xt)
 A2 = Regressor_fatalities.predict(X)
 A2 = np.round(A2)
 A2=abs(A2)
 Y2=abs(Y2)
 C1 = mean_squared_error(A2,Y2)
 C2 = mean_squared_log_error(A2,Y2)
 print("--------Fatalities-------")
 print("Training - Mean Squared Error is: ",C1)
 print("Training - Mean Squared LOG Error is: ",C2)
 print("Training - ROOT Mean Squared LOG Error is: ",rmsle(A2,Y2))
display(A2.shape)
 ypred2 = pd.DataFrame({'Fatalities' : ypred2})
 ypred2 = round(ypred2)
 ypred2.head(20 )
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']<set_options>
Regressor_Concases = XGBRegressor(n_estimators = 10, gamma = 0, learning_rate = 0.99,
random_state = 42 , max_depth = 20)
 Regressor_fatalities = XGBRegressor(n_estimators = 10 , gamma = 0, learning_rate = 0.99,
random_state = 42 , max_depth = 20)
 Regressor_Concases.fit(X,Y1.ravel())
 A1 = Regressor_Concases.predict(X)
 A1=abs(A1)
 Y1=abs(Y1)
 B1 = mean_squared_error(A1,Y1)
 B2 = mean_squared_log_error(A1,Y1)
 print("-------Confirmed Case---------s")
 print("Training - Mean Squared Error is: ",B1)
 print("Training - Mean Squared LOG Error is: ",B2)
 print("Training - ROOT Mean Squared LOG Error is: ",rmsle(A1,Y1))
display(A1.shape)
 ypred = Regressor_Concases.predict(Xt)
 ypred = pd.DataFrame({'ConfirmedCases' : ypred})
 ypred = round(ypred)
 ypred.head(20)
 Regressor_fatalities.fit(X,Y2.ravel())
 ypred2= Regressor_fatalities.predict(Xt)
 A2 = Regressor_fatalities.predict(X)
 A2 = np.round(A2)
 A2=abs(A2)
 Y2=abs(Y2)
 C1 = mean_squared_error(A2,Y2)
 C2 = mean_squared_log_error(A2,Y2)
 print("--------Fatalities-------")
 print("Training - Mean Squared Error is: ",C1)
 print("Training - Mean Squared LOG Error is: ",C2)
 print("Training - ROOT Mean Squared LOG Error is: ",rmsle(A2,Y2))
display(A2.shape)
 ypred2 = pd.DataFrame({'Fatalities' : ypred2})
 ypred2 = round(ypred2)
 ypred2.head(20 )
gc.collect()<set_options>
Regressor_Concases = XGBRegressor(n_estimators = 2500, gamma = 0, learning_rate = 0.99,
random_state = 42 , max_depth = 20)
 Regressor_fatalities = XGBRegressor(n_estimators = 2500 , gamma = 0, learning_rate = 0.99,
random_state = 42 , max_depth = 20)
 Regressor_Concases.fit(X,Y1.ravel())
 A1 = Regressor_Concases.predict(X)
 A1=abs(A1)
 Y1=abs(Y1)
 B1 = mean_squared_error(A1,Y1)
 B2 = mean_squared_log_error(A1,Y1)
 print("-------Confirmed Case---------s")
 print("Training - Mean Squared Error is: ",B1)
 print("Training - Mean Squared LOG Error is: ",B2)
 print("Training - ROOT Mean Squared LOG Error is: ",rmsle(A1,Y1))
display(A1.shape)
 ypred = Regressor_Concases.predict(Xt)
 ypred = pd.DataFrame({'ConfirmedCases' : ypred})
 ypred = round(ypred)
 ypred.head(20)
 Regressor_fatalities.fit(X,Y2.ravel())
 ypred2= Regressor_fatalities.predict(Xt)
 A2 = Regressor_fatalities.predict(X)
 A2 = np.round(A2)
 A2=abs(A2)
 Y2=abs(Y2)
 C1 = mean_squared_error(A2,Y2)
 C2 = mean_squared_log_error(A2,Y2)
 print("--------Fatalities-------")
 print("Training - Mean Squared Error is: ",C1)
 print("Training - Mean Squared LOG Error is: ",C2)
 print("Training - ROOT Mean Squared LOG Error is: ",rmsle(A2,Y2))
display(A2.shape)
 ypred2 = pd.DataFrame({'Fatalities' : ypred2})
 ypred2 = round(ypred2)
 ypred2.head(20 )
gc.collect()<prepare_x_and_y>
Regressor_Concases = XGBRegressor(n_estimators = 5000, gamma = 0, learning_rate = 0.99,
random_state = 42 , max_depth = 20)
 Regressor_fatalities = XGBRegressor(n_estimators = 5000 , gamma = 0, learning_rate = 0.99,
random_state = 42 , max_depth = 20)
 Regressor_Concases.fit(X,Y1.ravel())
 A1 = Regressor_Concases.predict(X)
 A1=abs(A1)
 Y1=abs(Y1)
 B1 = mean_squared_error(A1,Y1)
 B2 = mean_squared_log_error(A1,Y1)
 print("-------Confirmed Case---------s")
 print("Training - Mean Squared Error is: ",B1)
 print("Training - Mean Squared LOG Error is: ",B2)
 print("Training - ROOT Mean Squared LOG Error is: ",rmsle(A1,Y1))
display(A1.shape)
 ypred = Regressor_Concases.predict(Xt)
 ypred = pd.DataFrame({'ConfirmedCases' : ypred})
 ypred = round(ypred)
 ypred.head(20)
 Regressor_fatalities.fit(X,Y2.ravel())
 ypred2= Regressor_fatalities.predict(Xt)
 A2 = Regressor_fatalities.predict(X)
 A2 = np.round(A2)
 A2=abs(A2)
 Y2=abs(Y2)
 C1 = mean_squared_error(A2,Y2)
 C2 = mean_squared_log_error(A2,Y2)
 print("--------Fatalities-------")
 print("Training - Mean Squared Error is: ",C1)
 print("Training - Mean Squared LOG Error is: ",C2)
 print("Training - ROOT Mean Squared LOG Error is: ",rmsle(A2,Y2))
display(A2.shape)
 ypred2 = pd.DataFrame({'Fatalities' : ypred2})
 ypred2 = round(ypred2)
 ypred2.head(20 )
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 )<set_options>
Regressor_Concases = XGBRegressor(n_estimators = 2500, gamma = 0, learning_rate = 0.99,
random_state = 42 , max_depth = 2)
 Regressor_fatalities = XGBRegressor(n_estimators = 2500 , gamma = 0, learning_rate = 0.99, r
andom_state = 42 , max_depth = 2)
 Regressor_Concases.fit(X,Y1.ravel())
 A1 = Regressor_Concases.predict(X)
 A1=abs(A1)
 Y1=abs(Y1)
 B1 = mean_squared_error(A1,Y1)
 B2 = mean_squared_log_error(A1,Y1)
 print("-------Confirmed Case---------s")
 print("Training - Mean Squared Error is: ",B1)
 print("Training - Mean Squared LOG Error is: ",B2)
 print("Training - ROOT Mean Squared LOG Error is: ",rmsle(A1,Y1))
display(A1.shape)
 ypred = Regressor_Concases.predict(Xt)
 ypred = pd.DataFrame({'ConfirmedCases' : ypred})
 ypred = round(ypred)
 ypred.head(20)
 Regressor_fatalities.fit(X,Y2.ravel())
 ypred2= Regressor_fatalities.predict(Xt)
 A2 = Regressor_fatalities.predict(X)
 A2 = np.round(A2)
 A2=abs(A2)
 Y2=abs(Y2)
 C1 = mean_squared_error(A2,Y2)
 C2 = mean_squared_log_error(A2,Y2)
 print("--------Fatalities-------")
 print("Training - Mean Squared Error is: ",C1)
 print("Training - Mean Squared LOG Error is: ",C2)
 print("Training - ROOT Mean Squared LOG Error is: ",rmsle(A2,Y2))
display(A2.shape)
 ypred2 = pd.DataFrame({'Fatalities' : ypred2})
 ypred2 = round(ypred2)
 ypred2.head(20 )
gc.collect()<init_hyperparams>
Regressor_Concases = XGBRegressor(n_estimators = 2500, gamma = 0, learning_rate = 0.99,
random_state = 42 , max_depth = 200)
 Regressor_fatalities = XGBRegressor(n_estimators = 2500 , gamma = 0, learning_rate = 0.99,
random_state = 42 , max_depth = 200)
 Regressor_Concases.fit(X,Y1.ravel())
 A1 = Regressor_Concases.predict(X)
 A1=abs(A1)
 Y1=abs(Y1)
 B1 = mean_squared_error(A1,Y1)
 B2 = mean_squared_log_error(A1,Y1)
 print("-------Confirmed Case---------s")
 print("Training - Mean Squared Error is: ",B1)
 print("Training - Mean Squared LOG Error is: ",B2)
 print("Training - ROOT Mean Squared LOG Error is: ",rmsle(A1,Y1))
display(A1.shape)
 ypred = Regressor_Concases.predict(Xt)
 ypred = pd.DataFrame({'ConfirmedCases' : ypred})
 ypred = round(ypred)
 ypred.head(20)
 Regressor_fatalities.fit(X,Y2.ravel())
 ypred2= Regressor_fatalities.predict(Xt)
 A2 = Regressor_fatalities.predict(X)
 A2 = np.round(A2)
 A2=abs(A2)
 Y2=abs(Y2)
 C1 = mean_squared_error(A2,Y2)
 C2 = mean_squared_log_error(A2,Y2)
 print("--------Fatalities-------")
 print("Training - Mean Squared Error is: ",C1)
 print("Training - Mean Squared LOG Error is: ",C2)
 print("Training - ROOT Mean Squared LOG Error is: ",rmsle(A2,Y2))
display(A2.shape)
 ypred2 = pd.DataFrame({'Fatalities' : ypred2})
 ypred2 = round(ypred2)
 ypred2.head(20 )
valgrid = {'n_estimators':hp.quniform('n_estimators', 900, 1200, 100),
'learning_rate':hp.quniform('learning_rate', 0.1, 0.4, 0.1),
'max_depth':hp.quniform('max_depth', 4,8,1),
'num_leaves':hp.quniform('num_leaves', 25,75,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', 200, 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 )<set_options>
Regressor_Concases = XGBRegressor(n_estimators = 2500, gamma = 0, learning_rate = 0.99,
random_state = 42 , max_depth = 50)
 Regressor_fatalities = XGBRegressor(n_estimators = 2500 , gamma = 0, learning_rate = 0.99,
random_state = 42 , max_depth = 50)
 Regressor_Concases.fit(X,Y1.ravel())
 A1 = Regressor_Concases.predict(X)
 A1=abs(A1)
 Y1=abs(Y1)
 B1 = mean_squared_error(A1,Y1)
 B2 = mean_squared_log_error(A1,Y1)
 print("-------Confirmed Case---------s")
 print("Training - Mean Squared Error is: ",B1)
 print("Training - Mean Squared LOG Error is: ",B2)
 print("Training - ROOT Mean Squared LOG Error is: ",rmsle(A1,Y1))
display(A1.shape)
 ypred = Regressor_Concases.predict(Xt)
 ypred = pd.DataFrame({'ConfirmedCases' : ypred})
 ypred = round(ypred)
 ypred.head(20)
 Regressor_fatalities.fit(X,Y2.ravel())
 ypred2= Regressor_fatalities.predict(Xt)
 A2 = Regressor_fatalities.predict(X)
 A2 = np.round(A2)
 A2=abs(A2)
 Y2=abs(Y2)
 C1 = mean_squared_error(A2,Y2)
 C2 = mean_squared_log_error(A2,Y2)
 print("--------Fatalities-------")
 print("Training - Mean Squared Error is: ",C1)
 print("Training - Mean Squared LOG Error is: ",C2)
 print("Training - ROOT Mean Squared LOG Error is: ",rmsle(A2,Y2))
display(A2.shape)
 ypred2 = pd.DataFrame({'Fatalities' : ypred2})
 ypred2 = round(ypred2)
 ypred2.head(20 )
gc.collect()<train_model>
Regressor_Concases = XGBRegressor(n_estimators = 2500, gamma = 0, learning_rate = 0.99,
random_state = 42 , max_depth = 30)
 Regressor_fatalities = XGBRegressor(n_estimators = 2500 , gamma = 0, learning_rate = 0.99,
random_state = 42 , max_depth = 30)
 Regressor_Concases.fit(X,Y1.ravel())
 A1 = Regressor_Concases.predict(X)
 A1=abs(A1)
 Y1=abs(Y1)
 B1 = mean_squared_error(A1,Y1)
 B2 = mean_squared_log_error(A1,Y1)
 print("-------Confirmed Case---------s")
 print("Training - Mean Squared Error is: ",B1)
 print("Training - Mean Squared LOG Error is: ",B2)
 print("Training - ROOT Mean Squared LOG Error is: ",rmsle(A1,Y1))
display(A1.shape)
 ypred = Regressor_Concases.predict(Xt)
 ypred = pd.DataFrame({'ConfirmedCases' : ypred})
 ypred = round(ypred)
 ypred.head(20)
 Regressor_fatalities.fit(X,Y2.ravel())
 ypred2= Regressor_fatalities.predict(Xt)
 A2 = Regressor_fatalities.predict(X)
 A2 = np.round(A2)
 A2=abs(A2)
 Y2=abs(Y2)
 C1 = mean_squared_error(A2,Y2)
 C2 = mean_squared_log_error(A2,Y2)
 print("--------Fatalities-------")
 print("Training - Mean Squared Error is: ",C1)
 print("Training - Mean Squared LOG Error is: ",C2)
 print("Training - ROOT Mean Squared LOG Error is: ",rmsle(A2,Y2))
display(A2.shape)
 ypred2 = pd.DataFrame({'Fatalities' : ypred2})
 ypred2 = round(ypred2)
 ypred2.head(20 )
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'<train_model>
Regressor_Concases = XGBRegressor(n_estimators = 2500, gamma = 0, learning_rate = 0.99,
random_state = 42 ,min_child_weight=100, max_depth = 50)
 Regressor_fatalities = XGBRegressor(n_estimators = 2500 , gamma = 0, learning_rate = 0.99,
random_state = 42 ,min_child_weight=100, max_depth = 50)
 Regressor_Concases.fit(X,Y1.ravel())
 A1 = Regressor_Concases.predict(X)
 A1=abs(A1)
 Y1=abs(Y1)
 B1 = mean_squared_error(A1,Y1)
 B2 = mean_squared_log_error(A1,Y1)
 print("-------Confirmed Case---------s")
 print("Training - Mean Squared Error is: ",B1)
 print("Training - Mean Squared LOG Error is: ",B2)
 print("Training - ROOT Mean Squared LOG Error is: ",rmsle(A1,Y1))
display(A1.shape)
 ypred = Regressor_Concases.predict(Xt)
 ypred = pd.DataFrame({'ConfirmedCases' : ypred})
 ypred = round(ypred)
 ypred.head(20)
 Regressor_fatalities.fit(X,Y2.ravel())
 ypred2= Regressor_fatalities.predict(Xt)
 A2 = Regressor_fatalities.predict(X)
 A2 = np.round(A2)
 A2=abs(A2)
 Y2=abs(Y2)
 C1 = mean_squared_error(A2,Y2)
 C2 = mean_squared_log_error(A2,Y2)
 print("--------Fatalities-------")
 print("Training - Mean Squared Error is: ",C1)
 print("Training - Mean Squared LOG Error is: ",C2)
 print("Training - ROOT Mean Squared LOG Error is: ",rmsle(A2,Y2))
display(A2.shape)
 ypred2 = pd.DataFrame({'Fatalities' : ypred2})
 ypred2 = round(ypred2)
 ypred2.head(20 )
joblib.dump(model, filename)
 del model, X_train, y_train, X_valid, y_valid
gc.collect()<feature_engineering>
Regressor_Concases = XGBRegressor(n_estimators = 2500, gamma = 0, learning_rate = 0.99,
random_state = 42 ,min_child_weight=10, max_depth = 30)
 Regressor_fatalities = XGBRegressor(n_estimators = 2500 , gamma = 0, learning_rate = 0.99,
random_state = 42 ,min_child_weight=10, max_depth = 30)
 Regressor_Concases.fit(X,Y1.ravel())
 A1 = Regressor_Concases.predict(X)
 A1=abs(A1)
 Y1=abs(Y1)
 B1 = mean_squared_error(A1,Y1)
 B2 = mean_squared_log_error(A1,Y1)
 print("-------Confirmed Case---------s")
 print("Training - Mean Squared Error is: ",B1)
 print("Training - Mean Squared LOG Error is: ",B2)
 print("Training - ROOT Mean Squared LOG Error is: ",rmsle(A1,Y1))
display(A1.shape)
 ypred = Regressor_Concases.predict(Xt)
 ypred = pd.DataFrame({'ConfirmedCases' : ypred})
 ypred = round(ypred)
 ypred.head(20)
 Regressor_fatalities.fit(X,Y2.ravel())
 ypred2= Regressor_fatalities.predict(Xt)
 A2 = Regressor_fatalities.predict(X)
 A2 = np.round(A2)
 A2=abs(A2)
 Y2=abs(Y2)
 C1 = mean_squared_error(A2,Y2)
 C2 = mean_squared_log_error(A2,Y2)
 print("--------Fatalities-------")
 print("Training - Mean Squared Error is: ",C1)
 print("Training - Mean Squared LOG Error is: ",C2)
 print("Training - ROOT Mean Squared LOG Error is: ",rmsle(A2,Y2))
display(A2.shape)
 ypred2 = pd.DataFrame({'Fatalities' : ypred2})
 ypred2 = round(ypred2)
 ypred2.head(20 )
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 )<set_options>
Regressor_Concases = XGBRegressor(n_estimators = 2500, gamma = 0, learning_rate = 0.99,
random_state = 42 ,min_child_weight=1,min_split_loss=10, max_depth = 50)
 Regressor_fatalities = XGBRegressor(n_estimators = 2500 , gamma = 0, learning_rate = 0.99,
random_state = 42 ,min_child_weight=1,min_split_loss=10, max_depth = 50)
 Regressor_Concases.fit(X,Y1.ravel())
 A1 = Regressor_Concases.predict(X)
 A1=abs(A1)
 Y1=abs(Y1)
 B1 = mean_squared_error(A1,Y1)
 B2 = mean_squared_log_error(A1,Y1)
 print("-------Confirmed Case---------s")
 print("Training - Mean Squared Error is: ",B1)
 print("Training - Mean Squared LOG Error is: ",B2)
 print("Training - ROOT Mean Squared LOG Error is: ",rmsle(A1,Y1))
display(A1.shape)
 ypred = Regressor_Concases.predict(Xt)
 ypred = pd.DataFrame({'ConfirmedCases' : ypred})
 ypred = round(ypred)
 ypred.head(20)
 Regressor_fatalities.fit(X,Y2.ravel())
 ypred2= Regressor_fatalities.predict(Xt)
 A2 = Regressor_fatalities.predict(X)
 A2 = np.round(A2)
 A2=abs(A2)
 Y2=abs(Y2)
 C1 = mean_squared_error(A2,Y2)
 C2 = mean_squared_log_error(A2,Y2)
 print("--------Fatalities-------")
 print("Training - Mean Squared Error is: ",C1)
 print("Training - Mean Squared LOG Error is: ",C2)
 print("Training - ROOT Mean Squared LOG Error is: ",rmsle(A2,Y2))
display(A2.shape)
 ypred2 = pd.DataFrame({'Fatalities' : ypred2})
 ypred2 = round(ypred2)
 ypred2.head(20 )
gc.collect()<concatenate>
Regressor_Concases = XGBRegressor(n_estimators = 2500, gamma = 0, learning_rate = 0.99,
random_state = 42 ,min_child_weight=1,min_split_loss=0, max_depth = 50)
 Regressor_fatalities = XGBRegressor(n_estimators = 2500 , gamma = 0, learning_rate = 0.99,
random_state = 42 ,min_child_weight=1,min_split_loss=0, max_depth = 50)
 Regressor_Concases.fit(X,Y1.ravel())
 A1 = Regressor_Concases.predict(X)
 A1=abs(A1)
 Y1=abs(Y1)
 B1 = mean_squared_error(A1,Y1)
 B2 = mean_squared_log_error(A1,Y1)
 print("-------Confirmed Case---------s")
 print("Training - Mean Squared Error is: ",B1)
 print("Training - Mean Squared LOG Error is: ",B2)
 print("Training - ROOT Mean Squared LOG Error is: ",rmsle(A1,Y1))
display(A1.shape)
 ypred = Regressor_Concases.predict(Xt)
 ypred = pd.DataFrame({'ConfirmedCases' : ypred})
 ypred = round(ypred)
 ypred.head(20)
 Regressor_fatalities.fit(X,Y2.ravel())
 ypred2= Regressor_fatalities.predict(Xt)
 A2 = Regressor_fatalities.predict(X)
 A2 = np.round(A2)
 A2=abs(A2)
 Y2=abs(Y2)
 C1 = mean_squared_error(A2,Y2)
 C2 = mean_squared_log_error(A2,Y2)
 print("--------Fatalities-------")
 print("Training - Mean Squared Error is: ",C1)
 print("Training - Mean Squared LOG Error is: ",C2)
 print("Training - ROOT Mean Squared LOG Error is: ",rmsle(A2,Y2))
display(A2.shape)
 ypred2 = pd.DataFrame({'Fatalities' : ypred2})
 ypred2 = round(ypred2)
 ypred2.head(20 )
submit = pd.concat([validation,evaluation] ).reset_index(drop=True)
 submit.head()<save_to_csv>
Regressor_Concases = XGBRegressor(n_estimators = 2500, gamma = 0, learning_rate = 0.99,
reg_lambda=0, random_state = 42 ,min_child_weight=1,
min_split_loss=0, max_depth = 50)
 Regressor_fatalities = XGBRegressor(n_estimators = 2500 , gamma = 0, learning_rate = 0.99,
reg_lambda=0, random_state = 42 ,min_child_weight=1,
min_split_loss=0, max_depth = 50)
 Regressor_Concases.fit(X,Y1.ravel())
 A1 = Regressor_Concases.predict(X)
 A1=abs(A1)
 Y1=abs(Y1)
 B1 = mean_squared_error(A1,Y1)
 B2 = mean_squared_log_error(A1,Y1)
 print("-------Confirmed Case---------s")
 print("Training - Mean Squared Error is: ",B1)
 print("Training - Mean Squared LOG Error is: ",B2)
 print("Training - ROOT Mean Squared LOG Error is: ",rmsle(A1,Y1))
display(A1.shape)
 ypred = Regressor_Concases.predict(Xt)
 ypred = pd.DataFrame({'ConfirmedCases' : ypred})
 ypred = round(ypred)
 ypred.head(20)
 Regressor_fatalities.fit(X,Y2.ravel())
 ypred2= Regressor_fatalities.predict(Xt)
 A2 = Regressor_fatalities.predict(X)
 A2 = np.round(A2)
 A2=abs(A2)
 Y2=abs(Y2)
 C1 = mean_squared_error(A2,Y2)
 C2 = mean_squared_log_error(A2,Y2)
 print("--------Fatalities-------")
 print("Training - Mean Squared Error is: ",C1)
 print("Training - Mean Squared LOG Error is: ",C2)
 print("Training - ROOT Mean Squared LOG Error is: ",rmsle(A2,Y2))
display(A2.shape)
 ypred2 = pd.DataFrame({'Fatalities' : ypred2})
 ypred2 = round(ypred2)
 ypred2.head(20 )
submit.to_csv('submission.csv',index=False)
 print("Submission successful" )<import_modules>
Regressor_Concases = XGBRegressor(n_estimators = 2500, gamma = 0, learning_rate = 0.99,
reg_lambda=1, random_state = 42 ,min_child_weight=1,
min_split_loss=0, max_depth = 50)
 Regressor_fatalities = XGBRegressor(n_estimators = 2500 , gamma = 0, learning_rate = 0.99,
reg_lambda=1, random_state = 42 ,min_child_weight=1,
min_split_loss=0, max_depth = 50)
 Regressor_Concases.fit(X,Y1.ravel())
 A1 = Regressor_Concases.predict(X)
 A1=abs(A1)
 Y1=abs(Y1)
 B1 = mean_squared_error(A1,Y1)
 B2 = mean_squared_log_error(A1,Y1)
 print("-------Confirmed Case---------s")
 print("Training - Mean Squared Error is: ",B1)
 print("Training - Mean Squared LOG Error is: ",B2)
 print("Training - ROOT Mean Squared LOG Error is: ",rmsle(A1,Y1))
display(A1.shape)
 ypred = Regressor_Concases.predict(Xt)
 ypred = pd.DataFrame({'ConfirmedCases' : ypred})
 ypred = round(ypred)
 ypred.head(20)
 Regressor_fatalities.fit(X,Y2.ravel())
 ypred2= Regressor_fatalities.predict(Xt)
 A2 = Regressor_fatalities.predict(X)
 A2 = np.round(A2)
 A2=abs(A2)
 Y2=abs(Y2)
 C1 = mean_squared_error(A2,Y2)
 C2 = mean_squared_log_error(A2,Y2)
 print("--------Fatalities-------")
 print("Training - Mean Squared Error is: ",C1)
 print("Training - Mean Squared LOG Error is: ",C2)
 print("Training - ROOT Mean Squared LOG Error is: ",rmsle(A2,Y2))
display(A2.shape)
 ypred2 = pd.DataFrame({'Fatalities' : ypred2})
 ypred2 = round(ypred2)
 ypred2.head(20 )
from datetime import datetime, timedelta
import gc
import numpy as np, pandas as pd
import lightgbm as lgb<define_variables>
Regressor_Concases = XGBRegressor(n_estimators = 2500, gamma = 0, learning_rate = 0.99,
reg_lambda=0,reg_alpha=1, random_state = 42 ,min_child_weight=1,
min_split_loss=0, max_depth = 50)
 Regressor_fatalities = XGBRegressor(n_estimators = 2500 , gamma = 0, learning_rate = 0.99,
reg_lambda=0,reg_alpha=1, random_state = 42 ,min_child_weight=1,
min_split_loss=0, max_depth = 50)
 Regressor_Concases.fit(X,Y1.ravel())
 A1 = Regressor_Concases.predict(X)
 A1=abs(A1)
 Y1=abs(Y1)
 B1 = mean_squared_error(A1,Y1)
 B2 = mean_squared_log_error(A1,Y1)
 print("-------Confirmed Case---------s")
 print("Training - Mean Squared Error is: ",B1)
 print("Training - Mean Squared LOG Error is: ",B2)
 print("Training - ROOT Mean Squared LOG Error is: ",rmsle(A1,Y1))
display(A1.shape)
 ypred = Regressor_Concases.predict(Xt)
 ypred = pd.DataFrame({'ConfirmedCases' : ypred})
 ypred = round(ypred)
 ypred.head(20)
 Regressor_fatalities.fit(X,Y2.ravel())
 ypred2= Regressor_fatalities.predict(Xt)
 A2 = Regressor_fatalities.predict(X)
 A2 = np.round(A2)
 A2=abs(A2)
 Y2=abs(Y2)
 C1 = mean_squared_error(A2,Y2)
 C2 = mean_squared_log_error(A2,Y2)
 print("--------Fatalities-------")
 print("Training - Mean Squared Error is: ",C1)
 print("Training - Mean Squared LOG Error is: ",C2)
 print("Training - ROOT Mean Squared LOG Error is: ",rmsle(A2,Y2))
display(A2.shape)
 ypred2 = pd.DataFrame({'Fatalities' : ypred2})
 ypred2 = round(ypred2)
 ypred2.head(20 )
CAL_DTYPES={"event_name_1": "category", "event_name_2": "category", "event_type_1": "category",
"event_type_2": "category", "weekday": "category", 'wm_yr_wk': 'int16', "wday": "int16",
"month": "int16", "year": "int16", "snap_CA": "float32", 'snap_TX': 'float32', 'snap_WI': 'float32' }
PRICE_DTYPES = {"store_id": "category", "item_id": "category", "wm_yr_wk": "int16","sell_price":"float32" }<set_options>
Regressor_Concases = XGBRegressor(n_estimators = 2500, gamma = 0, learning_rate = 0.99,
reg_lambda=1,reg_alpha=0, random_state = 42 ,min_child_weight=1,
min_split_loss=0, max_depth = 50)
 Regressor_fatalities = XGBRegressor(n_estimators = 2500 , gamma = 0, learning_rate = 0.99,
reg_lambda=1,reg_alpha=0, random_state = 42 ,min_child_weight=1,
min_split_loss=0, max_depth = 50)
 Regressor_Concases.fit(X,Y1.ravel())
 A1 = Regressor_Concases.predict(X)
 A1=abs(A1)
 Y1=abs(Y1)
 B1 = mean_squared_error(A1,Y1)
 B2 = mean_squared_log_error(A1,Y1)
 print("-------Confirmed Case---------s")
 print("Training - Mean Squared Error is: ",B1)
 print("Training - Mean Squared LOG Error is: ",B2)
 print("Training - ROOT Mean Squared LOG Error is: ",rmsle(A1,Y1))
display(A1.shape)
 ypred = Regressor_Concases.predict(Xt)
 ypred = pd.DataFrame({'ConfirmedCases' : ypred})
 ypred = round(ypred)
 ypred.head(20)
 Regressor_fatalities.fit(X,Y2.ravel())
 ypred2= Regressor_fatalities.predict(Xt)
 A2 = Regressor_fatalities.predict(X)
 A2 = np.round(A2)
 A2=abs(A2)
 Y2=abs(Y2)
 C1 = mean_squared_error(A2,Y2)
 C2 = mean_squared_log_error(A2,Y2)
 print("--------Fatalities-------")
 print("Training - Mean Squared Error is: ",C1)
 print("Training - Mean Squared LOG Error is: ",C2)
 print("Training - ROOT Mean Squared LOG Error is: ",rmsle(A2,Y2))
display(A2.shape)
 ypred2 = pd.DataFrame({'Fatalities' : ypred2})
 ypred2 = round(ypred2)
 ypred2.head(20 )
pd.options.display.max_columns = 50<define_variables>
Regressor_Concases = XGBRegressor(n_estimators = 2500, gamma = 0,subsample=0, learning_rate = 0.99,
reg_lambda=1,reg_alpha=0, random_state = 42 ,min_child_weight=1,
min_split_loss=0, max_depth = 50)
 Regressor_fatalities = XGBRegressor(n_estimators = 2500 , gamma = 0,subsample=0, learning_rate = 0.99,
reg_lambda=1,reg_alpha=0, random_state = 42 ,min_child_weight=1,
min_split_loss=0, max_depth = 50)
 Regressor_Concases.fit(X,Y1.ravel())
 A1 = Regressor_Concases.predict(X)
 A1=abs(A1)
 Y1=abs(Y1)
 B1 = mean_squared_error(A1,Y1)
 B2 = mean_squared_log_error(A1,Y1)
 print("-------Confirmed Case---------s")
 print("Training - Mean Squared Error is: ",B1)
 print("Training - Mean Squared LOG Error is: ",B2)
 print("Training - ROOT Mean Squared LOG Error is: ",rmsle(A1,Y1))
display(A1.shape)
 ypred = Regressor_Concases.predict(Xt)
 ypred = pd.DataFrame({'ConfirmedCases' : ypred})
 ypred = round(ypred)
 ypred.head(20)
 Regressor_fatalities.fit(X,Y2.ravel())
 ypred2= Regressor_fatalities.predict(Xt)
 A2 = Regressor_fatalities.predict(X)
 A2 = np.round(A2)
 A2=abs(A2)
 Y2=abs(Y2)
 C1 = mean_squared_error(A2,Y2)
 C2 = mean_squared_log_error(A2,Y2)
 print("--------Fatalities-------")
 print("Training - Mean Squared Error is: ",C1)
 print("Training - Mean Squared LOG Error is: ",C2)
 print("Training - ROOT Mean Squared LOG Error is: ",rmsle(A2,Y2))
display(A2.shape)
 ypred2 = pd.DataFrame({'Fatalities' : ypred2})
 ypred2 = round(ypred2)
 ypred2.head(20 )
h = 28
max_lags = 57
tr_last = 1941
fday = datetime(2016,4, 25)
 fday<load_from_csv>
Regressor_Concases = XGBRegressor(n_estimators = 2500, gamma = 0,subsample=0.5, learning_rate = 0.99,
reg_lambda=1,reg_alpha=0, random_state = 42 ,min_child_weight=1,
min_split_loss=0, max_depth = 50)
 Regressor_fatalities = XGBRegressor(n_estimators = 2500 , gamma = 0,subsample=0.5,
learning_rate = 0.99,reg_lambda=1,reg_alpha=0,
random_state = 42 ,min_child_weight=1,min_split_loss=0, max_depth = 50)
 Regressor_Concases.fit(X,Y1.ravel())
 A1 = Regressor_Concases.predict(X)
 A1=abs(A1)
 Y1=abs(Y1)
 B1 = mean_squared_error(A1,Y1)
 B2 = mean_squared_log_error(A1,Y1)
 print("-------Confirmed Case---------s")
 print("Training - Mean Squared Error is: ",B1)
 print("Training - Mean Squared LOG Error is: ",B2)
 print("Training - ROOT Mean Squared LOG Error is: ",rmsle(A1,Y1))
display(A1.shape)
 ypred = Regressor_Concases.predict(Xt)
 ypred = pd.DataFrame({'ConfirmedCases' : ypred})
 ypred = round(ypred)
 ypred.head(20)
 Regressor_fatalities.fit(X,Y2.ravel())
 ypred2= Regressor_fatalities.predict(Xt)
 A2 = Regressor_fatalities.predict(X)
 A2 = np.round(A2)
 A2=abs(A2)
 Y2=abs(Y2)
 C1 = mean_squared_error(A2,Y2)
 C2 = mean_squared_log_error(A2,Y2)
 print("--------Fatalities-------")
 print("Training - Mean Squared Error is: ",C1)
 print("Training - Mean Squared LOG Error is: ",C2)
 print("Training - ROOT Mean Squared LOG Error is: ",rmsle(A2,Y2))
display(A2.shape)
 ypred2 = pd.DataFrame({'Fatalities' : ypred2})
 ypred2 = round(ypred2)
 ypred2.head(20 )
def create_dt(is_train = True, nrows = None, first_day = 1):
prices = pd.read_csv(".. /input/m5-forecasting-accuracy/sell_prices.csv", dtype = PRICE_DTYPES)
 for col, col_dtype in PRICE_DTYPES.items() :
if col_dtype == "category":
prices[col] = prices[col].cat.codes.astype("int16")
 prices[col] -= prices[col].min()
cal = pd.read_csv(".. /input/m5-forecasting-accuracy/calendar.csv", dtype = CAL_DTYPES)
 cal["date"] = pd.to_datetime(cal["date"])
 for col, col_dtype in CAL_DTYPES.items() :
if col_dtype == "category":
cal[col] = cal[col].cat.codes.astype("int16")
 cal[col] -= cal[col].min()
start_day = max(1 if is_train else tr_last-max_lags, first_day)
 numcols = [f"d_{day}" for day in range(start_day,tr_last+1)]
catcols = ['id', 'item_id', 'dept_id','store_id', 'cat_id', 'state_id']
dtype = {numcol:"float32" for numcol in numcols}
dtype.update({col: "category" for col in catcols if col != "id"})
 dt = pd.read_csv(".. /input/m5-forecasting-accuracy/sales_train_evaluation.csv",
nrows = nrows, usecols = catcols + numcols, dtype = dtype)
 for col in catcols:
if col != "id":
dt[col] = dt[col].cat.codes.astype("int16")
 dt[col] -= dt[col].min()
if not is_train:
for day in range(tr_last+1, tr_last+ 28 +1):
dt[f"d_{day}"] = np.nan
dt = pd.melt(dt,
id_vars = catcols,
value_vars = [col for col in dt.columns if col.startswith("d_")],
var_name = "d",
value_name = "sales")
 dt = dt.merge(cal, on= "d", copy = False)
 dt = dt.merge(prices, on = ["store_id", "item_id", "wm_yr_wk"], copy = False)
 return dt<categorify>
final_pred.to_csv('submission.csv', index=False )
def create_fea(dt):
lags = [7, 28]
lag_cols = [f"lag_{lag}" for lag in lags ]
for lag, lag_col in zip(lags, lag_cols):
dt[lag_col] = dt[["id","sales"]].groupby("id")["sales"].shift(lag)
 wins = [7, 28]
for win in wins :
for lag,lag_col in zip(lags, lag_cols):
dt[f"rmean_{lag}_{win}"] = dt[["id", lag_col]].groupby("id")[lag_col].transform(lambda x : x.rolling(win ).mean())
 date_features = {
"wday": "weekday",
"week": "weekofyear",
"month": "month",
"quarter": "quarter",
"year": "year",
"mday": "day",
}
for date_feat_name, date_feat_func in date_features.items() :
if date_feat_name in dt.columns:
dt[date_feat_name] = dt[date_feat_name].astype("int16")
 else:
dt[date_feat_name] = getattr(dt["date"].dt, date_feat_func ).astype("int16" )<define_variables>
print("End" )