Kaggle competition: Expedia Hotel Recommendations
<SOS> metric: MAP@{K} Kaggle data source: expedia-hotel-recommendations<define_variables>
sns.set_style('whitegrid')
 %matplotlib inline
train_df = pd.read_csv('.. /input/train.csv', nrows=10000)
 test_df = pd.read_csv('.. /input/test.csv', nrows=10000)
 destination = pd.read_csv('.. /input/destinations.csv', nrows=100000)
 train_df.info()
print("----------------------------")
 test_df.info()
train_df.head()
PATH = Path('.. /input/aptos2019-blindness-detection' )<load_from_csv>
def convert_date_into_days(df):
df['srch_ci'] = pd.to_datetime(df['srch_ci'])
 df['srch_co'] = pd.to_datetime(df['srch_co'])
 df['date_time'] = pd.to_datetime(df['date_time'])
 df['stay_dur'] =(df['srch_co'] - df['srch_ci'] ).astype('timedelta64[D]')
 df['no_of_days_bet_booking'] =(df['srch_ci'] - df['date_time'] ).astype('timedelta64[D]')
 df['Cin_day'] = df["srch_ci"].apply(lambda x: x.day)
 df['Cin_month'] = df["srch_ci"].apply(lambda x: x.month)
 df['Cin_year'] = df["srch_ci"].apply(lambda x: x.year)
 convert_date_into_days(train_df)
 convert_date_into_days(test_df)
 fig, ax = plt.subplots()
fig.set_size_inches(13, 8)
 sns.countplot('Cin_month',data=train_df[train_df["is_booking"] == 1],order=list(range(1,13)) ,ax=ax)
 fig, ax = plt.subplots()
fig.set_size_inches(13, 8)
 sns.countplot('Cin_day',data=train_df[train_df["is_booking"] == 1],order=list(range(1,32)) ,ax=ax)
 fig, ax = plt.subplots()
fig.set_size_inches(13, 8)
 sns.countplot('stay_dur',data=train_df[train_df["is_booking"] == 1],ax=ax)
 test_user_id = test_df['user_id']
columns = ['date_time', 'srch_ci','user_id','srch_destination_type_id','srch_destination_id', 'site_name', 'user_location_region', 'user_location_city',
'user_id', 'srch_co', 'srch_adults_cnt', 'srch_children_cnt', 'srch_rm_cnt']
train_df.drop(columns=columns,axis=1,inplace=True)
 test_df.drop(columns=columns,axis=1,inplace=True )
df = pd.read_csv(PATH/'train.csv')
 df.head()<count_values>
total = train_df.isnull().sum().sort_values(ascending=False)
 percent =(train_df.isnull().sum() /train_df['hotel_cluster'].count() ).sort_values(ascending=False)
 missing_data = pd.concat([total, percent], axis=1, keys=['Total', 'Percent'])
 missing_data
train_df['Cin_day'] = train_df['Cin_day'].fillna(26.0)
 train_df['Cin_month'] = train_df['Cin_month'].fillna(8.0)
 train_df['Cin_year'] = train_df['Cin_year'].fillna(2014.0)
 train_df['stay_dur'] = train_df['stay_dur'].fillna(1.0)
 train_df['no_of_days_bet_booking'] = train_df['no_of_days_bet_booking'].fillna(0.0)
 train_df['orig_destination_distance'].fillna(train_df['orig_destination_distance'].mean() , inplace=True )
df.diagnosis.value_counts()<feature_engineering>
train = pd.read_csv('.. /input/train.csv',
dtype={'is_booking':bool,'srch_destination_id':np.int32, 'hotel_cluster':np.int32},
usecols=['srch_destination_id','is_booking','hotel_cluster'],
chunksize=1000000)
 aggs = []
print('-'*38)
 for chunk in train:
agg = chunk.groupby(['srch_destination_id',
'hotel_cluster'])['is_booking'].agg(['sum','count'])
 agg.reset_index(inplace=True)
 aggs.append(agg)
 print('.',end='')
 print('')
 aggs = pd.concat(aggs, axis=0)
 aggs.head()
CLICK_WEIGHT = 0.05
agg = aggs.groupby(['srch_destination_id','hotel_cluster'] ).sum().reset_index()
agg['count'] -= agg['sum']
agg = agg.rename(columns={'sum':'bookings','count':'clicks'})
 agg['relevance'] = agg['bookings'] + CLICK_WEIGHT * agg['clicks']
agg.head()
def most_popular(group, n_max=5):
relevance = group['relevance'].values
hotel_cluster = group['hotel_cluster'].values
most_popular = hotel_cluster[np.argsort(relevance)[::-1]][:n_max]
return np.array_str(most_popular)[1:-1]
most_pop = agg.groupby(['srch_destination_id'] ).apply(most_popular)
 most_pop = pd.DataFrame(most_pop ).rename(columns={0:'hotel_cluster'})
 most_pop.head()
test = pd.read_csv('.. /input/test.csv',
dtype={'srch_destination_id':np.int32},
usecols=['srch_destination_id'],)
 test = test.merge(most_pop, how='left',left_on='srch_destination_id',right_index=True)
 test.head()
test.hotel_cluster.isnull().sum()
most_pop_all = agg.groupby('hotel_cluster')['relevance'].sum().nlargest(5 ).index
most_pop_all = np.array_str(most_pop_all)[1:-1]
print(most_pop_all)
 test.hotel_cluster.fillna(most_pop_all,inplace=True)
 test.hotel_cluster.to_csv('submission.csv',header=True, index_label='id' )