Kaggle competition: Airbnb New User Bookings
def get_data_by_store(store):
df = pd.concat([pd.read_pickle(BASE),
pd.read_pickle(PRICE ).iloc[:,2:],
pd.read_pickle(CALENDAR ).iloc[:,2:]],
axis=1)
 df = df[df['store_id']==store]
df2 = pd.read_pickle(MEAN_ENC)[mean_features]
df2 = df2[df2.index.isin(df.index)]
df3 = pd.read_pickle(LAGS ).iloc[:,3:]
df3 = df3[df3.index.isin(df.index)]
df = pd.concat([df, df2], axis=1)
 del df2
df = pd.concat([df, df3], axis=1)
 del df3
features = [col for col in list(df)if col not in remove_features]
df = df[['id','d',TARGET]+features]
df = df[df['d']>=START_TRAIN].reset_index(drop=True)
 return df, features
def get_base_test() :
base_test = pd.DataFrame()
for store_id in STORES_IDS:
temp_df = pd.read_pickle('test_'+store_id+'.pkl')
 temp_df['store_id'] = store_id
base_test = pd.concat([base_test, temp_df] ).reset_index(drop=True)
 return base_test
def make_lag(LAG_DAY):
lag_df = base_test[['id','d',TARGET]]
col_name = 'sales_lag_'+str(LAG_DAY)
 lag_df[col_name] = lag_df.groupby(['id'])[TARGET].transform(lambda x: x.shift(LAG_DAY)).astype(np.float16)
 return lag_df[[col_name]]
def make_lag_roll(LAG_DAY):
shift_day = LAG_DAY[0]
roll_wind = LAG_DAY[1]
lag_df = base_test[['id','d',TARGET]]
col_name = 'rolling_mean_tmp_'+str(shift_day)+'_'+str(roll_wind)
 lag_df[col_name] = lag_df.groupby(['id'])[TARGET].transform(lambda x: x.shift(shift_day ).rolling(roll_wind ).mean())
 return lag_df[[col_name]]<init_hyperparams>
data_train = pd.read_csv('.. /input/train_users_2.csv',parse_dates=['timestamp_first_active','date_account_created','date_first_booking'])
 data_test = pd.read_csv('.. /input/test_users.csv',parse_dates=['timestamp_first_active','date_account_created','date_first_booking'])
 data_train.head()
lgb_params = {
'boosting_type': 'gbdt',
'objective': 'tweedie',
'tweedie_variance_power': 1.1,
'metric': 'rmse',
'subsample': 0.5,
'subsample_freq': 1,
'learning_rate': 0.03,
'num_leaves': 2**11-1,
'min_data_in_leaf': 2**12-1,
'feature_fraction': 0.5,
'max_bin': 100,
'n_estimators': 1400,
'boost_from_average': False,
'verbose': -1,
}<init_hyperparams>
countries = pd.read_csv('.. /input/countries.csv')
 countries.head(10 )
VER = 1
SEED = 42
seed_everything(SEED)
 lgb_params['seed'] = SEED
N_CORES = psutil.cpu_count()
TARGET = 'sales'
START_TRAIN = 0
END_TRAIN = 1913
P_HORIZON = 28
USE_AUX = True
remove_features = ['id','state_id','store_id',
'date','wm_yr_wk','d',TARGET]
mean_features = ['enc_cat_id_mean','enc_cat_id_std',
'enc_dept_id_mean','enc_dept_id_std',
'enc_item_id_mean','enc_item_id_std']
ORIGINAL = '.. /input/m5-forecasting-accuracy/'
BASE = '.. /input/m5-simple-fe/grid_part_1.pkl'
PRICE = '.. /input/m5-simple-fe/grid_part_2.pkl'
CALENDAR = '.. /input/m5-simple-fe/grid_part_3.pkl'
LAGS = '.. /input/m5-lags-features/lags_df_28.pkl'
MEAN_ENC = '.. /input/m5-custom-features/mean_encoding_df.pkl'
AUX_MODELS = '.. /input/m5-aux-models/'
STORES_IDS = pd.read_csv(ORIGINAL+'sales_train_validation.csv')['store_id']
STORES_IDS = list(STORES_IDS.unique())
 SHIFT_DAY = 28
N_LAGS = 15
LAGS_SPLIT = [col for col in range(SHIFT_DAY,SHIFT_DAY+N_LAGS)]
ROLS_SPLIT = []
for i in [1,7,14]:
for j in [7,14,30,60]:
ROLS_SPLIT.append([i,j] )<define_variables>
test_ids = data_test['id']
Nrows_train = data_train.shape[0]
labels = data_train['country_destination'].values
data_train1 = data_train.drop(['country_destination'], axis=1)
 data_all = pd.concat(( data_train1, data_test), axis = 0, ignore_index = True)
 data_all = data_all.drop(['id','date_first_booking'], axis=1)
for store_id in STORES_IDS:
print('Train', store_id)
 grid_df, features_columns = get_data_by_store(store_id)
 train_mask = grid_df['d']<=END_TRAIN
valid_mask = train_mask&(grid_df['d']>(END_TRAIN-P_HORIZON))
preds_mask = grid_df['d']>(END_TRAIN-100)
 train_data = lgb.Dataset(grid_df[train_mask][features_columns],
label=grid_df[train_mask][TARGET])
 train_data.save_binary('train_data.bin')
 train_data = lgb.Dataset('train_data.bin')
 valid_data = lgb.Dataset(grid_df[valid_mask][features_columns],
label=grid_df[valid_mask][TARGET])
 grid_df = grid_df[preds_mask].reset_index(drop=True)
 keep_cols = [col for col in list(grid_df)if '_tmp_' not in col]
