{"cells":[{"metadata":{"id":"__U1__gUQDuf","trusted":true},"cell_type":"code","source":"import pandas as pd\nimport os\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n","execution_count":null,"outputs":[]},{"metadata":{"id":"O9Z1SOdRqhn5","trusted":true,"outputId":"60954cbd-23c1-4c05-d12c-e5c41c7286e0"},"cell_type":"code","source":"# from google.colab import drive\n# drive.mount('/content/drive')","execution_count":null,"outputs":[]},{"metadata":{"id":"W-q8fej3SC_X","trusted":true},"cell_type":"code","source":"# os.chdir('/content/drive/My Drive/ml_project/airbnb-new-user')","execution_count":null,"outputs":[]},{"metadata":{"id":"lKuMobNdbQdF","trusted":true},"cell_type":"code","source":"df_train = pd.read_csv('../input/airbnb-new-user/train.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_train.info()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_train['country_destination'].value_counts()","execution_count":null,"outputs":[]},{"metadata":{"id":"qAM_MbmyhPsF"},"cell_type":"markdown","source":"##**AGE_GENDER**\n"},{"metadata":{"id":"vsGdAmDHQ6hF","trusted":true},"cell_type":"code","source":"age_gender=pd.read_csv(\"../input/airbnb-new-user/age_gender_bkts.csv\")","execution_count":null,"outputs":[]},{"metadata":{"id":"ZEwPkUHSRJ1A","trusted":true,"outputId":"f158a590-4024-41c4-914d-030d272be227"},"cell_type":"code","source":"age_gender","execution_count":null,"outputs":[]},{"metadata":{"id":"0hiAV2pHRI_d","trusted":true,"outputId":"a336fe18-3491-423b-c37e-eba3485d86e4"},"cell_type":"code","source":"age_gender.info()","execution_count":null,"outputs":[]},{"metadata":{"id":"LHO89O59RV1V","trusted":true,"outputId":"db44182c-213e-498f-d1ad-2c6a37ab18f3"},"cell_type":"code","source":"age_gender.isnull().any() #age_gender.isnull().values.any()","execution_count":null,"outputs":[]},{"metadata":{"id":"tns59UOhZe_6","trusted":true,"outputId":"f0fe260f-c3d7-406e-c25b-11799f52496c"},"cell_type":"code","source":"age_gender['age_bucket'] = age_gender['age_bucket'].apply(lambda x:'100-104' if x =='100+' else x)\n\nage_gender['mean_age'] = age_gender['age_bucket'].apply(lambda x: (int(x.split('-')[0]) + int(x.split('-')[1]))/2)\nage_gender.drop('age_bucket', axis=1,inplace=True)\nage_gender.head()","execution_count":null,"outputs":[]},{"metadata":{"id":"Yuaf7tTaZfMg","trusted":true,"outputId":"11d52658-fa4e-4e98-b63b-bb8e84a41c52"},"cell_type":"code","source":"age_gender['year'].value_counts()","execution_count":null,"outputs":[]},{"metadata":{"id":"GkK4BrKbZfWz","trusted":true},"cell_type":"code","source":"age_gender=age_gender.drop(columns='year',axis=1)","execution_count":null,"outputs":[]},{"metadata":{"id":"C2BEizMZ0Bdr","trusted":true,"outputId":"71f876a5-7558-4cd9-e7d7-7a67f3ca1f6d"},"cell_type":"code","source":"age_gender","execution_count":null,"outputs":[]},{"metadata":{"id":"A0ZrDIr7hik2","trusted":true,"outputId":"a49901a8-f607-49dc-e38c-b57db611110d"},"cell_type":"code","source":"plt.figure(figsize=(15,8))\nsns.barplot(x='mean_age', y='population_in_thousands', hue='gender', data=age_gender, ci=None)","execution_count":null,"outputs":[]},{"metadata":{"id":"RGEZMEqinkEA"},"cell_type":"markdown","source":"The largest groups are people with mean ages 47 years and 52 years.<br>\nThe counts of young and middle aged people are fairly comparable. But, as we move towards the old age people (age > 57), the group population decreases gradually."},{"metadata":{"id":"bIsczU2Mhxjk","trusted":true,"outputId":"b60ea768-acc2-4746-a732-01f5cb5bd83f"},"cell_type":"code","source":"sns.set_style('whitegrid')\nplt.figure(figsize=(10,7))\npop_stats = age_gender.groupby('country_destination')['population_in_thousands'].sum()\nsns.barplot(x=pop_stats.index, y=pop_stats)","execution_count":null,"outputs":[]},{"metadata":{"id":"NzqRnawBnkEL"},"cell_type":"markdown","source":"US is the most populated country among all the destination countries."},{"metadata":{"id":"mhcfJCh8heBA"},"cell_type":"markdown","source":"\n##**Country Statistics**"},{"metadata":{"id":"8voj8-84ROKZ","trusted":true,"outputId":"fff3d70c-d476-4360-82d2-932ced2495e7"},"cell_type":"code","source":"df_con = pd.read_csv('../input/airbnb-new-user/countries.csv')\ndf_con","execution_count":null,"outputs":[]},{"metadata":{"id":"DHXUtl_pRnHr","trusted":true,"outputId":"91031fc2-8029-4122-db93-2e55cd715b6c"},"cell_type":"code","source":"df_con.info()","execution_count":null,"outputs":[]},{"metadata":{"id":"9wjWQMZFyYxI"},"cell_type":"markdown","source":"Our initial intuition is that the farther a country,the less likely people are to visit that particular country.\nTo see this we will construct a scatterplot between the frequency of booking in a country and the distance of the country."},{"metadata":{"id":"ANgpgGxViBlZ","trusted":true,"outputId":"9739efa1-e345-4118-9d65-ee822d2dfa5e"},"cell_type":"code","source":"sns.set_style('whitegrid')\nplt.figure(figsize=(10,7))\nsns.barplot(x='country_destination', y='distance_km', data=df_con)","execution_count":null,"outputs":[]},{"metadata":{"id":"NXHY6Y91nkEu"},"cell_type":"markdown","source":"Among all the destinaton countries, the most farthest country from the US is Australia."},{"metadata":{"id":"UgZjUC3Yyy55"},"cell_type":"markdown","source":"Creating a dataframe of different types of distances and no of bookings for different countries."},{"metadata":{"id":"Q-TTjKpqiNpM","trusted":true},"cell_type":"code","source":"count = df_train[(df_train['country_destination'] != 'NDF') & (df_train['country_destination'] != 'other')]['country_destination'].value_counts()\n\ncountry_distance = pd.Series(df_con['distance_km'])\ncountry_distance.index = df_con['country_destination']\n\nlanguage_distance = pd.Series(df_con['language_levenshtein_distance'])\nlanguage_distance.index = df_con['country_destination']\n\ncountry_area = pd.Series(df_con['destination_km2'])\ncountry_area.index = df_con['country_destination']","execution_count":null,"outputs":[]},{"metadata":{"id":"DCxrwrI6iN5f","trusted":true,"outputId":"819abc59-2bde-453b-a8a5-58675146e523"},"cell_type":"code","source":"df_dp = pd.concat([count, country_distance, language_distance, country_area], axis=1)\ndf_dp.columns = ['count', 'distance_km', 'language', 'area']\ndf_dp","execution_count":null,"outputs":[]},{"metadata":{"id":"_IxJ8RYviOGk","trusted":true,"outputId":"e7a1dd8b-0971-4039-96e4-ca853609aa2b"},"cell_type":"code","source":"df_dp.corr()['count']","execution_count":null,"outputs":[]},{"metadata":{"id":"Yvb-GdQzim2p","trusted":true,"outputId":"5e107efe-ea21-4141-e26a-5d87c8c089c7"},"cell_type":"code","source":"df_dp.drop('US').corr()['count']","execution_count":null,"outputs":[]},{"metadata":{"id":"jY7rhF6Bu5h4"},"cell_type":"markdown","source":"There is a strong negative correlation of -0.64 between distance from US and no of travellers.People mostly prefer booking in the United States than any other country in the world.\nThe correlation when we remove United States ,is much lower at -0.16. However, there is still a negative correlation.\n\n"},{"metadata":{"id":"h1A4Luu9i2Gm","trusted":true,"outputId":"fa95657b-b128-4bcb-c6e0-46d3d73e8888"},"cell_type":"code","source":"sns.scatterplot(x='distance_km',y='count',data=df_dp.drop('US'))","execution_count":null,"outputs":[]},{"metadata":{"id":"2rILemI0i4x-","trusted":true,"outputId":"a866a69a-b291-49a4-8e41-6ba3c9c02027"},"cell_type":"code","source":"sns.scatterplot(x='language',y='count',data=df_dp.drop('US'))","execution_count":null,"outputs":[]},{"metadata":{"id":"aoovc7BojJMe"},"cell_type":"markdown","source":"##**Session Statistics**\n"},{"metadata":{"id":"SRD67eP-Sh3a","trusted":true,"outputId":"15a8e66b-2f84-4e9b-b9d6-6dd5086ecd84"},"cell_type":"code","source":"df_ses = pd.read_csv('../input/airbnb-new-user/sessions.csv')\ndf_ses","execution_count":null,"outputs":[]},{"metadata":{"id":"t20JMAp1S0fd","trusted":true,"outputId":"83eef629-4bde-4437-bb27-0f097fb362b1"},"cell_type":"code","source":"df_ses.info()","execution_count":null,"outputs":[]},{"metadata":{"id":"U5gDdlC2iMVW","trusted":true,"outputId":"3ebd8521-bdf3-4aba-a625-7b2068ca437c"},"cell_type":"code","source":"df_ses.isna().sum()","execution_count":null,"outputs":[]},{"metadata":{"id":"qacpOu9Fp2OE","trusted":true,"outputId":"522291bc-8d96-4c6a-ae04-bcd111429dd1"},"cell_type":"code","source":"df_ses['action_type'].value_counts()","execution_count":null,"outputs":[]},{"metadata":{"id":"-x5gDCZthua-","trusted":true,"outputId":"4493beeb-83b5-4212-f7e0-bbd7c961e857"},"cell_type":"code","source":"df_ses['action_detail'].value_counts()","execution_count":null,"outputs":[]},{"metadata":{"id":"KZgrt0BpjMVB","trusted":true},"cell_type":"code","source":"df_ses['action_type']=df_ses['action_type'].replace('-unknown-',np.nan)\n\ndf_ses['action_detail']=df_ses['action_detail'].replace('-unknown-',np.nan)","execution_count":null,"outputs":[]},{"metadata":{"id":"hmORz7gqomS4","trusted":true,"outputId":"7dce0421-459f-490c-96e8-2717f53cef04"},"cell_type":"code","source":"df_ses['secs_elapsed'].describe()","execution_count":null,"outputs":[]},{"metadata":{"id":"XjnCkDmio0PT","trusted":true,"outputId":"0d4d5289-a0ef-402f-878e-f088c3b03f95"},"cell_type":"code","source":"sns.distplot(df_ses['secs_elapsed'])","execution_count":null,"outputs":[]},{"metadata":{"id":"8xYh7kEkpfMW","trusted":true,"outputId":"0b3c8771-aea3-48f4-ffe3-d19e02055531"},"cell_type":"code","source":"sns.distplot(df_ses[(df_ses['secs_elapsed'].notnull()) & (df_ses['secs_elapsed'] < 5000)]['secs_elapsed'])","execution_count":null,"outputs":[]},{"metadata":{"id":"DQXa93waxGjI"},"cell_type":"markdown","source":"From plot can see that most the number of sessions greater than 1500 seconds decreases almost exponentially. We can assume that most sessions were less than 1500 seconds long. "},{"metadata":{"id":"dOsRy-txpo2N","trusted":true,"outputId":"b7c58423-06fd-47dd-b802-16e8add1a9ca"},"cell_type":"code","source":"len(df_ses[df_ses['secs_elapsed'] < 1500])/len(df_ses[df_ses['secs_elapsed'].notnull()])","execution_count":null,"outputs":[]},{"metadata":{"id":"97VxffXyqw_z","trusted":true,"outputId":"12c031fc-0dc1-4ad5-a6f9-dfcd6f35267b"},"cell_type":"code","source":"df_ses['device_type'].value_counts()","execution_count":null,"outputs":[]},{"metadata":{"id":"2LAT06uHrmQ5","trusted":true,"outputId":"4573aa8b-0e93-4026-9c20-c11b0b08e9ac"},"cell_type":"code","source":"len(df_ses[df_ses['device_type'].isnull()])","execution_count":null,"outputs":[]},{"metadata":{"id":"whpeXR7yVWQX","trusted":true,"outputId":"0de0de23-8672-4b27-9b25-f97111e28365"},"cell_type":"code","source":"plt.figure(figsize=(12,7))\nsns.countplot(y='device_type', data=df_ses)","execution_count":null,"outputs":[]},{"metadata":{"id":"71UtGkuQnkIf"},"cell_type":"markdown","source":"Mac Desktop is the most popular device among users, followed by Windows Desktop.<br>\nApple products are very popular among the users."},{"metadata":{"id":"CVSNi7-g6Zfn"},"cell_type":"markdown","source":"##**Training Data**"},{"metadata":{"id":"fksGrlKrry7W","trusted":true,"outputId":"c083bcd7-fad4-4036-8bed-bb1e1acaa4cc"},"cell_type":"code","source":"df_train = pd.read_csv('../input/airbnb-new-user/train.csv')\ndf_train.head()","execution_count":null,"outputs":[]},{"metadata":{"id":"1RvvP4n4tjHB","trusted":true,"outputId":"dc4bd061-a8bf-43b1-eac8-deeb0fc9689d"},"cell_type":"code","source":"df_train.info()","execution_count":null,"outputs":[]},{"metadata":{"id":"Vd3PUFLg8MPm"},"cell_type":"markdown","source":"**Country Destination**"},{"metadata":{"id":"oIcvsOyr8fdx","trusted":true,"outputId":"a3814868-4c0a-4d53-a9fc-d786f0f8d420"},"cell_type":"code","source":"df_train['country_destination'].isna().sum()","execution_count":null,"outputs":[]},{"metadata":{"id":"skSZQncBRDqs","trusted":true,"outputId":"7a619648-3c6b-45fd-cb59-a851734b86e8"},"cell_type":"code","source":"plt.figure(figsize=(8,7))\nplt.style.use(\"seaborn\")\ncountry_share = df_train['country_destination'].value_counts() / df_train.shape[0] * 100\ncountry_share.plot(kind='bar',color='#FD5C64', rot=0)\nplt.xlabel('Destination Country')\nplt.ylabel('Percentage')\n\n","execution_count":null,"outputs":[]},{"metadata":{"id":"9zgbdCONnkJL"},"cell_type":"markdown","source":"Almost 60% of the users don't any any bookings.<br>\nSo, our model should be capable of distinguishing between NDF and other bookers. Also, we can use the above information to further predict the destination for the bookers. <br>\nAlso, almost 30% of the users have their destination country as US.<br>\nMeans most of the travel is domestic travel. "},{"metadata":{"id":"LZUt1dnpueJy","trusted":true},"cell_type":"code","source":"classes = ['NDF','US','other','FR','IT','GB','ES','CA','DE','NL','AU','PT']","execution_count":null,"outputs":[]},{"metadata":{"id":"gegXMnra9MAV"},"cell_type":"markdown","source":"Use of stacked bar charts:- <br>\nWhat we will try to deduce is the proportion of the bar which belongs to the non booking class (light peach) lower portion of plot.If this is large, it means that particular segment are unlikely to book an Airbnb. Above non booking class color 'dark peach' refer to USA destination."},{"metadata":{"id":"zK4RRV8og2CN","trusted":true},"cell_type":"code","source":"def stacked_bar(feature):\n    ctab = pd.crosstab([df_train[feature].fillna('Unknown')], df_train.country_destination).apply(lambda x: x/x.sum(), axis=1)\n    ctab[classes].plot(kind='bar', stacked=True, colormap='rocket_r', legend=False)","execution_count":null,"outputs":[]},{"metadata":{"id":"gzQD6PyXAbTh"},"cell_type":"markdown","source":"**Gender**"},{"metadata":{"id":"exedYom1AlGL","trusted":true,"outputId":"cf97fbb6-c37a-49be-8b4d-18ea28a7d3ed"},"cell_type":"code","source":"df_train['gender'].value_counts()","execution_count":null,"outputs":[]},{"metadata":{"id":"OZ0P6zDRAwzi","trusted":true,"outputId":"4c6e6940-467e-4c9b-c817-2d77eaff5691"},"cell_type":"code","source":"df_train['gender'].isna().sum()","execution_count":null,"outputs":[]},{"metadata":{"id":"rwTCbOyYsiDA","trusted":true,"outputId":"310693fa-465f-4f95-836b-98d3e1916bd8"},"cell_type":"code","source":"sns.countplot(df_train['gender'])","execution_count":null,"outputs":[]},{"metadata":{"id":"LFMc4PNOnkJ6"},"cell_type":"markdown","source":"Majority of the peoples gender is unknown.<br>\nOut of the users whose gender is known, there are more females than males.<br>\nBelow are some possibilities:- <br>\n(1) There are more female users than male and others.<br>\n(2) Women disclose their gender more often than men.<br>\n(3) 'other' gender people are more shy than male and female in disclosing their gender and hence such a less number in 'other' and such a high number in '-unknown-'"},{"metadata":{"id":"EQTRkHI5sljB","trusted":true,"outputId":"9e5af174-164e-4712-d0f1-c2c08e126682"},"cell_type":"code","source":"stacked_bar('gender')","execution_count":null,"outputs":[]},{"metadata":{"id":"3sdrOoN1nkKE"},"cell_type":"markdown","source":"From the above stacked plot, we can see that people who haven't marked their gender are more likely to belong to the class 'NDF'"},{"metadata":{"trusted":true,"id":"R7seEqwknkKG","outputId":"fac77593-9df4-4a2d-e794-d5ce88fea4e8"},"cell_type":"code","source":"plt.figure(figsize=(14,8))\norder1 = df_train['country_destination'].value_counts().index\nsns.countplot(data = df_train, x = 'country_destination', order = order1,hue='gender',log = True)\nplt.xlabel('Destination')\nplt.ylabel('Count')\nplt.title('Gender-Destination Distribution')","execution_count":null,"outputs":[]},{"metadata":{"id":"z0GatB3tGBb7"},"cell_type":"markdown","source":"**Age**\n"},{"metadata":{"id":"jJ5Nsiy7GOF8","trusted":true,"outputId":"90aedd8c-9b7f-4336-8c8a-721742e511e0"},"cell_type":"code","source":"df_train['age'].value_counts()","execution_count":null,"outputs":[]},{"metadata":{"id":"Jymd9GkBGnOL","trusted":true},"cell_type":"code","source":"df_train['age'] = df_train['age'].apply(lambda x: np.nan if x > 115 or x<18 else x)","execution_count":null,"outputs":[]},{"metadata":{"id":"73CQLnu2GW62","trusted":true,"outputId":"81a633df-195c-4c48-dcff-48598a52f235"},"cell_type":"code","source":"df_train['age'].isna().sum()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"id":"daOT7gi5nkKp","outputId":"10b4ba04-f9c7-4b2e-977f-306cca330028"},"cell_type":"code","source":"plt.figure(figsize=[14,6])\nsns.distplot(df_train.age.dropna(),bins=np.arange(18,100+5,5),color=sns.color_palette()[5],kde=False);\nplt.xlabel('Age of members')\nplt.ylabel('Count')\nplt.title('Age distribution of Users')\nplt.xlim(18,100);","execution_count":null,"outputs":[]},{"metadata":{"id":"H5y0yYOEBCi_"},"cell_type":"markdown","source":"Most of users are of age 28-38"},{"metadata":{"id":"LcstktIvs5AT","trusted":true,"outputId":"171c681b-47e7-470e-c2c1-860073a63fdf"},"cell_type":"code","source":"fig, ax = plt.subplots(nrows=1, ncols=1,figsize=(15, 8))\nsns.boxplot(x='country_destination', y='age', data=df_train, palette=\"muted\", ax =ax)\nax.set_ylim([10, 75])\n","execution_count":null,"outputs":[]},{"metadata":{"id":"GLjRYyB4nkK2"},"cell_type":"markdown","source":"From the above box pot, it is clear that their is not much difference in the age distribution for different destination countries. But countries like Spain and US are more popular among young travellers."},{"metadata":{"id":"TNtuuJ2JtBct","trusted":true},"cell_type":"code","source":"def set_age_group(x):\n    if x < 35:\n        return 'Young'\n    elif x >=35 and x < 60:\n        return 'Middle'\n    elif x >= 60 and x <= 115:\n        return 'Old'\n    else:\n        return 'Unknown'","execution_count":null,"outputs":[]},{"metadata":{"id":"Pb8YkX5ZBq1L","outputId":"ef0df7be-288f-40a7-e26d-f71e24da3fb5","trusted":true},"cell_type":"code","source":"df_train['age'].isna().sum()","execution_count":null,"outputs":[]},{"metadata":{"id":"M-cE-KAGI3nc","trusted":true,"outputId":"ef1ecf09-245b-4534-b43b-012b7b826d1f"},"cell_type":"code","source":"# df_train['age_group'].value_counts()","execution_count":null,"outputs":[]},{"metadata":{"id":"Ii7eZs_UtRu8","trusted":true,"outputId":"a0228f72-63b7-4dc1-f8e3-0dba78fe6b46"},"cell_type":"code","source":"# stacked_bar('age_group')","execution_count":null,"outputs":[]},{"metadata":{"id":"O_hEkRRTnkLS"},"cell_type":"markdown","source":"People who have not disclosed their ages will more likely belong to the class 'NDF'.<br>\nAmongst the other 3 groups their is not much difference."},{"metadata":{"id":"MmEGVj8_JF_d"},"cell_type":"markdown","source":"**Signup Method**"},{"metadata":{"id":"tCknKazSJNzP","trusted":true,"outputId":"42a32fdb-e5f6-4a3f-fa31-959c698ec593"},"cell_type":"code","source":"df_train['signup_method']","execution_count":null,"outputs":[]},{"metadata":{"id":"rqwCLQRkJUM9","trusted":true,"outputId":"a932d944-593a-4908-d78f-f08a6e43d05a"},"cell_type":"code","source":"df_train['signup_method'].value_counts()","execution_count":null,"outputs":[]},{"metadata":{"id":"bQiugqMKJelH","trusted":true,"outputId":"04c3db4a-319b-4000-b11d-04d7cb81291f"},"cell_type":"code","source":"df_train['signup_method'].isna().sum()","execution_count":null,"outputs":[]},{"metadata":{"id":"S2wkRdouJ5k4","trusted":true,"outputId":"06c9ef8a-7658-4969-cefe-deff8d62e63e"},"cell_type":"code","source":"plt.figure(figsize=(7,4))\nsns.countplot(df_train['signup_method'])","execution_count":null,"outputs":[]},{"metadata":{"id":"s85gApDfnkLu"},"cell_type":"markdown","source":"The basic signup method is much more used than facebook. "},{"metadata":{"id":"iEWxnU6itUhH","trusted":true,"outputId":"c1803d62-8cc5-44bd-d0d4-929357012ce3"},"cell_type":"code","source":"stacked_bar('signup_method')","execution_count":null,"outputs":[]},{"metadata":{"id":"BE54eaVGnkL0"},"cell_type":"markdown","source":"The chances of a facebook user belonging to the class 'NDF' are slightly higher than that of basic."},{"metadata":{"id":"2Lsh7HAHK2cM"},"cell_type":"markdown","source":"**Signup_flow**"},{"metadata":{"id":"NQP36ixqK539","trusted":true,"outputId":"ece42ad3-98db-4c26-feac-e0f962c03278"},"cell_type":"code","source":"df_train['signup_flow']","execution_count":null,"outputs":[]},{"metadata":{"id":"fCjCujREK-r1","trusted":true,"outputId":"733cce8e-68f1-474c-b6eb-7c5a2eaaf37f"},"cell_type":"code","source":"df_train['signup_flow'].value_counts()","execution_count":null,"outputs":[]},{"metadata":{"id":"iKwM24jjLEYu","trusted":true,"outputId":"2c9c5711-de17-40d4-c49d-299f34dc63af"},"cell_type":"code","source":"df_train['signup_flow'].isna().sum()","execution_count":null,"outputs":[]},{"metadata":{"id":"Kzi8qTvRLVfq","trusted":true,"outputId":"307fd1d6-4db1-448f-a6d6-85e64dce0244"},"cell_type":"code","source":"sns.countplot(df_train['signup_flow'])","execution_count":null,"outputs":[]},{"metadata":{"id":"0un5sZ8CnkMQ"},"cell_type":"markdown","source":"Users with signup_flow 0 are the most in number, followed by 3,12,25,2."},{"metadata":{"id":"Y4YcudD6tuMU","trusted":true,"outputId":"80e52a59-ed7a-49e9-e0c6-e4dd32425a0c"},"cell_type":"code","source":"stacked_bar('signup_flow')","execution_count":null,"outputs":[]},{"metadata":{"id":"iiVwLQPBnkMY"},"cell_type":"markdown","source":"Users with signup flow 4 have never made a booking.<br>\nAlso, Users with signup flow 1,5,16,20 have a very high chance of not booking."},{"metadata":{"id":"Tbcc2tiPKiAt"},"cell_type":"markdown","source":"**Language**"},{"metadata":{"id":"_cheKyiILvmn","trusted":true,"outputId":"d8e75bad-9e26-42dc-8543-5809f033855f"},"cell_type":"code","source":"df_train['language']","execution_count":null,"outputs":[]},{"metadata":{"id":"Q1ETOwpgLvXG","trusted":true},"cell_type":"code","source":"df_train['language'].value_counts()","execution_count":null,"outputs":[]},{"metadata":{"id":"vOoV-ZejLu8A","trusted":true,"outputId":"710ac6d4-cccc-4740-be9e-7bfbdaa6a4fa"},"cell_type":"code","source":"df_train['language'].isna().sum()","execution_count":null,"outputs":[]},{"metadata":{"id":"YKpMLvd5MAUF","trusted":true,"outputId":"22159cc7-ee59-4353-da22-cab89c5953b9"},"cell_type":"code","source":"plt.figure(figsize=(14,6))\nsns.countplot(df_train['language'],log=True)","execution_count":null,"outputs":[]},{"metadata":{"id":"jJGh67nFnkMv"},"cell_type":"markdown","source":"Majority of the users language preference is English."},{"metadata":{"id":"vpSOuNG7tZq9","trusted":true,"outputId":"c3faf6ac-060e-4978-a74c-bd7b0456d3b5"},"cell_type":"code","source":"stacked_bar('language')","execution_count":null,"outputs":[]},{"metadata":{"id":"Fb-KT9yfnkM5"},"cell_type":"markdown","source":"People who speak Hungarian and Indonesian made no bookings.<br>\nPeople who speak language 'ca' made a lot of bookings and all of those bookings were to the US. Since the users are American, we can assume that the non English users consitute a minortiy."},{"metadata":{"id":"6d_rcCAXM2Pi"},"cell_type":"markdown","source":"**affiliate_channel**"},{"metadata":{"id":"cZJDjKAGM1KG","trusted":true,"outputId":"e2bbce42-cb6a-4c59-806b-ffefe67054bc"},"cell_type":"code","source":"df_train['affiliate_channel']","execution_count":null,"outputs":[]},{"metadata":{"id":"LtlKba-NNVjz","trusted":true,"outputId":"4eb8a15c-eaa2-4d08-9142-a3417614897a"},"cell_type":"code","source":"df_train['affiliate_channel'].value_counts()","execution_count":null,"outputs":[]},{"metadata":{"id":"fTc13uS9NZr1","trusted":true,"outputId":"7b634fdc-4052-4b68-b7cc-7fbb6aac0e36"},"cell_type":"code","source":"df_train['affiliate_channel'].isna().sum()","execution_count":null,"outputs":[]},{"metadata":{"id":"l-OOYrZpNnVO","trusted":true,"outputId":"9dc80322-5b57-4909-b3b7-d13714bcc800"},"cell_type":"code","source":"plt.figure(figsize=(10,6))\nsns.countplot(df_train['affiliate_channel'])","execution_count":null,"outputs":[]},{"metadata":{"id":"OJ9aPnDvnkNS"},"cell_type":"markdown","source":"Number of direct users is significantly higher than others."},{"metadata":{"id":"GCyOEFGQtgGP","trusted":true,"outputId":"6f323488-b207-4866-de3f-bdba43c0ceba"},"cell_type":"code","source":"stacked_bar('affiliate_channel')","execution_count":null,"outputs":[]},{"metadata":{"id":"m-uVD2FsnkNX"},"cell_type":"markdown","source":"'content' users are more likely to not make a booking as compared to other users."},{"metadata":{"id":"PeKV_uJsM-fh"},"cell_type":"markdown","source":"**affiliate_provider**"},{"metadata":{"id":"O4PGDkUyOa-E","trusted":true,"outputId":"ff1adb10-863e-4156-feef-ae94e15a1018"},"cell_type":"code","source":"df_train['affiliate_provider']","execution_count":null,"outputs":[]},{"metadata":{"id":"x9ayEz_-ObYg","trusted":true,"outputId":"400644ae-9f7b-4826-8813-c9b1bcfce387"},"cell_type":"code","source":"df_train['affiliate_provider'].value_counts()","execution_count":null,"outputs":[]},{"metadata":{"id":"J4ToqJ4TObsX","trusted":true,"outputId":"feb79c05-16a0-413f-bdda-28ed2b06bb89"},"cell_type":"code","source":"df_train['affiliate_provider'].isna().sum()","execution_count":null,"outputs":[]},{"metadata":{"id":"56IwktHbOtFv","trusted":true,"outputId":"fa95e04f-7ad5-4fc2-d823-3883d93b3f91"},"cell_type":"code","source":"plt.figure(figsize=(14,6))\na=sns.countplot(df_train['affiliate_provider'],log=True)\na.set_xticklabels(a.get_xticklabels(),rotation=45)\nplt.plot()","execution_count":null,"outputs":[]},{"metadata":{"id":"hemV68xYnkNs"},"cell_type":"markdown","source":"direct users are much more as compared to others.\ndirect is followed by google, 'other', craigslist."},{"metadata":{"id":"542DZTeAtmmU","trusted":true,"outputId":"1d6e45c8-a1d8-49bf-f5f3-968de6616360"},"cell_type":"code","source":"stacked_bar('affiliate_provider')","execution_count":null,"outputs":[]},{"metadata":{"id":"g01nxtD4nkNz"},"cell_type":"markdown","source":"Users with affiliate provider 'wayn' haven't made any booking and users with affiliate providers 'gsp', 'meetup' and 'yandex' are more likely to not make a booking."},{"metadata":{"id":"CB8l6mrFNESl"},"cell_type":"markdown","source":"**first_affiliate_tracked**"},{"metadata":{"id":"O2ka-pDNQ5w4","trusted":true,"outputId":"c6fca4ff-2b29-4284-edf3-a35c1b1f9eb0"},"cell_type":"code","source":"df_train['first_affiliate_tracked']","execution_count":null,"outputs":[]},{"metadata":{"id":"8QSMBFeFQ-so","trusted":true,"outputId":"1cb73148-a32c-4978-8779-cbbe9b10f8f2"},"cell_type":"code","source":"df_train['first_affiliate_tracked'].value_counts()","execution_count":null,"outputs":[]},{"metadata":{"id":"QnkdDxg_RDKf","trusted":true,"outputId":"d86a1306-c328-40a5-bad3-c61e94f09e19"},"cell_type":"code","source":"df_train['first_affiliate_tracked'].isna().sum()","execution_count":null,"outputs":[]},{"metadata":{"id":"1KSGrBlSROv8","trusted":true,"outputId":"dfec0543-6b12-4903-ddfb-d44722400ad8"},"cell_type":"code","source":"plt.figure(figsize=(10,6))\na=sns.countplot(df_train['first_affiliate_tracked'],log=True)\na.set_xticklabels(a.get_xticklabels(),rotation=45)\nplt.plot()","execution_count":null,"outputs":[]},{"metadata":{"id":"YdmRFTrsnkOO"},"cell_type":"markdown","source":"'untracked' users are the most in number. <br>\nomg and linked are almost equal in number."},{"metadata":{"id":"_rF5qXu-tsfm","trusted":true,"outputId":"55b10efb-6c8b-4e5e-d815-69f2def69614"},"cell_type":"code","source":"stacked_bar('first_affiliate_tracked')","execution_count":null,"outputs":[]},{"metadata":{"id":"EKNu1GJGnkOV"},"cell_type":"markdown","source":"Users whose first affiliate track is unknown are more likely to not make any booking as compared to other tracks."},{"metadata":{"id":"j9Ih9nLJTDJB"},"cell_type":"markdown","source":"**signup_app**\n"},{"metadata":{"id":"jzxiZj33Sy3t","trusted":true,"outputId":"14059e01-63d8-46ed-f3a7-27e61ce40c6e"},"cell_type":"code","source":"df_train['signup_app']","execution_count":null,"outputs":[]},{"metadata":{"id":"sxsxO70_TIrR","trusted":true,"outputId":"46a47bb0-8bdd-4c2d-9e23-3a68c52ec59b"},"cell_type":"code","source":"df_train['signup_app'].value_counts()","execution_count":null,"outputs":[]},{"metadata":{"id":"EK902WSUTLbq","trusted":true,"outputId":"17081334-55b0-46ac-e657-8cea28ed7a08"},"cell_type":"code","source":"df_train['signup_app'].isna().sum()","execution_count":null,"outputs":[]},{"metadata":{"id":"xjZTOO16TX67","trusted":true,"outputId":"25a7d575-bd2a-401f-bf56-f1b84cbe4480"},"cell_type":"code","source":"plt.figure(figsize=(6,4))\nsns.countplot(df_train['signup_app'],log=True)","execution_count":null,"outputs":[]},{"metadata":{"id":"Y_CEu2IvnkOp"},"cell_type":"markdown","source":"The number of Web users is significantly higher than others, followed by iOS then Moweb and then Android."},{"metadata":{"id":"wjcamRbttxSn","trusted":true,"outputId":"0c1c8222-d740-4fd8-b997-d7ce2da9065f"},"cell_type":"code","source":"stacked_bar('signup_app')","execution_count":null,"outputs":[]},{"metadata":{"id":"kceYK3QWnkOu"},"cell_type":"markdown","source":"Android and iOS users are more likely to not make a booking than other users."},{"metadata":{"id":"04aKUHdGS7yE"},"cell_type":"markdown","source":"**first_device_type** "},{"metadata":{"id":"i3RDjzbGUu7J","trusted":true,"outputId":"bf124038-9670-4734-ae34-35cc10954f81"},"cell_type":"code","source":"df_train['first_device_type']","execution_count":null,"outputs":[]},{"metadata":{"id":"eriufOKNU3IE","trusted":true,"outputId":"931447f5-fcec-4cb6-ed0e-4b297090fe29"},"cell_type":"code","source":"df_train['first_device_type'].value_counts()","execution_count":null,"outputs":[]},{"metadata":{"id":"25yTBI6hVdoy","trusted":true,"outputId":"425d1524-f744-471b-cf12-72079a8a72d1"},"cell_type":"code","source":"df_train['first_device_type'].isna().sum()","execution_count":null,"outputs":[]},{"metadata":{"id":"M0nkN9xEVs2W","trusted":true,"outputId":"b11f7c9b-1fca-4f3e-ce52-0c87e9baf69c"},"cell_type":"code","source":"plt.figure(figsize=(14,5))\nsns.countplot(df_train['first_device_type'],log=True)","execution_count":null,"outputs":[]},{"metadata":{"id":"NgaalR8qnkPA"},"cell_type":"markdown","source":"Desktop users are more in number as compared to other users."},{"metadata":{"id":"r9kFOQrFty-x","trusted":true,"outputId":"2a017aff-b02f-4833-826c-bc233bc5684a"},"cell_type":"code","source":"stacked_bar('first_device_type')","execution_count":null,"outputs":[]},{"metadata":{"id":"6WOjxNEunkPM"},"cell_type":"markdown","source":"Users whose devices are unknown made less bookings.<br>\nAlso, Desktop users are more likely to make a booking as compared to mobile and tablet users."},{"metadata":{"id":"wGxmkjn7TACM"},"cell_type":"markdown","source":"**first_browser**"},{"metadata":{"id":"JT2Cw_4-WBti","trusted":true,"outputId":"51066b94-422b-46c9-9e9a-cc197d1724aa"},"cell_type":"code","source":"df_train['first_browser']","execution_count":null,"outputs":[]},{"metadata":{"id":"v5HP9cAEWH_h","trusted":true,"outputId":"e366b2df-1728-476a-b16e-a9fc2cf13994"},"cell_type":"code","source":"df_train['first_browser'].value_counts().nlargest(10)","execution_count":null,"outputs":[]},{"metadata":{"id":"ibc12z8EDqyf","outputId":"b0caaf67-0e3c-4a48-dceb-daef4fcd52d0","trusted":true},"cell_type":"code","source":"len(df_train['first_browser'].value_counts())","execution_count":null,"outputs":[]},{"metadata":{"id":"P6O8oNcwWIPM","trusted":true,"outputId":"f44defd0-800d-49e6-a95c-d899e209cdfb"},"cell_type":"code","source":"df_train['first_browser'].isna().sum()","execution_count":null,"outputs":[]},{"metadata":{"id":"RI8deNc-D0YV"},"cell_type":"markdown","source":"Since most of the first browers are Chrome,Safari,Firefox and others we can make merge remaining categories out of 50 to a single label."},{"metadata":{"id":"Y-jW-DGzXkIn"},"cell_type":"markdown","source":"**date_account_created**"},{"metadata":{"id":"d2eWnZFDY0Xs","trusted":true,"outputId":"2e4e4955-9a45-44f6-b61d-45c72da37920"},"cell_type":"code","source":"df_train['date_account_created']","execution_count":null,"outputs":[]},{"metadata":{"id":"IU8JtfCGt1Hy","trusted":true},"cell_type":"code","source":"df_train['date_account_created'] = pd.to_datetime(df_train['date_account_created'])","execution_count":null,"outputs":[]},{"metadata":{"id":"rgA7vVCYZCJ2","trusted":true,"outputId":"b9dc4ced-d790-4051-bab8-6fda3ab7e692"},"cell_type":"code","source":"df_train['date_account_created'].isna().sum()","execution_count":null,"outputs":[]},{"metadata":{"id":"HfBbIpn8t4GE","trusted":true,"outputId":"8d0abbee-017d-481e-c897-2ad187f64cb6"},"cell_type":"code","source":"plt.figure(figsize=(12,6))\n\ndf_train[df_train['country_destination'] != 'NDF']['date_account_created'].value_counts().plot(kind='line', linewidth=1, color='green')\ndf_train[df_train['country_destination'] == 'NDF']['date_account_created'].value_counts().plot(kind='line', linewidth=1, color='blue')","execution_count":null,"outputs":[]},{"metadata":{"id":"AO1M4Mfq5JJ0"},"cell_type":"markdown","source":"Here blue plot represent the users which didn't book the airbnb and green represent booking users.The number of non booking users have increased more than the number of booking users as time passed by.This can be due to the fact that more people are using the platform just for exploration."},{"metadata":{"trusted":true,"id":"fGNbC6PsnkP2","outputId":"895d70c5-4753-49af-9677-82af45cdbab6"},"cell_type":"code","source":"dac = np.vstack(df_train.date_account_created.astype(str).apply(lambda x: list(map(int, x.split('-')))).values)\ndf_train['dac_year'] = dac[:,0]\ndf_train['dac_month'] = dac[:,1]\ndf_train['dac_day'] = dac[:,2]\ndf_train['dac_weekday'] = df_train.date_account_created.dt.day_name()\nplt.figure(figsize=(12,6))\nsns.countplot(x='dac_weekday',data=df_train)\nplt.xlabel('Day wise')\nplt.ylabel('Number of users')","execution_count":null,"outputs":[]},{"metadata":{"id":"UeVOUtqEnkP7"},"cell_type":"markdown","source":"From above distribution over weekdays, we can observe that there is less chance of booking on weekends as there is less user activity on saturdays and sundays."},{"metadata":{"trusted":true,"id":"2e5vsLoDnkP8","outputId":"361cc833-240e-41b7-b79c-0f85c1d41707"},"cell_type":"code","source":"plt.figure(figsize=[20,8])\ndf_train['booked'] = df_train.country_destination.apply(lambda x:1 if x!='NDF' else 0 )\ndestination_percentage = df_train.groupby(['dac_year','dac_month']).booked.sum() /df_train.shape[0] * 100\ndestination_percentage.plot(kind='bar')\nplt.xlabel('Year wise - each month Travel count')\nplt.ylabel('Percentage')","execution_count":null,"outputs":[]},{"metadata":{"id":"fHzrC-GHnkP_"},"cell_type":"markdown","source":"From above distribution, we can observe that there are high chances of booking in 7,8,9 months of the year.This suggests the month in the date variables could be useful to separate."},{"metadata":{"id":"f-XhBGf3XqI-"},"cell_type":"markdown","source":"**timestamp_first_active**"},{"metadata":{"id":"LeSv_w5XZ_J9","trusted":true,"outputId":"b24524df-708c-4c8c-d6de-1caa75c26e5f"},"cell_type":"code","source":"df_train['timestamp_first_active']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"id":"sMRmPRPSnkQE"},"cell_type":"code","source":"tfa = np.vstack(df_train.timestamp_first_active.astype(str).apply(lambda x: list(map(int, [x[:4],x[4:6],x[6:8],x[8:10],x[10:12],x[12:14]]))).values)\ndf_train['tfa_year'] = tfa[:,0]\ndf_train['tfa_month'] = tfa[:,1]\ndf_train['tfa_day'] = tfa[:,2]","execution_count":null,"outputs":[]},{"metadata":{"id":"3ql72BIKXx-7"},"cell_type":"markdown","source":"**date_first_booking**   "},{"metadata":{"id":"8rKHeB5Bt6FN","trusted":true,"outputId":"18c6b7b2-c19d-416c-984c-3b4bd70bf569"},"cell_type":"code","source":"df_train['date_first_booking']","execution_count":null,"outputs":[]},{"metadata":{"id":"y2SdQRd78QEa","trusted":true},"cell_type":"code","source":"df_train['date_first_booking'] = pd.to_datetime(df_train['date_first_booking'])","execution_count":null,"outputs":[]},{"metadata":{"id":"0jFcG3Qj8STV","trusted":true,"outputId":"f02cc570-8bfa-454f-d214-effede77f798"},"cell_type":"code","source":"plt.figure(figsize=(12,6))\ndf_train[df_train['country_destination'] != 'NDF']['date_first_booking'].value_counts().plot(kind='line', linewidth=1, color='green')\nplt.ylabel(\"count\")\n","execution_count":null,"outputs":[]},{"metadata":{"id":"k76wO88F8Yzf"},"cell_type":"markdown","source":"The number of bookings have increased as time passed by."},{"metadata":{"id":"QtKzcrSwrJ-8"},"cell_type":"markdown","source":"##**Data preprocessing**"},{"metadata":{"id":"3WPitlHJUtek","trusted":true},"cell_type":"code","source":"df_train=pd.read_csv(\"../input/airbnb-new-user/train.csv\")","execution_count":null,"outputs":[]},{"metadata":{"id":"-HNd310KE9fh","trusted":true},"cell_type":"code","source":"def set_age_group(x):\n    if x < 35:\n        return 'Young'\n    elif x >=35 and x < 60:\n        return 'Middle'\n    elif x >= 60 and x <= 115:\n        return 'Old'\n    else:\n        return 'Unknown'","execution_count":null,"outputs":[]},{"metadata":{"id":"9yfDeqj4Z-bb","trusted":true},"cell_type":"code","source":"major_browsers = ['Chrome', 'Safari', 'Firefox', 'IE', 'Mobile Safari','Chrome Mobile','Android Browser']\ndf_train['first_browser'] = df_train['first_browser'].apply(lambda x: 'Other' if x not in major_browsers else x)\n\ndf_train['age_group'] = df_train['age'].apply(set_age_group)\n\ndf_train['first_affiliate_tracked'] = df_train['first_affiliate_tracked'].fillna('Untracked')\n\ndf_train['flow'] = df_train['signup_flow'].apply(lambda x: 1 if x==3 or x==6 or x==10 or x==2 else 0)\n\ndf_train['signup_method'] = df_train['signup_method'].apply(lambda x: x if x==\"basic\" else \"other\")\n\ndf_train['affiliate_provider'] = df_train['affiliate_provider'].apply(lambda x: 'rest' if x not in ['direct', 'google', 'other'] else x)\n\ndf_train['language'] = df_train['language'].apply(lambda x: 'foreign' if x != 'en' else x)\n\n#date_account_created\ndac = np.vstack(df_train.date_account_created.astype(str).apply(lambda x: list(map(int, x.split('-')))).values)\ndf_train['dac_year'] = dac[:,0]\ndf_train['dac_month'] = dac[:,1]\ndf_train['dac_day'] = dac[:,2]\ndf_train = df_train.drop(['date_account_created'], axis=1)\n\n#timestamp_first_active\ntfa = np.vstack(df_train.timestamp_first_active.astype(str).apply(lambda x: list(map(int, [x[:4],x[4:6],x[6:8],x[8:10],x[10:12],x[12:14]]))).values)\ndf_train['tfa_year'] = tfa[:,0]\ndf_train['tfa_month'] = tfa[:,1]\ndf_train['tfa_day'] = tfa[:,2]\ndf_train = df_train.drop(['timestamp_first_active'], axis=1)\n\n\n\ntrain = df_train.drop(['id','country_destination','date_first_booking','age','signup_flow'], axis = 1)\nY=df_train['country_destination']\n\nohe_feats = ['gender', 'signup_method', 'flow', 'language', 'affiliate_channel',\n             'affiliate_provider', 'first_affiliate_tracked', 'signup_app', 'first_device_type', 'first_browser','age_group']\nfor f in ohe_feats:\n    train_dummy = pd.get_dummies(train[f], prefix=f)\n    train = train.drop([f], axis=1)\n    train = pd.concat((train, train_dummy), axis=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat":4,"nbformat_minor":4}