Kaggle competition: Recruit Restaurant Visitor Forecasting
<SOS> metric: RMSLE Kaggle data source: recruit-restaurant-visitor-forecasting<install_modules>
data = {
'tra': pd.read_csv('.. /input/air_visit_data.csv'),
'as': pd.read_csv('.. /input/air_store_info.csv'),
'hs': pd.read_csv('.. /input/hpg_store_info.csv'),
'ar': pd.read_csv('.. /input/air_reserve.csv'),
'hr': pd.read_csv('.. /input/hpg_reserve.csv'),
'id': pd.read_csv('.. /input/store_id_relation.csv'),
'tes': pd.read_csv('.. /input/sample_submission.csv'),
'hol': pd.read_csv('.. /input/date_info.csv' ).rename(columns={'calendar_date':'visit_date'})
 }
deps_path = '.. /input/kagglejigsaw2019deps/kaggle-jigsaw-2019-deps/kaggle-jigsaw-2019-deps/'
! pip install -v --no-cache-dir --global-option="--cpp_ext" --global-option="--cuda_ext" $deps_path/apex
! pip install $deps_path/pytorch-pretrained-BERT/<define_variables>
data['hr'] = pd.merge(data['hr'], data['id'], how='inner', on=['hpg_store_id'] )
test_size = 0<install_modules>
data['hr'].isnull().values.any()
! python.. /input/jigsaw2019code/bert.py \
_runs/gpt2-ep2-lr8e-5 \
--bucket 0 \
--model.. /input/gpt2ep2lr8e5/gpt2-ep2-lr8e-5/gpt2-ep2-lr8e-5/ \
--submission --test-size $test_size<load_from_csv>
for df in ['ar','hr']:
data[df]['visit_datetime'] = pd.to_datetime(data[df]['visit_datetime'])
 data[df]['visit_datetime'] = data[df]['visit_datetime'].dt.date
data[df]['reserve_datetime'] = pd.to_datetime(data[df]['reserve_datetime'])
 data[df]['reserve_datetime'] = data[df]['reserve_datetime'].dt.date
data[df]['reserve_datetime_diff'] = data[df].apply(lambda r:(r['visit_datetime'] - r['reserve_datetime'] ).days, axis=1 )
bert_uncased1 = pd.read_csv('_runs/bert-base-uncased-pretrained-ep1/submission.csv')['prediction']
bert_cased1 = pd.read_csv('_runs/bert-base-cased-pretrained-ep1/submission.csv')['prediction']
bert_uncased_fresh1 = pd.read_csv('_runs/bert-base-uncased-fresh-ep1/submission.csv')['prediction']
bert_large1 = pd.read_csv('_runs/resume-lr0.1e-5-from-bert-large-pretrained-uncased-ep1-lr0.5e-5-as16/submission.csv')['prediction']<prepare_output>
for df in ['ar','hr']:
tmp1 = data[df].groupby(['air_store_id','visit_datetime'], as_index=False)[['reserve_datetime_diff', 'reserve_visitors']].sum().rename(columns={'visit_datetime':'visit_date', 'reserve_visitors':'rv1'})
 tmp2 = data[df].groupby(['air_store_id','visit_datetime'], as_index=False)[['reserve_datetime_diff', 'reserve_visitors']].mean().rename(columns={'visit_datetime':'visit_date', 'reserve_visitors':'rv2'})
 data[df] = pd.merge(tmp1, tmp2, how='inner', on=['air_store_id','visit_date'] )
test_model_preds['bert_uncased1'] = bert_uncased1
test_model_preds['bert_cased1'] = bert_cased1
test_model_preds['bert_uncased_fresh1'] = bert_uncased_fresh1
test_model_preds['bert_large1'] = bert_large1<load_from_csv>
data['tra']['visit_date'] = pd.to_datetime(data['tra']['visit_date'])
 data['tra']['dow'] = data['tra']['visit_date'].dt.dayofweek
data['tra']['year'] = data['tra']['visit_date'].dt.year
data['tra']['month'] = data['tra']['visit_date'].dt.month
data['tra']['visit_date'] = data['tra']['visit_date'].dt.date
gpt22 = pd.read_csv('_runs/gpt2-ep2-lr8e-5/submission.csv')['prediction']<prepare_output>
data['tes']['visit_date'] = data['tes']['id'].map(lambda x: str(x ).split('_')[2])
 data['tes']['air_store_id'] = data['tes']['id'].map(lambda x: '_'.join(x.split('_')[:2]))
