{"cells":[{"metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","execution":{"iopub.execute_input":"2020-12-20T11:40:11.714089Z","iopub.status.busy":"2020-12-20T11:40:11.713382Z","iopub.status.idle":"2020-12-20T11:40:11.722215Z","shell.execute_reply":"2020-12-20T11:40:11.721646Z"},"papermill":{"duration":0.037362,"end_time":"2020-12-20T11:40:11.722308","exception":false,"start_time":"2020-12-20T11:40:11.684946","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename)) \n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","execution":{"iopub.execute_input":"2020-12-20T11:40:11.760329Z","iopub.status.busy":"2020-12-20T11:40:11.759402Z","iopub.status.idle":"2020-12-20T11:40:13.34739Z","shell.execute_reply":"2020-12-20T11:40:13.346817Z"},"papermill":{"duration":1.609593,"end_time":"2020-12-20T11:40:13.347508","exception":false,"start_time":"2020-12-20T11:40:11.737915","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"import numpy as np \nimport pandas as pd \nfrom sklearn.preprocessing import LabelEncoder\nfrom sklearn.ensemble import RandomForestClassifier\nfrom sklearn.model_selection import train_test_split , cross_val_score , RandomizedSearchCV","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2020-12-20T11:40:13.399812Z","iopub.status.busy":"2020-12-20T11:40:13.399043Z","iopub.status.idle":"2020-12-20T11:40:14.385837Z","shell.execute_reply":"2020-12-20T11:40:14.385184Z"},"papermill":{"duration":1.016011,"end_time":"2020-12-20T11:40:14.385975","exception":false,"start_time":"2020-12-20T11:40:13.369964","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"train = pd.read_csv(\"../input/airbnb-new-user/train.csv\")\ntest = pd.read_csv(\"../input/airbnb-new-user/test.csv\")","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2020-12-20T11:40:14.434381Z","iopub.status.busy":"2020-12-20T11:40:14.433237Z","iopub.status.idle":"2020-12-20T11:40:14.720291Z","shell.execute_reply":"2020-12-20T11:40:14.719347Z"},"papermill":{"duration":0.31333,"end_time":"2020-12-20T11:40:14.720428","exception":false,"start_time":"2020-12-20T11:40:14.407098","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"train.isnull().sum() ","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2020-12-20T11:40:14.799768Z","iopub.status.busy":"2020-12-20T11:40:14.798925Z","iopub.status.idle":"2020-12-20T11:40:14.824446Z","shell.execute_reply":"2020-12-20T11:40:14.823716Z"},"papermill":{"duration":0.072233,"end_time":"2020-12-20T11:40:14.824597","exception":false,"start_time":"2020-12-20T11:40:14.752364","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"labels = train['country_destination'].values \ntrain = train.drop(['country_destination'], axis=1) \nid_test = test['id']\npiv_train = train.shape[0]","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2020-12-20T11:40:14.900161Z","iopub.status.busy":"2020-12-20T11:40:14.899214Z","iopub.status.idle":"2020-12-20T11:40:14.906535Z","shell.execute_reply":"2020-12-20T11:40:14.905728Z"},"papermill":{"duration":0.050084,"end_time":"2020-12-20T11:40:14.906674","exception":false,"start_time":"2020-12-20T11:40:14.85659","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"train.dtypes","execution_count":null,"outputs":[]},{"metadata":{"papermill":{"duration":0.032454,"end_time":"2020-12-20T11:40:14.971609","exception":false,"start_time":"2020-12-20T11:40:14.939155","status":"completed"},"tags":[]},"cell_type":"markdown","source":"Creating a dataframe with train and test datasets "},{"metadata":{"execution":{"iopub.execute_input":"2020-12-20T11:40:15.045792Z","iopub.status.busy":"2020-12-20T11:40:15.045011Z","iopub.status.idle":"2020-12-20T11:40:15.117123Z","shell.execute_reply":"2020-12-20T11:40:15.116108Z"},"papermill":{"duration":0.113077,"end_time":"2020-12-20T11:40:15.117271","exception":false,"start_time":"2020-12-20T11:40:15.004194","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"final_data = pd.concat((train, test), axis = 0, ignore_index = True)","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2020-12-20T11:40:15.191254Z","iopub.status.busy":"2020-12-20T11:40:15.190337Z","iopub.status.idle":"2020-12-20T11:40:15.286976Z","shell.execute_reply":"2020-12-20T11:40:15.287644Z"},"papermill":{"duration":0.136802,"end_time":"2020-12-20T11:40:15.287795","exception":false,"start_time":"2020-12-20T11:40:15.150993","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"final_data = final_data.drop(['id','date_first_booking'], axis=1)","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2020-12-20T11:40:15.451952Z","iopub.status.busy":"2020-12-20T11:40:15.345069Z","iopub.status.idle":"2020-12-20T11:40:15.592546Z","shell.execute_reply":"2020-12-20T11:40:15.59314Z"},"papermill":{"duration":0.282895,"end_time":"2020-12-20T11:40:15.593295","exception":false,"start_time":"2020-12-20T11:40:15.3104","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"# Missing values\nfinal_data.gender.replace('-unknown-', np.nan, inplace=True) # -unknown- is not considered as a missing value so we replace it by nan\nprint(final_data.isnull().sum())\nfinal_data = final_data.fillna(-1) ","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2020-12-20T11:40:15.732235Z","iopub.status.busy":"2020-12-20T11:40:15.650374Z","iopub.status.idle":"2020-12-20T11:40:17.868208Z","shell.execute_reply":"2020-12-20T11:40:17.867406Z"},"papermill":{"duration":2.250678,"end_time":"2020-12-20T11:40:17.868346","exception":false,"start_time":"2020-12-20T11:40:15.617668","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"# Date_account_created transformation\n# Separate the day month and year into different columns\ndac = np.vstack(final_data.date_account_created.astype(str).apply(lambda x: list(map(int, x.split('-')))).values)\nprint(dac)\nfinal_data['dac_year'] = dac[:,0] \nfinal_data['dac_month'] = dac[:,1]\nfinal_data['dac_day'] = dac[:,2]\nfinal_data = final_data.drop(['date_account_created'], axis = 1)","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2020-12-20T11:40:17.950698Z","iopub.status.busy":"2020-12-20T11:40:17.949561Z","iopub.status.idle":"2020-12-20T11:40:21.207985Z","shell.execute_reply":"2020-12-20T11:40:21.208766Z"},"papermill":{"duration":3.305336,"end_time":"2020-12-20T11:40:21.208986","exception":false,"start_time":"2020-12-20T11:40:17.90365","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"# timestamp_first_active transformation\n# Here as well we separate the timestamp first active into day month and year\ntfa = np.vstack(final_data.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)\nprint(tfa)\nfinal_data['tfa_year'] = tfa[:,0] \nfinal_data['tfa_month'] = tfa[:,1]\nfinal_data['tfa_day'] = tfa[:,2]\nfinal_data = final_data.drop(['timestamp_first_active'], axis=1)","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2020-12-20T11:40:21.266663Z","iopub.status.busy":"2020-12-20T11:40:21.265751Z","iopub.status.idle":"2020-12-20T11:40:21.288414Z","shell.execute_reply":"2020-12-20T11:40:21.289244Z"},"papermill":{"duration":0.055802,"end_time":"2020-12-20T11:40:21.28941","exception":false,"start_time":"2020-12-20T11:40:21.233608","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"print(final_data.age.describe()) # We can see that the age has some inconsistancy variables\nav = final_data.age.values\nfinal_data['age'] = np.where(np.logical_or(av<14, av>100), -1, av)","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2020-12-20T11:40:21.36574Z","iopub.status.busy":"2020-12-20T11:40:21.364951Z","iopub.status.idle":"2020-12-20T11:40:21.471546Z","shell.execute_reply":"2020-12-20T11:40:21.470717Z"},"papermill":{"duration":0.146309,"end_time":"2020-12-20T11:40:21.471694","exception":false,"start_time":"2020-12-20T11:40:21.325385","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"final_data.isnull().sum() ","execution_count":null,"outputs":[]},{"metadata":{"papermill":{"duration":0.03668,"end_time":"2020-12-20T11:40:21.546584","exception":false,"start_time":"2020-12-20T11:40:21.509904","status":"completed"},"tags":[]},"cell_type":"markdown","source":"Feature Selection\nWe are choosing 10 columns other than country_destination, dropping columns like age, date_account_created etc, as they did not quite provide much valuable information needed to predict the next destination."},{"metadata":{"execution":{"iopub.execute_input":"2020-12-20T11:40:21.62795Z","iopub.status.busy":"2020-12-20T11:40:21.626809Z","iopub.status.idle":"2020-12-20T11:40:21.631735Z","shell.execute_reply":"2020-12-20T11:40:21.632268Z"},"papermill":{"duration":0.048152,"end_time":"2020-12-20T11:40:21.632422","exception":false,"start_time":"2020-12-20T11:40:21.58427","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"features = ['gender', 'signup_method', 'signup_flow', 'language', 'affiliate_channel', 'affiliate_provider', 'first_affiliate_tracked', 'signup_app', 'first_device_type', 'first_browser']","execution_count":null,"outputs":[]},{"metadata":{"papermill":{"duration":0.025278,"end_time":"2020-12-20T11:40:21.682823","exception":false,"start_time":"2020-12-20T11:40:21.657545","status":"completed"},"tags":[]},"cell_type":"markdown","source":"Using one-hot encoding to create a new dataframe which consists of zeros and ones. This is useful because in columns like signup_method, affiliate_channel etc, there are multiple values."},{"metadata":{"execution":{"iopub.execute_input":"2020-12-20T11:40:21.753126Z","iopub.status.busy":"2020-12-20T11:40:21.75217Z","iopub.status.idle":"2020-12-20T11:40:22.472349Z","shell.execute_reply":"2020-12-20T11:40:22.471763Z"},"papermill":{"duration":0.764213,"end_time":"2020-12-20T11:40:22.472467","exception":false,"start_time":"2020-12-20T11:40:21.708254","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"for f in features:\n    final_data_dummy = pd.get_dummies(final_data[f], prefix=f)\n    final_data = final_data.drop([f], axis=1)\n    final_data = pd.concat((final_data, final_data_dummy), axis=1)","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2020-12-20T11:40:22.529244Z","iopub.status.busy":"2020-12-20T11:40:22.528086Z","iopub.status.idle":"2020-12-20T11:40:22.685124Z","shell.execute_reply":"2020-12-20T11:40:22.68562Z"},"papermill":{"duration":0.188055,"end_time":"2020-12-20T11:40:22.685769","exception":false,"start_time":"2020-12-20T11:40:22.497714","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"# Splitting train and test for the classifier\nvals = final_data.values\nX = vals[:piv_train]\nle = LabelEncoder()\ny = le.fit_transform(labels)   \nX_test = vals[piv_train:]","execution_count":null,"outputs":[]},{"metadata":{"papermill":{"duration":0.024917,"end_time":"2020-12-20T11:40:22.736204","exception":false,"start_time":"2020-12-20T11:40:22.711287","status":"completed"},"tags":[]},"cell_type":"markdown","source":"Searching for the best hyperparameters using RandomizedSearchCV"},{"metadata":{"execution":{"iopub.execute_input":"2020-12-20T11:40:22.793672Z","iopub.status.busy":"2020-12-20T11:40:22.792974Z","iopub.status.idle":"2020-12-20T11:40:22.797281Z","shell.execute_reply":"2020-12-20T11:40:22.796638Z"},"papermill":{"duration":0.035306,"end_time":"2020-12-20T11:40:22.797383","exception":false,"start_time":"2020-12-20T11:40:22.762077","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"rf= RandomForestClassifier() ","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2020-12-20T11:40:22.856401Z","iopub.status.busy":"2020-12-20T11:40:22.855676Z","iopub.status.idle":"2020-12-20T11:40:22.859262Z","shell.execute_reply":"2020-12-20T11:40:22.859739Z"},"papermill":{"duration":0.037372,"end_time":"2020-12-20T11:40:22.860174","exception":false,"start_time":"2020-12-20T11:40:22.822802","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"rf_param = {'n_estimators':list(range(100,500)) , \n         'max_depth':list(range(1,10)) , \n         'criterion':['gini','entropy'] ,\n         'max_samples':list(range(1,10))    \n} ","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2020-12-20T11:40:22.920698Z","iopub.status.busy":"2020-12-20T11:40:22.920054Z","iopub.status.idle":"2020-12-20T11:40:22.92552Z","shell.execute_reply":"2020-12-20T11:40:22.92476Z"},"papermill":{"duration":0.039064,"end_time":"2020-12-20T11:40:22.925638","exception":false,"start_time":"2020-12-20T11:40:22.886574","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"rscv = RandomizedSearchCV(rf ,param_distributions=rf_param ,  cv =5 , n_iter=10 , \n                          scoring = 'accuracy',n_jobs =-1 , verbose =10)","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2020-12-20T11:40:22.985101Z","iopub.status.busy":"2020-12-20T11:40:22.984295Z","iopub.status.idle":"2020-12-20T11:42:46.521296Z","shell.execute_reply":"2020-12-20T11:42:46.522029Z"},"papermill":{"duration":143.570185,"end_time":"2020-12-20T11:42:46.522195","exception":false,"start_time":"2020-12-20T11:40:22.95201","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"rscv.fit(X,y) ","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2020-12-20T11:42:46.620212Z","iopub.status.busy":"2020-12-20T11:42:46.61925Z","iopub.status.idle":"2020-12-20T11:42:46.625902Z","shell.execute_reply":"2020-12-20T11:42:46.625181Z"},"papermill":{"duration":0.059355,"end_time":"2020-12-20T11:42:46.626041","exception":false,"start_time":"2020-12-20T11:42:46.566686","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"print(rscv.best_score_) \nprint(rscv.best_estimator_)\nprint(rscv.best_index_) \nprint(rscv.best_params_) ","execution_count":null,"outputs":[]},{"metadata":{"papermill":{"duration":0.044689,"end_time":"2020-12-20T11:42:46.715103","exception":false,"start_time":"2020-12-20T11:42:46.670414","status":"completed"},"tags":[]},"cell_type":"markdown","source":"Building the Random Forest model with the best parameters found."},{"metadata":{"execution":{"iopub.execute_input":"2020-12-20T11:42:46.811735Z","iopub.status.busy":"2020-12-20T11:42:46.810947Z","iopub.status.idle":"2020-12-20T11:42:50.65697Z","shell.execute_reply":"2020-12-20T11:42:50.656214Z"},"papermill":{"duration":3.896655,"end_time":"2020-12-20T11:42:50.657117","exception":false,"start_time":"2020-12-20T11:42:46.760462","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"model = RandomForestClassifier(max_samples= 1, max_depth= 7, n_estimators = 165, criterion = \"entropy\", warm_start = True)                  \nmodel.fit(X, y)\ny_pred = model.predict_proba(X_test) ","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2020-12-20T11:42:50.755474Z","iopub.status.busy":"2020-12-20T11:42:50.75441Z","iopub.status.idle":"2020-12-20T11:42:50.76054Z","shell.execute_reply":"2020-12-20T11:42:50.759807Z"},"papermill":{"duration":0.058372,"end_time":"2020-12-20T11:42:50.760685","exception":false,"start_time":"2020-12-20T11:42:50.702313","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"print(y_pred) ","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2020-12-20T11:42:50.862741Z","iopub.status.busy":"2020-12-20T11:42:50.862016Z","iopub.status.idle":"2020-12-20T11:42:57.498722Z","shell.execute_reply":"2020-12-20T11:42:57.499245Z"},"papermill":{"duration":6.692869,"end_time":"2020-12-20T11:42:57.499395","exception":false,"start_time":"2020-12-20T11:42:50.806526","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"# We take the 5 highest probabilities for each person\nids = []  #list of ids\ncts = []  #list of countries \nfor i in range(len(id_test)):\n    idx = id_test[i]\n    ids += [idx] * 5\n    cts += le.inverse_transform(np.argsort(y_pred[i])[::-1])[:5].tolist() ","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2020-12-20T11:42:57.599618Z","iopub.status.busy":"2020-12-20T11:42:57.598986Z","iopub.status.idle":"2020-12-20T11:42:58.728149Z","shell.execute_reply":"2020-12-20T11:42:58.727255Z"},"papermill":{"duration":1.197628,"end_time":"2020-12-20T11:42:58.7283","exception":false,"start_time":"2020-12-20T11:42:57.530672","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"# Generating a csv file with the predictions \nsub = pd.DataFrame(np.column_stack((ids, cts)), columns=['id', 'country_destination'])\nsub.to_csv('randomForest3.csv',index=False) ","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2020-12-20T11:42:58.863368Z","iopub.status.busy":"2020-12-20T11:42:58.862178Z","iopub.status.idle":"2020-12-20T11:42:58.885293Z","shell.execute_reply":"2020-12-20T11:42:58.884502Z"},"papermill":{"duration":0.091039,"end_time":"2020-12-20T11:42:58.885444","exception":false,"start_time":"2020-12-20T11:42:58.794405","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"sub","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}