grid_df = grid_df[keep_cols]
grid_df.to_pickle('test_'+store_id+'.pkl')
 del grid_df
seed_everything(SEED)
 estimator = lgb.train(lgb_params,
train_data,
valid_sets = [valid_data],
verbose_eval = 100, 
)
 model_name = 'lgb_model_'+store_id+'_v'+str(VER)+'.bin'
pickle.dump(estimator, open(model_name, 'wb'))
!rm train_data.bin
del train_data, valid_data, estimator
gc.collect()
MODEL_FEATURES = features_columns<define_variables>
print(data_all.isnull().sum() )
all_preds = pd.DataFrame()
base_test = get_base_test()
main_time = time.time()
for PREDICT_DAY in range(1,29):
print('Predict | Day:', PREDICT_DAY)
 start_time = time.time()
grid_df = base_test.copy()
grid_df = pd.concat([grid_df, df_parallelize_run(make_lag_roll, ROLS_SPLIT)], axis=1)
 for store_id in STORES_IDS:
model_path = 'lgb_model_'+store_id+'_v'+str(VER)+'.bin'
if USE_AUX:
model_path = AUX_MODELS + model_path
estimator = pickle.load(open(model_path, 'rb'))
day_mask = base_test['d']==(END_TRAIN+PREDICT_DAY)
 store_mask = base_test['store_id']==store_id
mask =(day_mask)&(store_mask)
 base_test[TARGET][mask] = estimator.predict(grid_df[mask][MODEL_FEATURES])
 temp_df = base_test[day_mask][['id',TARGET]]
temp_df.columns = ['id','F'+str(PREDICT_DAY)]
if 'id' in list(all_preds):
all_preds = all_preds.merge(temp_df, on=['id'], how='left')
 else:
all_preds = temp_df.copy()
print('
' %0.2f min total |' %(( time.time() - main_time)/ 60),
' %0.2f day sales |' %(temp_df['F'+str(PREDICT_DAY)].sum()))
del temp_df
all_preds = all_preds.reset_index(drop=True)
 all_preds<save_to_csv>
data_all.loc[data_all.age > 100, 'age'] = np.nan
data_all.loc[data_all.age < 18, 'age'] = np.nan
submission = pd.read_csv(ORIGINAL+'sample_submission.csv')[['id']]
submission = submission.merge(all_preds, on=['id'], how='left' ).fillna(0)
 submission.to_csv('submission_v'+str(VER)+'.csv', index=False )<import_modules>
data_all['dac_year'] = data_all.date_account_created.dt.year
data_all['dac_month'] = data_all.date_account_created.dt.month
data_all['dac_day'] = data_all.date_account_created.dt.day
data_all['tfa_year'] = data_all.timestamp_first_active.dt.year
data_all['tfa_month'] = data_all.timestamp_first_active.dt.month
data_all['tfa_day'] = data_all.timestamp_first_active.dt.day
data_all.drop('date_account_created',1, inplace=True)
 data_all.drop('timestamp_first_active',1, inplace=True )
import numpy as np
import pandas as pd<load_from_csv>
data_all.groupby('gender' ).age.agg(['min','max','mean','count'] )
data_Raw = pd.read_csv('.. /input/m5resaultdataset/ss.csv')
 data = data_Raw
data.head() 
<feature_engineering>
data_all.language.value_counts(sort=True )
data["F1"] = data["F1"] - data["F1"]*0.016
data["F2"] = data["F2"] + data["F2"]*0.017
data["F3"] = data["F3"] - data["F3"]*0.018
data["F4"] = data["F4"] + data["F4"]*0.019
data["F5"] = data["F5"] - data["F5"]*0.020
data["F6"] = data["F6"] + data["F6"]*0.021
data["F7"] = data["F7"] - data["F7"]*0.022
data["F8"] = data["F8"] + data["F8"]*0.023
data["F9"] = data["F9"] - data["F9"]*0.024
data["F10"] = data["F10"] + data["F10"]*0.025 
<save_to_csv>
print(data_all.isnull().sum() )
data.to_csv('ss-001.csv', index=False )<load_from_csv>
features = ['gender','signup_method','signup_flow','language','affiliate_channel','affiliate_provider',\
'first_affiliate_tracked','signup_app','first_device_type','first_browser']
data_all = pd.get_dummies(data_all,columns=features)
<load_from_csv>
V = data_all.values
X_train = V[:Nrows_train]
X_test = V[Nrows_train:]
labler = LabelEncoder()
y = labler.fit_transform(labels)
 xgb = XGBClassifier(max_depth=6, learning_rate=0.3, n_estimators=22,
objective='multi:softprob', subsample=0.6, colsample_bytree=0.6, seed=0)
 xgb.fit(X_train, y)
 y_pred = xgb.predict_proba(X_test )
<load_from_csv>
ids = []
cts = []
for i in range(len(test_ids)) :
idx = test_ids[i]
ids += [idx] * 5
cts += labler.inverse_transform(np.argsort(y_pred[i])[::-1])[:5].tolist()
sub = pd.DataFrame(np.column_stack(( ids, cts)) , columns=['id', 'country'])
 sub.to_csv('submission.csv',index=False )