data['tes']['visit_date'] = pd.to_datetime(data['tes']['visit_date'])
 data['tes']['dow'] = data['tes']['visit_date'].dt.dayofweek
data['tes']['year'] = data['tes']['visit_date'].dt.year
data['tes']['month'] = data['tes']['visit_date'].dt.month
data['tes']['visit_date'] = data['tes']['visit_date'].dt.date
test_model_preds['gpt22'] = gpt22<save_to_csv>
stores = pd.concat([pd.DataFrame({'air_store_id': unique_stores, 'dow': [i]*len(unique_stores)})for i in range(7)], axis=0, ignore_index=True ).reset_index(drop=True )
test_model_preds.to_csv('test_model_preds.csv', index=False )<load_from_csv>
print('Range of dow is from {} to {}'.format(min(stores.dow), max(stores.dow)) )
! cp _runs/bert-base-cased-pretrained-ep1/submission.csv bert-base-cased-pretrained-ep1.csv
! cp _runs/bert-base-uncased-pretrained-ep1/submission.csv bert-base-uncased-pretrained-ep1.csv
! cp _runs/bert-base-uncased-fresh-ep1/submission.csv bert-base-uncased-fresh-ep1.csv
! cp _runs/gpt2-ep2-lr8e-5/submission.csv gpt2-ep2-lr8e-5.csv
! cp _runs/resume-lr0.1e-5-from-bert-large-pretrained-uncased-ep1-lr0.5e-5-as16/submission.csv \
resume-lr0.1e-5-from-bert-large-pretrained-uncased-ep1-lr0.5e-5-as16.csv<find_best_model_class>
tmp = data['tra'].groupby(['air_store_id','dow'], as_index=False)['visitors'].min().rename(columns={'visitors':'min_visitors'})
 stores = pd.merge(stores, tmp, how='left', on=['air_store_id','dow'])
 tmp = data['tra'].groupby(['air_store_id','dow'], as_index=False)['visitors'].mean().rename(columns={'visitors':'mean_visitors'})
 stores = pd.merge(stores, tmp, how='left', on=['air_store_id','dow'])
 tmp = data['tra'].groupby(['air_store_id','dow'], as_index=False)['visitors'].median().rename(columns={'visitors':'median_visitors'})
 stores = pd.merge(stores, tmp, how='left', on=['air_store_id','dow'])
 tmp = data['tra'].groupby(['air_store_id','dow'], as_index=False)['visitors'].max().rename(columns={'visitors':'max_visitors'})
 stores = pd.merge(stores, tmp, how='left', on=['air_store_id','dow'])
 tmp = data['tra'].groupby(['air_store_id','dow'], as_index=False)['visitors'].count().rename(columns={'visitors':'count_observations'} )
best_weights = {
'cp': [50, 40, 40],
'clip': [40, 40, 44],
'nb': [40, 39, 44],
'tw': [40, 39, 40],
'tfidf': [42, 44, 41, 37],
'bert': [59, 30, -28, 65],
'blend': [17, 33, -13, -1, -3, 132, 42]
}
def custom_predict(X_models, weights=best_weights, type_='linear'):
model_cp = ensemble_predictions(X_models[[col for col in X_models.columns if col.startswith('lstm_cp')]], weights=(np.array(weights['cp'])+ 1e-15)/(sum(weights['cp'])+ 1e-15), type_=type_)
 model_clip = ensemble_predictions(X_models[[col for col in X_models.columns if col.startswith('lstm_clip')]], weights=(np.array(weights['clip'])+ 1e-15)/(sum(weights['clip'])+ 1e-15), type_=type_)
 model_nb = ensemble_predictions(X_models[[col for col in X_models.columns if col.startswith('lstm_nb')]], weights=(np.array(weights['nb'])+ 1e-15)/(sum(weights['nb'])+ 1e-15), type_=type_)
 model_tw = ensemble_predictions(X_models[[col for col in X_models.columns if col.startswith('lstm_tw')]], weights=(np.array(weights['tw'])+ 1e-15)/(sum(weights['tw'])+ 1e-15), type_=type_)
 model_tfidf = ensemble_predictions(X_models[[col for col in X_models.columns if col.startswith('tfidf')]], weights=(np.array(weights['tfidf'])+ 1e-15)/(sum(weights['tfidf'])+ 1e-15), type_=type_)
 model_bert = ensemble_predictions(X_models[[col for col in X_models.columns if col.startswith('bert')]], weights=(np.array(weights['bert'])+ 1e-15)/(sum(weights['bert'])+ 1e-15), type_=type_)
 model_gpt = X_models['gpt22']
