Kaggle competition: Recruit Restaurant Visitor Forecasting
%%time
omer_lstm_preds=[]
lstm128_num = len(os.listdir('.. /input/omer-lstm-128'))
for _ in range(lstm128_num):
PATH = '.. /input/omer-lstm-128/modellstm'+str(_)+'.pt'
model = load_model(PATH)
 all_test_preds = predict(model)
 test_preds = all_test_preds[-1]
test_preds = np.mean(all_test_preds, axis=0)[:, 0]
omer_lstm_preds.append(test_preds)
 del model
gc.collect()<predict_on_test>
dfs = { re.search('/([^/\.]*)\.csv', fn ).group(1):pd.read_csv(fn)for fn in glob.glob('.. /input/*.csv')}
print('data frames read:{}'.format(list(dfs.keys())))
 print('local variables with the same names are created.')
 for k, v in dfs.items() : locals() [k] = v
%%time
lstm256_num = len(os.listdir('.. /input/toxic-lstm256'))
for _ in range(lstm256_num):
PATH = '.. /input/toxic-lstm256/lstm256_'+str(_)+'.pt'
model = load_model(PATH, 256)
 all_test_preds = predict(model)
 test_preds = all_test_preds[-1]
test_preds = np.mean(all_test_preds, axis=0)[:, 0]
omer_lstm_preds.append(test_preds)
 del model
gc.collect()<prepare_output>
print('holidays at weekends are not special, right?')
 wkend_holidays = date_info.apply(( lambda x:(x.day_of_week=='Sunday' or x.day_of_week=='Saturday')and x.holiday_flg==1), axis=1)
 date_info.loc[wkend_holidays, 'holiday_flg'] = 0
print('add decreasing weights from now')
 date_info['weight'] =(( date_info.index + 1)/ len(date_info)) ** 5
print('weighted mean visitors for each(air_store_id, day_of_week, holiday_flag)or(air_store_id, day_of_week)')
 visit_data = air_visit_data.merge(date_info, left_on='visit_date', right_on='calendar_date', how='left')
 visit_data.drop('calendar_date', axis=1, inplace=True)
 visit_data['visitors'] = visit_data.visitors.map(pd.np.log1p)
 wmean = lambda x:(( x.weight * x.visitors ).sum() / x.weight.sum())
 visitors = visit_data.groupby(['air_store_id', 'day_of_week', 'holiday_flg'] ).apply(wmean ).reset_index()
visitors.rename(columns={0:'visitors'}, inplace=True)
 print('prepare to merge with date_info and visitors')
 sample_submission['air_store_id'] = sample_submission.id.map(lambda x: '_'.join(x.split('_')[:-1]))
sample_submission['calendar_date'] = sample_submission.id.map(lambda x: x.split('_')[2])
 sample_submission.drop('visitors', axis=1, inplace=True)
 sample_submission = sample_submission.merge(date_info, on='calendar_date', how='left')
 sample_submission = sample_submission.merge(visitors, on=['air_store_id', 'day_of_week', 'holiday_flg'], how='left')
omer_lstm_preds = np.mean(omer_lstm_preds, axis=0 )<create_dataframe>
ora1= sample_submission
print('split date')
 ora1['year'] = ora1.calendar_date.map(lambda x:(x.split('-')[0]))
ora1['month'] = ora1.calendar_date.map(lambda x:(x.split('-')[1]))
ora1['day'] = ora1.calendar_date.map(lambda x:(x.split('-')[2]))
ora1['mhend_flg'] = ora1.day.map(lambda x: 1 if int(x)>=25 else 0)
 ora1=pd.get_dummies(ora1, columns = ['day_of_week'])
 ora1=pd.get_dummies(ora1, columns = ['air_store_id'])
 ora1.head()
submission_omer = pd.DataFrame.from_dict({
'id': test_df['id'],
'prediction': omer_lstm_preds
} )<prepare_output>
missings = ora1.visitors.isnull()
test = ora1[missings].copy()
test.drop(['visitors'], axis=1, inplace=True)
 test.drop(['id'], axis=1, inplace=True)
 test.drop(['calendar_date'], axis=1, inplace=True)
 x = ora1[-missings].copy()
y = x['visitors'].copy()
x.drop(['visitors'], axis=1, inplace=True)
 x.drop(['id'], axis=1, inplace=True)
 x.drop(['calendar_date'], axis=1, inplace=True)
 x.shape
lstm_ensemble_prediction = submission_chris.prediction*0.5 + submission_omer.prediction*0.5<save_to_csv>
model = RandomForestRegressor(max_depth = 1000,
max_features =834,
min_samples_split = 20,
n_estimators = 50,
n_jobs = -1,
random_state = 0)
 model.fit(x, y)
 output = model.predict(test)
 model.score(x, y )
<string_transform><EOS> 
ora1.loc[missings,'visitors'] =output
ora1['visitors'] = ora1.visitors.map(pd.np.expm1)
 sub = pd.DataFrame({ 'id': ora1['id'],
'visitors':ora1['visitors'] })
 sub.to_csv('random_result.csv', float_format='%.4f', index=None)
 print("done")
 ora1.head()