Kaggle competition: Airbnb New User Bookings
<load_from_csv>
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()
<import_modules>
countries = pd.read_csv('.. /input/countries.csv')
 countries.head(10 )
import matplotlib.pyplot as plt
from matplotlib.pyplot import figure
from tqdm import tqdm<load_from_csv>
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)
df = pd.read_csv('.. /input/m5-forecasting-accuracy/sales_train_validation.csv')
 price_df = pd.read_csv(".. /input/m5-forecasting-accuracy/sell_prices.csv")
 cal_df = pd.read_csv(".. /input/m5-forecasting-accuracy/calendar.csv" )<feature_engineering>
print(data_all.isnull().sum() )
cal_df["d"]=cal_df["d"].apply(lambda x: int(x.split("_")[1]))
price_df["id"] = price_df["item_id"] + "_" + price_df["store_id"] + "_validation"<merge>
data_all.loc[data_all.age > 100, 'age'] = np.nan
data_all.loc[data_all.age < 18, 'age'] = np.nan
for day in tqdm(range(1886, 1914)) :
wk_id = list(cal_df[cal_df["d"]==day]["wm_yr_wk"])[0]
wk_price_df = price_df[price_df["wm_yr_wk"]==wk_id]
df = df.merge(wk_price_df[["sell_price", "id"]], on=["id"], how='inner')
 df["unit_sales_" + str(day)] = df["sell_price"] * df["d_" + str(day)]
df.drop(columns=["sell_price"], inplace=True )<feature_engineering>
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 )
df["dollar_sales"] = df[[c for c in df.columns if c.find("unit_sales")==0]].sum(axis=1 )<drop_column>
data_all.groupby('gender' ).age.agg(['min','max','mean','count'] )
df.drop(columns=[c for c in df.columns if c.find("unit_sales")==0], inplace=True )<feature_engineering>
data_all.language.value_counts(sort=True )
df["weight"] = df["dollar_sales"] / df["dollar_sales"].sum()<drop_column>
print(data_all.isnull().sum() )
df.drop(columns=["dollar_sales"], inplace=True )<feature_engineering>
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)
df["weight"] /= 12<feature_engineering>
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 )
<drop_column><EOS> 
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 )