{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Booking.com Multi-Destinations Trips Dataset demo"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Load train set"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(1166835, 9)\n"
]
},
{
"data": {
"text/html": [
"
\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" user_id | \n",
" checkin | \n",
" checkout | \n",
" city_id | \n",
" device_class | \n",
" affiliate_id | \n",
" booker_country | \n",
" hotel_country | \n",
" utrip_id | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 1000027 | \n",
" 2016-08-13 | \n",
" 2016-08-14 | \n",
" 8183 | \n",
" desktop | \n",
" 7168 | \n",
" Elbonia | \n",
" Gondal | \n",
" 1000027_1 | \n",
"
\n",
" \n",
" 1 | \n",
" 1000027 | \n",
" 2016-08-14 | \n",
" 2016-08-16 | \n",
" 15626 | \n",
" desktop | \n",
" 7168 | \n",
" Elbonia | \n",
" Gondal | \n",
" 1000027_1 | \n",
"
\n",
" \n",
" 2 | \n",
" 1000027 | \n",
" 2016-08-16 | \n",
" 2016-08-18 | \n",
" 60902 | \n",
" desktop | \n",
" 7168 | \n",
" Elbonia | \n",
" Gondal | \n",
" 1000027_1 | \n",
"
\n",
" \n",
" 3 | \n",
" 1000027 | \n",
" 2016-08-18 | \n",
" 2016-08-21 | \n",
" 30628 | \n",
" desktop | \n",
" 253 | \n",
" Elbonia | \n",
" Gondal | \n",
" 1000027_1 | \n",
"
\n",
" \n",
" 4 | \n",
" 1000033 | \n",
" 2016-04-09 | \n",
" 2016-04-11 | \n",
" 38677 | \n",
" mobile | \n",
" 359 | \n",
" Gondal | \n",
" Cobra Island | \n",
" 1000033_1 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" user_id checkin checkout city_id device_class affiliate_id \\\n",
"0 1000027 2016-08-13 2016-08-14 8183 desktop 7168 \n",
"1 1000027 2016-08-14 2016-08-16 15626 desktop 7168 \n",
"2 1000027 2016-08-16 2016-08-18 60902 desktop 7168 \n",
"3 1000027 2016-08-18 2016-08-21 30628 desktop 253 \n",
"4 1000033 2016-04-09 2016-04-11 38677 mobile 359 \n",
"\n",
" booker_country hotel_country utrip_id \n",
"0 Elbonia Gondal 1000027_1 \n",
"1 Elbonia Gondal 1000027_1 \n",
"2 Elbonia Gondal 1000027_1 \n",
"3 Elbonia Gondal 1000027_1 \n",
"4 Gondal Cobra Island 1000033_1 "
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"train_set = pd.read_csv('train_set.csv').sort_values(by=['utrip_id','checkin'])\n",
"\n",
"print(train_set.shape)\n",
"train_set.head()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Load testset"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(378667, 9)\n"
]
},
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" user_id | \n",
" checkin | \n",
" checkout | \n",
" device_class | \n",
" affiliate_id | \n",
" booker_country | \n",
" utrip_id | \n",
" city_id | \n",
" hotel_country | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 1000066 | \n",
" 2016-07-21 | \n",
" 2016-07-23 | \n",
" desktop | \n",
" 9924 | \n",
" Gondal | \n",
" 1000066_2 | \n",
" 56430 | \n",
" Urkesh | \n",
"
\n",
" \n",
" 1 | \n",
" 1000066 | \n",
" 2016-07-23 | \n",
" 2016-07-25 | \n",
" desktop | \n",
" 9924 | \n",
" Gondal | \n",
" 1000066_2 | \n",
" 41971 | \n",
" Urkesh | \n",
"
\n",
" \n",
" 2 | \n",
" 1000066 | \n",
" 2016-07-25 | \n",
" 2016-07-28 | \n",
" desktop | \n",
" 9924 | \n",
" Gondal | \n",
" 1000066_2 | \n",
" 5797 | \n",
" Urkesh | \n",
"
\n",
" \n",
" 3 | \n",
" 1000066 | \n",
" 2016-07-28 | \n",
" 2016-07-31 | \n",
" mobile | \n",
" 2436 | \n",
" Gondal | \n",
" 1000066_2 | \n",
" 0 | \n",
" NaN | \n",
"
\n",
" \n",
" 4 | \n",
" 1000270 | \n",
" 2016-02-08 | \n",
" 2016-02-09 | \n",
" mobile | \n",
" 9452 | \n",
" The Devilfire Empire | \n",
" 1000270_1 | \n",
" 50075 | \n",
" The Devilfire Empire | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" user_id checkin checkout device_class affiliate_id \\\n",
"0 1000066 2016-07-21 2016-07-23 desktop 9924 \n",
"1 1000066 2016-07-23 2016-07-25 desktop 9924 \n",
"2 1000066 2016-07-25 2016-07-28 desktop 9924 \n",
"3 1000066 2016-07-28 2016-07-31 mobile 2436 \n",
"4 1000270 2016-02-08 2016-02-09 mobile 9452 \n",
"\n",
" booker_country utrip_id city_id hotel_country \n",
"0 Gondal 1000066_2 56430 Urkesh \n",
"1 Gondal 1000066_2 41971 Urkesh \n",
"2 Gondal 1000066_2 5797 Urkesh \n",
"3 Gondal 1000066_2 0 NaN \n",
"4 The Devilfire Empire 1000270_1 50075 The Devilfire Empire "
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"test_set = pd.read_csv('test_set.csv').sort_values(by=['utrip_id','checkin'])\n",
"print(test_set.shape)\n",
"\n",
"test_set.head()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Generate Dummy Predictions - use top 4 cities in the trainset as benchmark recommendation"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"topcities = train_set.city_id.value_counts().index[:4]\n",
"\n",
"test_trips = (test_set[['utrip_id']].drop_duplicates()).reset_index().drop('index', axis=1)\n",
"cities_prediction = pd.DataFrame([topcities]*test_trips.shape[0]\n",
" , columns= ['city_id_1','city_id_2','city_id_3','city_id_4'])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Create Submission file according to the format"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(70662, 5)\n"
]
},
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" utrip_id | \n",
" city_id_1 | \n",
" city_id_2 | \n",
" city_id_3 | \n",
" city_id_4 | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 1000066_2 | \n",
" 47499 | \n",
" 23921 | \n",
" 36063 | \n",
" 17013 | \n",
"
\n",
" \n",
" 1 | \n",
" 1000270_1 | \n",
" 47499 | \n",
" 23921 | \n",
" 36063 | \n",
" 17013 | \n",
"
\n",
" \n",
" 2 | \n",
" 1000441_1 | \n",
" 47499 | \n",
" 23921 | \n",
" 36063 | \n",
" 17013 | \n",
"
\n",
" \n",
" 3 | \n",
" 100048_1 | \n",
" 47499 | \n",
" 23921 | \n",
" 36063 | \n",
" 17013 | \n",
"
\n",
" \n",
" 4 | \n",
" 1000543_1 | \n",
" 47499 | \n",
" 23921 | \n",
" 36063 | \n",
" 17013 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" utrip_id city_id_1 city_id_2 city_id_3 city_id_4\n",
"0 1000066_2 47499 23921 36063 17013\n",
"1 1000270_1 47499 23921 36063 17013\n",
"2 1000441_1 47499 23921 36063 17013\n",
"3 100048_1 47499 23921 36063 17013\n",
"4 1000543_1 47499 23921 36063 17013"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"submission = pd.concat([test_trips,cities_prediction], axis =1)\n",
"print(submission.shape)\n",
"submission.head()"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"submission.to_csv('submission.csv',index=False)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Read submission file and ground truth"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"ground_truth = pd.read_csv('ground_truth.csv',index_col=[0])\n",
"submission = pd.read_csv('submission.csv',index_col=[0])"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(70662, 2)\n"
]
},
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" city_id | \n",
" hotel_country | \n",
"
\n",
" \n",
" utrip_id | \n",
" | \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
" 1038944_1 | \n",
" 54085 | \n",
" Sokovia | \n",
"
\n",
" \n",
" 1068715_1 | \n",
" 29319 | \n",
" Cobra Island | \n",
"
\n",
" \n",
" 1075528_1 | \n",
" 55763 | \n",
" Bozatta | \n",
"
\n",
" \n",
" 1110462_4 | \n",
" 11930 | \n",
" Alvonia | \n",
"
\n",
" \n",
" 1132565_1 | \n",
" 58659 | \n",
" Axphain | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" city_id hotel_country\n",
"utrip_id \n",
"1038944_1 54085 Sokovia\n",
"1068715_1 29319 Cobra Island\n",
"1075528_1 55763 Bozatta\n",
"1110462_4 11930 Alvonia\n",
"1132565_1 58659 Axphain"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"print(ground_truth.shape)\n",
"ground_truth.head()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Evaluate - use accuracy at 4 to evaluate the prediction"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"def evaluate_accuracy_at_4(submission,ground_truth):\n",
" '''checks if the true city is within the four recommended cities'''\n",
" data = submission.join(ground_truth,on='utrip_id')\n",
"\n",
" hits = ((data['city_id']==data['city_id_1'])|(data['city_id']==data['city_id_2'])|\n",
" (data['city_id']==data['city_id_3'])|(data['city_id']==data['city_id_4']))*1\n",
" return hits.mean()"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.05271574537941185"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"evaluate_accuracy_at_4(submission,ground_truth)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.3"
}
},
"nbformat": 4,
"nbformat_minor": 4
}