Kaggle competition: Corporación Favorita Grocery Sales Forecasting
model4.fit(X_train2, y_train2 )<compute_test_metric>
dir_raw = ".. /input/favorita-grocery-sales-forecasting/"
dir_prep = ".. /input/preparing-data-for-lgbm-or-something-else/"
dir_1st_result = ".. /input/stage-2a-the-first-half/"
dir_2nd_result = ".. /input/stage-2b-the-second-half/"
dirs = [dir_raw, dir_prep, dir_1st_result, dir_2nd_result]
model3.score(X_test1,y_test1 )<compute_test_metric>
val2.columns = pd.Index([str(i)for i in range(8,16)])
 val2.head()
model4.score(X_test2,y_test2 )<import_modules>
val_pred = pd.concat([val1,val2],axis=1)
 print(val_pred.shape)
 val_pred.head()
import xgboost as xgb<choose_model_class>
y_val = pd.read_csv(dir_prep + 'y_val.csv')
 print(y_val.shape)
 y_val.head()
model5=xgb.XGBRegressor()<choose_model_class>
items = pd.read_csv(dir_raw + 'items.csv' ).set_index("item_nbr")
 stores_items = pd.read_csv(dir_prep + 'stores_items.csv', index_col=['store_nbr','item_nbr'])
 items = items.reindex(stores_items.index.get_level_values(1))
print(items.shape)
 items.head()
model6=xgb.XGBRegressor()<rename_columns>
n_public = 5
weights=pd.concat([items["perishable"]])* 0.25 + 1
print("Unweighted validation mse: ", mean_squared_error(
 np.array(y_val), np.array(val_pred)))
 print("Full validation mse: ", mean_squared_error(
 np.array(y_val), np.array(val_pred), sample_weight=weights))
print("'Public' validation mse: ", mean_squared_error(
 np.array(y_val)[:,:n_public], np.array(val_pred)[:,:n_public], sample_weight=weights))
print("'Private' validation mse: ", mean_squared_error(
 np.array(y_val)[:,n_public:], np.array(val_pred)[:,n_public:], sample_weight=weights))
X_train1.rename(columns = {'Id':'ForecastId'}, inplace = True)
 X_test1.rename(columns = {'Id':'ForecastId'}, inplace = True )<rename_columns>
sub1 = pd.read_csv(dir_1st_result+'nn_sub_first_half.csv', index_col='id')
 print(sub1.shape)
 sub1.head()
X_train2.rename(columns = {'Id':'ForecastId'}, inplace = True)
 X_test2.rename(columns = {'Id':'ForecastId'}, inplace = True )<train_model>
sub2 = pd.read_csv(dir_2nd_result+'nn_sub_second_half.csv', index_col='id')
 print(sub2.shape)
 sub2.head()
model5.fit(X_train1, y_train1 )<train_model>
submax = sub1.join(sub2, rsuffix='_2' ).max(axis=1)
 submax.head()
model6.fit(X_train2, y_train2 )<compute_test_metric>
submax.name = 'unit_sales'
sub = pd.DataFrame(submax ).reset_index()
print(sub.shape)
 sub.head()
model5.score(X_test1,y_test1 )<compute_test_metric>
test = pd.read_csv('.. /input/favorita-grocery-sales-forecasting/test.csv',
parse_dates=['date'], index_col=0)
 test.head()
model6.score(X_test2,y_test2 )<save_to_csv>
check_df = test[['date']].join(sub.set_index("id"))
check_df.head()
ank=model5.predict(x_predfinal)
 annk=model6.predict(x_predfinal)
 a4=np.around(ank)
 a4=a4.astype(int)
 a5=np.around(annk)
 a5=a5.astype(int)
 predictions2 = pd.DataFrame({'ForecastId':x_test['ForecastId'],'ConfirmedCases':a4,'Fatalities':a5})
 predictions2.to_csv('submission.csv', header=True, index=False)
 predictions2.head()<import_modules>
public_dates = ['2017-08-16', '2017-08-17', '2017-08-18', '2017-08-19', '2017-08-20']
private_public_dows = ['2017-08-23', '2017-08-24', '2017-08-25', '2017-08-26', '2017-08-27']
public_dates_nohol = ['2017-08-16', '2017-08-18', '2017-08-19', '2017-08-20']
private_public_dows_nohol = ['2017-08-23', '2017-08-25', '2017-08-26', '2017-08-27']
early_private_dates = ['2017-08-21', '2017-08-22']
private_private_dows = ['2017-08-28', '2017-08-29']
last_private_dates = ['2017-08-30', '2017-08-31']
public_last_dows = ['2017-08-16', '2017-08-17']
from sklearn.metrics import mean_squared_error<import_modules>
def corr_results(results, earlier_dates, later_dates):
d1 = results[results.date.isin(earlier_dates)].reset_index() [['unit_sales']]
d2 = results[results.date.isin(later_dates)].reset_index() [['unit_sales']]
d = pd.concat([d1, d2], axis=1)
 print(d.corr() )
from sklearn.metrics import mean_squared_error<predict_on_test>
print('
Comparing results for public test data with subsequent week...')
 compare_results(check_df, public_dates, private_public_dows)
 print('
Comparing results for public test data with non-holidays in subsequent week...')
 compare_results(check_df, public_dates_nohol, private_public_dows_nohol)
 print('
Comparing results for early private test data with a week later...')
 compare_results(check_df, early_private_dates, private_private_dows)
 print('
Comparing results for end of private data with corresponding days in public data...')
 compare_results(check_df, public_last_dows, last_private_dates )
y_predmodel5=model5.predict(X_test1 )<compute_test_metric>
print('
Correlating results for public test data with subsequent week...')
 corr_results(check_df, public_dates, private_public_dows)
 print('
Correlating results for public test data with non-holidays in subsequent week...')
 corr_results(check_df, public_dates_nohol, private_public_dows_nohol)
 print('
Correlating results for early private test data with a week later...')
 corr_results(check_df, early_private_dates, private_private_dows)
 print('
Correlating results for end of private data with corresponding days in public data...')
 corr_results(check_df, public_last_dows, last_private_dates )
<import_modules><EOS> 
sub.to_csv('nn_sub_combined.csv', float_format='%.5f', index=None )