models = [model_cp, model_clip, model_nb, model_tw, model_tfidf, model_bert, model_gpt]
model_blend = np.zeros_like(model_bert)+ 1e-15
for i in range(len(models)) :
model_blend += weights['blend'][i] * models[i]
model_blend /=(sum(weights['blend'])+ 1e-15)
 return model_blend<save_to_csv>
stores = pd.merge(stores, tmp, how='left', on=['air_store_id','dow'] )
submission = pd.DataFrame.from_dict({
'id': test_df['id'],
'prediction': custom_predict(test_model_preds)
 })
 submission.to_csv("submission.csv", index=False)
 submission.head()<set_options>
stores = pd.merge(stores, data['as'], how='left', on=['air_store_id'] )
! rm -rf _runs<import_modules>
lbl = preprocessing.LabelEncoder()
stores['air_genre_name'] = lbl.fit_transform(stores['air_genre_name'])
 stores['air_area_name'] = lbl.fit_transform(stores['air_area_name'] )
import os
import math
import numpy as np
import pandas as pd
from itertools import product
from matplotlib import pyplot as plt
from sklearn.linear_model import LinearRegression
from sklearn.neighbors import KNeighborsRegressor
from sklearn.neural_network import MLPRegressor
from sklearn.ensemble import RandomForestRegressor
from sklearn.metrics import mean_squared_error, r2_score
import xgboost as xgb<load_from_csv>
data['hol']['visit_date'] = pd.to_datetime(data['hol']['visit_date'])
 data['hol']['day_of_week'] = lbl.fit_transform(data['hol']['day_of_week'])
 data['hol']['visit_date'] = data['hol']['visit_date'].dt.date
sample_submission = pd.read_csv(os.path.join(data_dir, 'sample_submission.csv'))
items = pd.read_csv(os.path.join(data_dir, 'items.csv'))
shops = pd.read_csv(os.path.join(data_dir,'shops.csv'))
sales = pd.read_csv(os.path.join(data_dir, 'sales_train.csv'))
test_data = pd.read_csv(os.path.join(data_dir, 'test.csv'))<count_unique_values>
train = pd.merge(data['tra'], data['hol'], how='left', on=['visit_date'])
 test = pd.merge(data['tes'], data['hol'], how='left', on=['visit_date'] )
sales.nunique()<filter>
train = pd.merge(train, stores, how='left', on=['air_store_id','dow'])
 test = pd.merge(test, stores, how='left', on=['air_store_id','dow'] )
sales[sales['item_price'] > 100000]<filter>
for df in ['ar','hr']:
train = pd.merge(train, data[df], how='left', on=['air_store_id','visit_date'])
 test = pd.merge(test, data[df], how='left', on=['air_store_id','visit_date'] )
sales = sales[sales['date_block_num'] > 11]<feature_engineering>
train['id'] = train.apply(lambda r: '_'.join([str(r['air_store_id']), str(r['visit_date'])]), axis=1 )
all_data.loc[all_data['shop_id'] == 0, 'shop_id'] = 57
all_data.loc[all_data['shop_id'] == 1, 'shop_id'] = 58
all_data.loc[all_data['shop_id'] == 11, 'shop_id'] = 10<merge>
train['total_reserv_sum'] = train['rv1_x'] + train['rv1_y']
train['total_reserv_mean'] =(train['rv2_x'] + train['rv2_y'])/ 2
test['total_reserv_sum'] = test['rv1_x'] + test['rv1_y']
test['total_reserv_mean'] =(test['rv2_x'] + test['rv2_y'])/ 2
lags = [1, 2, 3, 6, 12]
for lag in lags:
lag_col_name = 'target_lag_' + str(lag)
 shifted = all_data[index_cols + ['target']].copy()
shifted.columns = index_cols + [lag_col_name]
shifted['date_block_num'] += lag
all_data = pd.merge(all_data, shifted, on=index_cols, how='left')
 all_data[lag_col_name].fillna(0, inplace=True )<merge>
train = train.fillna(-1)
 test = test.fillna(-1 )
item_category_mapping = items[['item_id','item_category_id']].drop_duplicates()
all_data = pd.merge(all_data, item_category_mapping, how='left', on='item_id' )<groupby>
def RMSLE(y, pred):
return metrics.mean_squared_error(y, pred)**0.5
cumsum = all_data.groupby('item_id')['target'].cumsum() - all_data['target']
cumcnt = all_data.groupby('item_id')['target'].cumcount()
all_data['item_target_enc_exp'] = cumsum / cumcnt
target_mean = all_data['target'].mean()
all_data['item_target_enc_exp'].fillna(target_mean, inplace=True )<groupby>
X_train, X_val, y_train, y_val = train_test_split(train[col], train['visitors'], test_size=0.33, random_state=42)
 X_train.head()
cumsum = all_data.groupby('shop_id')['target'].cumsum() - all_data['target']
cumcnt = all_data.groupby('shop_id')['target'].cumcount()
all_data['shop_target_enc_exp'] = cumsum / cumcnt
target_mean = all_data['target'].mean()
all_data['shop_target_enc_exp'].fillna(target_mean, inplace=True )<groupby>
cumsum = all_data.groupby('item_category_id')['target'].cumsum() - all_data['target']
cumcnt = all_data.groupby('item_category_id')['target'].cumcount()
all_data['item_category_target_enc_exp'] = cumsum / cumcnt
target_mean = all_data['target'].mean()
all_data['item_category_target_enc_exp'].fillna(target_mean, inplace=True )<merge>
last_sale_df = []
for d in range(1, 35):
df = sales[sales.date_block_num < d].groupby(['shop_id', 'item_id'], as_index=False)['date_block_num'].max()
df['last_sale_ago'] = d - df.date_block_num
df.date_block_num = d
last_sale_df.append(df)
 last_sale_df = pd.concat(last_sale_df)
 all_data = pd.merge(all_data, last_sale_df, on=['shop_id', 'item_id', 'date_block_num'], how='left')
 all_data['last_sale_ago'].fillna(0, inplace=True )<merge>
model1 = ensemble.GradientBoostingRegressor(learning_rate = 0.1, n_estimators = 375, max_depth = 6, min_samples_leaf = 2)
last_shop_sale_df = []
for d in range(1, 35):
df = sales[sales.date_block_num < d].groupby('shop_id', as_index=False)['date_block_num'].max()
df['last_shop_sale_ago'] = d - df.date_block_num
df.date_block_num = d
last_shop_sale_df.append(df)
 last_shop_sale_df = pd.concat(last_shop_sale_df)
 all_data = pd.merge(all_data, last_shop_sale_df, on=['shop_id', 'date_block_num'], how='left')
 all_data['last_shop_sale_ago'].fillna(0, inplace=True )<merge>
model1.fit(train[col], np.log1p(train['visitors'].values))
last_item_sale_df = []
for d in range(1, 35):
df = sales[sales.date_block_num < d].groupby('item_id', as_index=False)['date_block_num'].max()
df['last_item_sale_ago'] = d - df.date_block_num
df.date_block_num = d
last_item_sale_df.append(df)
 last_item_sale_df = pd.concat(last_item_sale_df)
 all_data = pd.merge(all_data, last_item_sale_df, on=['item_id', 'date_block_num'], how='left')
 all_data['last_item_sale_ago'].fillna(0, inplace=True )<feature_engineering>
print('RMSE GradientBoostingRegressor: ', RMSLE(np.log1p(train['visitors'].values), model1.predict(train[col])))
 test['visitors'] = model1.predict(test[col])
 test['visitors'] = np.expm1(test['visitors'] ).clip(lower=0.)
sub1 = test[['id','visitors']].copy()
<categorify><EOS> 
sub1[['id', 'visitors']].to_csv('submission.csv', index=False )