{"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_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Spaceship Titanic End to End Machine Learning Project","metadata":{"id":"UTZX82MX4y_z"}},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.model_selection import cross_validate\nfrom sklearn.impute import SimpleImputer\nfrom sklearn.preprocessing import StandardScaler\nfrom sklearn.preprocessing import LabelEncoder\nfrom sklearn.preprocessing import OrdinalEncoder\nfrom sklearn.preprocessing import OneHotEncoder\nfrom sklearn.linear_model import LogisticRegression\nfrom sklearn.svm import SVC\nfrom sklearn.neighbors import KNeighborsClassifier\nfrom sklearn.ensemble import RandomForestClassifier\nfrom sklearn.ensemble import VotingClassifier\nfrom sklearn.ensemble import GradientBoostingClassifier\nfrom sklearn.model_selection import RandomizedSearchCV\nfrom sklearn.base import BaseEstimator, TransformerMixin\nfrom sklearn.pipeline import Pipeline\nfrom imblearn.over_sampling import SMOTE","metadata":{"id":"oAVEl5x24sCX","execution":{"iopub.status.busy":"2022-07-07T07:31:40.550172Z","iopub.execute_input":"2022-07-07T07:31:40.550531Z","iopub.status.idle":"2022-07-07T07:31:40.558202Z","shell.execute_reply.started":"2022-07-07T07:31:40.550502Z","shell.execute_reply":"2022-07-07T07:31:40.557277Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import warnings\nwarnings.filterwarnings(\"ignore\", category=DeprecationWarning)\nwarnings.filterwarnings(\"ignore\", category=FutureWarning)","metadata":{"id":"Y64m_rD2whJU","execution":{"iopub.status.busy":"2022-07-07T07:31:40.591131Z","iopub.execute_input":"2022-07-07T07:31:40.592013Z","iopub.status.idle":"2022-07-07T07:31:40.597437Z","shell.execute_reply.started":"2022-07-07T07:31:40.591967Z","shell.execute_reply":"2022-07-07T07:31:40.59638Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Import and observe data","metadata":{"id":"kSar665H_pFj"}},{"cell_type":"code","source":"train_df = pd.read_csv('../input/spaceship-titanic/train.csv')\ntest_df = pd.read_csv('../input/spaceship-titanic/test.csv')","metadata":{"id":"mUgc3pHU_WdL","execution":{"iopub.status.busy":"2022-07-07T07:31:40.603667Z","iopub.execute_input":"2022-07-07T07:31:40.604105Z","iopub.status.idle":"2022-07-07T07:31:40.653708Z","shell.execute_reply.started":"2022-07-07T07:31:40.604075Z","shell.execute_reply":"2022-07-07T07:31:40.65271Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"id":"LQW9iFMh_jCy","outputId":"b5cec2c0-ca72-4267-f583-afd7eb2054a1","execution":{"iopub.status.busy":"2022-07-07T07:31:40.65823Z","iopub.execute_input":"2022-07-07T07:31:40.659107Z","iopub.status.idle":"2022-07-07T07:31:40.681523Z","shell.execute_reply.started":"2022-07-07T07:31:40.659065Z","shell.execute_reply":"2022-07-07T07:31:40.680284Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.head()","metadata":{"id":"nSInf64C_lUp","outputId":"72ed612f-59ed-4df9-de9c-984854db7766","execution":{"iopub.status.busy":"2022-07-07T07:31:40.683346Z","iopub.execute_input":"2022-07-07T07:31:40.684074Z","iopub.status.idle":"2022-07-07T07:31:40.708033Z","shell.execute_reply.started":"2022-07-07T07:31:40.684033Z","shell.execute_reply":"2022-07-07T07:31:40.707043Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.info()","metadata":{"id":"MjyXZ7MT_mc_","outputId":"4a6f8792-c554-4b5f-d7fb-b39cfc46cdf7","execution":{"iopub.status.busy":"2022-07-07T07:31:40.710741Z","iopub.execute_input":"2022-07-07T07:31:40.711569Z","iopub.status.idle":"2022-07-07T07:31:40.741973Z","shell.execute_reply.started":"2022-07-07T07:31:40.711512Z","shell.execute_reply":"2022-07-07T07:31:40.741007Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.info()","metadata":{"id":"Em-qASlx_u_5","outputId":"b078f9e7-1355-4bfc-9250-3004a4ec2a3e","execution":{"iopub.status.busy":"2022-07-07T07:31:40.743938Z","iopub.execute_input":"2022-07-07T07:31:40.744651Z","iopub.status.idle":"2022-07-07T07:31:40.761389Z","shell.execute_reply.started":"2022-07-07T07:31:40.744611Z","shell.execute_reply":"2022-07-07T07:31:40.760227Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# drop 'Name' column, because it's unnecessary\ntrain_df.drop('Name', axis=1, inplace=True)\ntest_df.drop('Name', axis=1, inplace=True)","metadata":{"id":"SeM2VkVx_04v","execution":{"iopub.status.busy":"2022-07-07T07:31:40.763057Z","iopub.execute_input":"2022-07-07T07:31:40.763457Z","iopub.status.idle":"2022-07-07T07:31:40.775838Z","shell.execute_reply.started":"2022-07-07T07:31:40.763418Z","shell.execute_reply":"2022-07-07T07:31:40.774757Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# check if 'Name' column are successfully dropped\ntrain_df.shape, test_df.shape","metadata":{"id":"qYOR9BgWA2tP","outputId":"fad2693d-b1a4-40dc-dde1-f676df588079","execution":{"iopub.status.busy":"2022-07-07T07:31:40.777042Z","iopub.execute_input":"2022-07-07T07:31:40.778061Z","iopub.status.idle":"2022-07-07T07:31:40.785188Z","shell.execute_reply.started":"2022-07-07T07:31:40.777984Z","shell.execute_reply":"2022-07-07T07:31:40.784224Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Check for null values\ndef plot_null(df):\n  plt.figure(figsize=(10,5))\n  ax = sns.barplot(x=df.isna().sum().sort_values().index, \n                   y=df.isna().sum().sort_values().values)\n  ax.set_xticklabels(ax.get_xticklabels(), rotation=45)\n  ax.bar_label(ax.containers[0])\n  plt.show()\n\nplot_null(train_df)","metadata":{"id":"w77Q-QvvA7Ko","outputId":"40c95436-ca7c-4b05-8e4e-ee3bf5db8e81","execution":{"iopub.status.busy":"2022-07-07T07:31:40.78655Z","iopub.execute_input":"2022-07-07T07:31:40.787591Z","iopub.status.idle":"2022-07-07T07:31:41.139205Z","shell.execute_reply.started":"2022-07-07T07:31:40.787554Z","shell.execute_reply":"2022-07-07T07:31:41.138142Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_null(test_df)","metadata":{"id":"9fAYd-BYEr-e","outputId":"79abad9d-55e4-44e2-9553-6a376b819bdf","execution":{"iopub.status.busy":"2022-07-07T07:31:41.143167Z","iopub.execute_input":"2022-07-07T07:31:41.143938Z","iopub.status.idle":"2022-07-07T07:31:41.428367Z","shell.execute_reply.started":"2022-07-07T07:31:41.143907Z","shell.execute_reply":"2022-07-07T07:31:41.427282Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data preprocessing","metadata":{"id":"7db4GFwOFbYf"}},{"cell_type":"markdown","source":"## handle null on numeric variables","metadata":{"id":"cY8N9rSDI7Ic"}},{"cell_type":"code","source":"num_var = train_df.select_dtypes(exclude=['O', bool]).columns.values","metadata":{"id":"W0noOTA1IypO","execution":{"iopub.status.busy":"2022-07-07T07:31:41.430066Z","iopub.execute_input":"2022-07-07T07:31:41.430445Z","iopub.status.idle":"2022-07-07T07:31:41.438028Z","shell.execute_reply.started":"2022-07-07T07:31:41.430406Z","shell.execute_reply":"2022-07-07T07:31:41.436815Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_var","metadata":{"id":"nWdsjvXhJLnX","outputId":"154aaca8-be8c-4e89-c667-fde709d76fa9","execution":{"iopub.status.busy":"2022-07-07T07:31:41.439445Z","iopub.execute_input":"2022-07-07T07:31:41.440307Z","iopub.status.idle":"2022-07-07T07:31:41.448456Z","shell.execute_reply.started":"2022-07-07T07:31:41.44027Z","shell.execute_reply":"2022-07-07T07:31:41.44736Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_null(train_df.loc[:, num_var])","metadata":{"id":"NumPs2TyJtlk","outputId":"52c35c15-33d1-441a-ecb8-68f867c9cf2e","execution":{"iopub.status.busy":"2022-07-07T07:31:41.449819Z","iopub.execute_input":"2022-07-07T07:31:41.45042Z","iopub.status.idle":"2022-07-07T07:31:41.663138Z","shell.execute_reply.started":"2022-07-07T07:31:41.450376Z","shell.execute_reply":"2022-07-07T07:31:41.66223Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_null(test_df.loc[:, num_var])","metadata":{"id":"W7n2mG76Jy5j","outputId":"06277d15-a363-4b6a-c37e-086f5e032cce","execution":{"iopub.status.busy":"2022-07-07T07:31:41.66528Z","iopub.execute_input":"2022-07-07T07:31:41.666237Z","iopub.status.idle":"2022-07-07T07:31:41.869254Z","shell.execute_reply.started":"2022-07-07T07:31:41.666197Z","shell.execute_reply":"2022-07-07T07:31:41.868258Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_imputer = SimpleImputer(strategy='mean')\n\ntrain_df.loc[:,num_var] = num_imputer.fit_transform(train_df.loc[:,num_var])\nplot_null(train_df.loc[:, num_var])","metadata":{"id":"Ln4EKPVjJPkT","outputId":"c2dabcb9-23ba-429b-cde9-cc3cb57efba1","execution":{"iopub.status.busy":"2022-07-07T07:31:41.871131Z","iopub.execute_input":"2022-07-07T07:31:41.872141Z","iopub.status.idle":"2022-07-07T07:31:42.090133Z","shell.execute_reply.started":"2022-07-07T07:31:41.872102Z","shell.execute_reply":"2022-07-07T07:31:42.089202Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.loc[:, num_var] = num_imputer.transform(test_df.loc[:, num_var])\nplot_null(test_df.loc[:, num_var])","metadata":{"id":"6ewLdvnBJ7IJ","outputId":"0d634d88-8d08-47db-adb9-fa0989bee104","execution":{"iopub.status.busy":"2022-07-07T07:31:42.091585Z","iopub.execute_input":"2022-07-07T07:31:42.092623Z","iopub.status.idle":"2022-07-07T07:31:42.413293Z","shell.execute_reply.started":"2022-07-07T07:31:42.092576Z","shell.execute_reply":"2022-07-07T07:31:42.412307Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plot null on all variables\nplot_null(train_df)","metadata":{"id":"J-TlojFvKQIt","outputId":"51badd99-a24c-4c3f-c26e-f4c02ef060f4","execution":{"iopub.status.busy":"2022-07-07T07:31:42.417712Z","iopub.execute_input":"2022-07-07T07:31:42.418099Z","iopub.status.idle":"2022-07-07T07:31:42.80613Z","shell.execute_reply.started":"2022-07-07T07:31:42.418061Z","shell.execute_reply":"2022-07-07T07:31:42.805143Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_null(test_df)","metadata":{"id":"5J17Xr_lKXXc","outputId":"0cab3980-731b-42f9-b5a8-c72fb65c70e9","execution":{"iopub.status.busy":"2022-07-07T07:31:42.807575Z","iopub.execute_input":"2022-07-07T07:31:42.808237Z","iopub.status.idle":"2022-07-07T07:31:43.082285Z","shell.execute_reply.started":"2022-07-07T07:31:42.808187Z","shell.execute_reply":"2022-07-07T07:31:43.081387Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# drop the PassengerId column because the group some passenger travelling can be represent on their cabin\ntrain_df.drop('PassengerId', axis=1, inplace=True)\npassenger_id_df = test_df[['PassengerId']]\ntest_df = test_df.drop('PassengerId', axis=1)","metadata":{"id":"nVG1roIzKcTb","execution":{"iopub.status.busy":"2022-07-07T07:31:43.083655Z","iopub.execute_input":"2022-07-07T07:31:43.084091Z","iopub.status.idle":"2022-07-07T07:31:43.093976Z","shell.execute_reply.started":"2022-07-07T07:31:43.084056Z","shell.execute_reply":"2022-07-07T07:31:43.092562Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## handle null values on categorical variables","metadata":{"id":"kIKlSJoWLZ0j"}},{"cell_type":"code","source":"cat_var = train_df.select_dtypes('O').columns.values","metadata":{"id":"H2hftMBTK_-m","execution":{"iopub.status.busy":"2022-07-07T07:31:43.096198Z","iopub.execute_input":"2022-07-07T07:31:43.096687Z","iopub.status.idle":"2022-07-07T07:31:43.106711Z","shell.execute_reply.started":"2022-07-07T07:31:43.096651Z","shell.execute_reply":"2022-07-07T07:31:43.105634Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.loc[:, cat_var].head()","metadata":{"id":"LFROI9NPLfC5","outputId":"8f6a593d-6f4d-4c96-e07d-a5435ebd1cd2","execution":{"iopub.status.busy":"2022-07-07T07:31:43.108256Z","iopub.execute_input":"2022-07-07T07:31:43.108655Z","iopub.status.idle":"2022-07-07T07:31:43.124199Z","shell.execute_reply.started":"2022-07-07T07:31:43.108617Z","shell.execute_reply":"2022-07-07T07:31:43.123156Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.loc[:, cat_var].describe()","metadata":{"id":"jnZLF0g1Lqro","outputId":"40d617c4-81d2-46b1-b31a-abb2a10a171f","execution":{"iopub.status.busy":"2022-07-07T07:31:43.125895Z","iopub.execute_input":"2022-07-07T07:31:43.127194Z","iopub.status.idle":"2022-07-07T07:31:43.160073Z","shell.execute_reply.started":"2022-07-07T07:31:43.127125Z","shell.execute_reply":"2022-07-07T07:31:43.158887Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Handle Destination and HomePlanet null values","metadata":{"id":"gS7PiSQYRFUF"}},{"cell_type":"code","source":"# handle Destination and HomePlanet columns null values\ncat_imputer = SimpleImputer(strategy='most_frequent')\ntrain_df.loc[:, ['HomePlanet', 'Destination']] = cat_imputer.fit_transform(train_df[['HomePlanet', 'Destination']])","metadata":{"id":"bZBL9r6ML5mL","execution":{"iopub.status.busy":"2022-07-07T07:31:43.162555Z","iopub.execute_input":"2022-07-07T07:31:43.163222Z","iopub.status.idle":"2022-07-07T07:31:43.176889Z","shell.execute_reply.started":"2022-07-07T07:31:43.163186Z","shell.execute_reply":"2022-07-07T07:31:43.175822Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.loc[:, ['HomePlanet', 'Destination']] = cat_imputer.transform(test_df[['HomePlanet', 'Destination']])","metadata":{"id":"zekiqDxMMyT2","execution":{"iopub.status.busy":"2022-07-07T07:31:43.178688Z","iopub.execute_input":"2022-07-07T07:31:43.179091Z","iopub.status.idle":"2022-07-07T07:31:43.190588Z","shell.execute_reply.started":"2022-07-07T07:31:43.179053Z","shell.execute_reply":"2022-07-07T07:31:43.189386Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_null(train_df)","metadata":{"id":"_SGGwYHeM4rJ","outputId":"7f58a2d5-08f7-4fa0-e087-a70e4b4f6ced","execution":{"iopub.status.busy":"2022-07-07T07:31:43.197491Z","iopub.execute_input":"2022-07-07T07:31:43.197747Z","iopub.status.idle":"2022-07-07T07:31:43.469458Z","shell.execute_reply.started":"2022-07-07T07:31:43.197721Z","shell.execute_reply":"2022-07-07T07:31:43.468362Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_null(test_df)","metadata":{"id":"ZOCOlOSoM6Sp","outputId":"32e6648d-0697-4b53-c6dc-efcd7379e5e9","execution":{"iopub.status.busy":"2022-07-07T07:31:43.470906Z","iopub.execute_input":"2022-07-07T07:31:43.471403Z","iopub.status.idle":"2022-07-07T07:31:43.858195Z","shell.execute_reply.started":"2022-07-07T07:31:43.471363Z","shell.execute_reply":"2022-07-07T07:31:43.857251Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Make 3 new columns consist of cabin deck, number, and side","metadata":{"id":"-LBlgKM9O60J"}},{"cell_type":"code","source":"cabin_split_train_df = train_df['Cabin'].str.split('/', expand=True)\ncabin_split_train_df.columns = ['cabin_deck', 'cabin_deck_num', 'cabin_side']\ncabin_split_train_df.head()","metadata":{"id":"Qx1V-IeMM8h7","outputId":"e5219cea-5a39-44e7-96a1-05c826380019","execution":{"iopub.status.busy":"2022-07-07T07:31:43.859684Z","iopub.execute_input":"2022-07-07T07:31:43.860268Z","iopub.status.idle":"2022-07-07T07:31:43.887971Z","shell.execute_reply.started":"2022-07-07T07:31:43.860227Z","shell.execute_reply":"2022-07-07T07:31:43.887025Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cabin_split_test_df = test_df['Cabin'].str.split('/', expand=True)\ncabin_split_test_df.columns = ['cabin_deck', 'cabin_deck_num', 'cabin_side']\ncabin_split_test_df.head()","metadata":{"id":"eOPGmtCYPDJr","outputId":"62e038e7-eb04-4f01-8289-ba6215e42c0e","execution":{"iopub.status.busy":"2022-07-07T07:31:43.889224Z","iopub.execute_input":"2022-07-07T07:31:43.889996Z","iopub.status.idle":"2022-07-07T07:31:43.911064Z","shell.execute_reply.started":"2022-07-07T07:31:43.889954Z","shell.execute_reply":"2022-07-07T07:31:43.910129Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.concat([train_df, cabin_split_train_df], axis=1).drop('Cabin', axis=1)\ntest_df = pd.concat([test_df, cabin_split_test_df], axis=1).drop('Cabin', axis=1) ","metadata":{"id":"t4LrYiv7OgeB","execution":{"iopub.status.busy":"2022-07-07T07:31:43.91434Z","iopub.execute_input":"2022-07-07T07:31:43.914617Z","iopub.status.idle":"2022-07-07T07:31:43.930783Z","shell.execute_reply.started":"2022-07-07T07:31:43.914593Z","shell.execute_reply":"2022-07-07T07:31:43.929855Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"id":"v9dc8DBzPREo","outputId":"d8ad30cd-5da9-417c-978e-0ba29b2c6e90","execution":{"iopub.status.busy":"2022-07-07T07:31:43.93255Z","iopub.execute_input":"2022-07-07T07:31:43.933311Z","iopub.status.idle":"2022-07-07T07:31:43.955962Z","shell.execute_reply.started":"2022-07-07T07:31:43.933272Z","shell.execute_reply":"2022-07-07T07:31:43.955109Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.head()","metadata":{"id":"hmCGjI9mPSXI","outputId":"fcfb02a8-eb0d-4cdd-ecee-d894302a7950","execution":{"iopub.status.busy":"2022-07-07T07:31:43.957002Z","iopub.execute_input":"2022-07-07T07:31:43.957262Z","iopub.status.idle":"2022-07-07T07:31:43.977345Z","shell.execute_reply.started":"2022-07-07T07:31:43.957238Z","shell.execute_reply":"2022-07-07T07:31:43.976165Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cat_var_new = train_df.select_dtypes('O').columns.values","metadata":{"id":"5038wcK0PTNS","execution":{"iopub.status.busy":"2022-07-07T07:31:43.979077Z","iopub.execute_input":"2022-07-07T07:31:43.97982Z","iopub.status.idle":"2022-07-07T07:31:43.987374Z","shell.execute_reply.started":"2022-07-07T07:31:43.979783Z","shell.execute_reply":"2022-07-07T07:31:43.986529Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.loc[:, cat_var_new].describe()","metadata":{"id":"hjvMMP7YPWdo","outputId":"dfe2027b-e922-488b-845d-814c4070e737","execution":{"iopub.status.busy":"2022-07-07T07:31:43.989108Z","iopub.execute_input":"2022-07-07T07:31:43.989532Z","iopub.status.idle":"2022-07-07T07:31:44.030573Z","shell.execute_reply.started":"2022-07-07T07:31:43.98949Z","shell.execute_reply":"2022-07-07T07:31:44.029541Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.loc[:, cat_var_new].describe()","metadata":{"id":"qkJAm1VvQCK6","outputId":"5dc7a6fe-c5fa-4fa8-b22c-9d842d1eb296","execution":{"iopub.status.busy":"2022-07-07T07:31:44.032112Z","iopub.execute_input":"2022-07-07T07:31:44.032681Z","iopub.status.idle":"2022-07-07T07:31:44.064135Z","shell.execute_reply.started":"2022-07-07T07:31:44.032644Z","shell.execute_reply":"2022-07-07T07:31:44.063275Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Cabin deck's number can be represent with the cabin deck and cabin side, so I'll drop that column","metadata":{"id":"i4x_v7dCP2UF"}},{"cell_type":"code","source":"train_df.drop('cabin_deck_num', axis=1, inplace=True)\ntest_df.drop('cabin_deck_num', axis=1, inplace=True)","metadata":{"id":"f-MV7e6aPkbr","execution":{"iopub.status.busy":"2022-07-07T07:31:44.065185Z","iopub.execute_input":"2022-07-07T07:31:44.065506Z","iopub.status.idle":"2022-07-07T07:31:44.073056Z","shell.execute_reply.started":"2022-07-07T07:31:44.065469Z","shell.execute_reply":"2022-07-07T07:31:44.072036Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cat_var_new = train_df.select_dtypes('O').columns.values","metadata":{"id":"ZHjtnIELRwxZ","execution":{"iopub.status.busy":"2022-07-07T07:31:44.074731Z","iopub.execute_input":"2022-07-07T07:31:44.075424Z","iopub.status.idle":"2022-07-07T07:31:44.084104Z","shell.execute_reply.started":"2022-07-07T07:31:44.075386Z","shell.execute_reply":"2022-07-07T07:31:44.083023Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Handle cabin_deck and cabin_side null values","metadata":{"id":"QOoHragERLi6"}},{"cell_type":"code","source":"train_df.loc[:, ['cabin_deck', 'cabin_side']] = cat_imputer.fit_transform(train_df.loc[:, ['cabin_deck', 'cabin_side']])\ntest_df.loc[:, ['cabin_deck', 'cabin_side']] = cat_imputer.transform(test_df.loc[:, ['cabin_deck', 'cabin_side']])","metadata":{"id":"lr6sS3XxQrev","execution":{"iopub.status.busy":"2022-07-07T07:31:44.085378Z","iopub.execute_input":"2022-07-07T07:31:44.08578Z","iopub.status.idle":"2022-07-07T07:31:44.106255Z","shell.execute_reply.started":"2022-07-07T07:31:44.085745Z","shell.execute_reply":"2022-07-07T07:31:44.105134Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_null(train_df.loc[:, cat_var_new])","metadata":{"id":"L9OyS38BRfLV","outputId":"1e3dc816-2ae3-4cb3-efd9-cb069fbe9839","execution":{"iopub.status.busy":"2022-07-07T07:31:44.107845Z","iopub.execute_input":"2022-07-07T07:31:44.108364Z","iopub.status.idle":"2022-07-07T07:31:44.31605Z","shell.execute_reply.started":"2022-07-07T07:31:44.108311Z","shell.execute_reply":"2022-07-07T07:31:44.315085Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_null(test_df.loc[:, cat_var_new])","metadata":{"id":"xaeQne_tRqFU","outputId":"4c280703-b556-4dd5-e52f-80f0f2792f84","execution":{"iopub.status.busy":"2022-07-07T07:31:44.317317Z","iopub.execute_input":"2022-07-07T07:31:44.318077Z","iopub.status.idle":"2022-07-07T07:31:44.521696Z","shell.execute_reply.started":"2022-07-07T07:31:44.318037Z","shell.execute_reply":"2022-07-07T07:31:44.520745Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df[['CryoSleep', 'VIP']].describe()","metadata":{"id":"rGbsz3BuR3Iy","outputId":"db95403e-7685-46ec-bed7-02052c5c5653","execution":{"iopub.status.busy":"2022-07-07T07:31:44.523138Z","iopub.execute_input":"2022-07-07T07:31:44.523731Z","iopub.status.idle":"2022-07-07T07:31:44.543255Z","shell.execute_reply.started":"2022-07-07T07:31:44.523693Z","shell.execute_reply":"2022-07-07T07:31:44.542288Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Handle CryoSleep and VIP null values","metadata":{"id":"41iskWCTSIoG"}},{"cell_type":"code","source":"for var in num_var:\n  sns.boxplot(x='CryoSleep', y=var, data=train_df)\n  plt.show()","metadata":{"id":"T2PrbDJ-R8nX","outputId":"b150590a-0a3c-4d6a-b21f-3447e8b81235","execution":{"iopub.status.busy":"2022-07-07T07:31:44.54473Z","iopub.execute_input":"2022-07-07T07:31:44.545087Z","iopub.status.idle":"2022-07-07T07:31:45.60048Z","shell.execute_reply.started":"2022-07-07T07:31:44.545052Z","shell.execute_reply":"2022-07-07T07:31:45.599455Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for var in num_var:\n  sns.boxplot(x='VIP', y=var, data=train_df)\n  plt.show()","metadata":{"id":"gCEP88V0Suvv","outputId":"d50cfb01-1ede-444c-90e3-ffb1da1a2f04","execution":{"iopub.status.busy":"2022-07-07T07:31:45.602086Z","iopub.execute_input":"2022-07-07T07:31:45.602695Z","iopub.status.idle":"2022-07-07T07:31:46.685174Z","shell.execute_reply.started":"2022-07-07T07:31:45.602654Z","shell.execute_reply":"2022-07-07T07:31:46.684028Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# make new column: total_bill\n\ntrain_df['total_bill'] = train_df.loc[:, num_var[1:]].sum(axis=1).values\ntest_df['total_bill'] = test_df.loc[:, num_var[1:]].sum(axis=1).values","metadata":{"id":"UjZtVZ0zS7Sk","execution":{"iopub.status.busy":"2022-07-07T07:31:46.686828Z","iopub.execute_input":"2022-07-07T07:31:46.687198Z","iopub.status.idle":"2022-07-07T07:31:46.698266Z","shell.execute_reply.started":"2022-07-07T07:31:46.687161Z","shell.execute_reply":"2022-07-07T07:31:46.69728Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"id":"4PTGZIxSTQIN","outputId":"ef3d9f94-f488-4ac7-df6b-73e39e734535","execution":{"iopub.status.busy":"2022-07-07T07:31:46.700406Z","iopub.execute_input":"2022-07-07T07:31:46.701009Z","iopub.status.idle":"2022-07-07T07:31:46.724997Z","shell.execute_reply.started":"2022-07-07T07:31:46.700956Z","shell.execute_reply":"2022-07-07T07:31:46.724053Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.head()","metadata":{"id":"sWjvnhVQTQ3O","outputId":"d0bf0ffe-6c11-4e01-e167-e74a4ed2f858","execution":{"iopub.status.busy":"2022-07-07T07:31:46.726166Z","iopub.execute_input":"2022-07-07T07:31:46.726493Z","iopub.status.idle":"2022-07-07T07:31:46.749893Z","shell.execute_reply.started":"2022-07-07T07:31:46.726466Z","shell.execute_reply":"2022-07-07T07:31:46.748998Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for var in ['CryoSleep', 'VIP']:\n  sns.boxplot(x=var, y='total_bill', data=train_df)\n  plt.show()","metadata":{"id":"Ibrtyl1LTbjc","outputId":"9716b556-f640-4386-fb94-59a822d01c71","execution":{"iopub.status.busy":"2022-07-07T07:31:46.753163Z","iopub.execute_input":"2022-07-07T07:31:46.754219Z","iopub.status.idle":"2022-07-07T07:31:47.134419Z","shell.execute_reply.started":"2022-07-07T07:31:46.75418Z","shell.execute_reply":"2022-07-07T07:31:47.133453Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for var in ['HomePlanet', 'Destination', 'cabin_deck', 'cabin_side']:\n  sns.countplot(x=var, data=train_df, hue='CryoSleep')\n  plt.show()","metadata":{"id":"vV-4Hni-TnaB","outputId":"af6aae05-b13b-46c9-e7c4-17012cc58b33","execution":{"iopub.status.busy":"2022-07-07T07:31:47.136035Z","iopub.execute_input":"2022-07-07T07:31:47.136437Z","iopub.status.idle":"2022-07-07T07:31:48.119393Z","shell.execute_reply.started":"2022-07-07T07:31:47.136398Z","shell.execute_reply":"2022-07-07T07:31:48.118465Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for var in ['HomePlanet', 'Destination', 'cabin_deck', 'cabin_side']:\n  sns.countplot(x=var, data=train_df, hue='VIP')\n  plt.show()","metadata":{"id":"HBW2fmu7ULBV","outputId":"08700f52-80f2-42a7-9e6d-48748fc1d6ef","execution":{"iopub.status.busy":"2022-07-07T07:31:48.12148Z","iopub.execute_input":"2022-07-07T07:31:48.122184Z","iopub.status.idle":"2022-07-07T07:31:48.942376Z","shell.execute_reply.started":"2022-07-07T07:31:48.12214Z","shell.execute_reply":"2022-07-07T07:31:48.941439Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Handling VIP and CryoSleep null values will be using classification model","metadata":{"id":"ZG-aKVE6U1do"}},{"cell_type":"markdown","source":"#### handle VIP null values","metadata":{"id":"eBKR4jjkYSBi"}},{"cell_type":"code","source":"train_vip_df = train_df.loc[:, np.append(num_var, ['total_bill', 'VIP'])].copy()\ntest_vip_df = test_df.loc[:, np.append(num_var, ['total_bill', 'VIP'])].copy()","metadata":{"id":"61sPMS61UsW3","execution":{"iopub.status.busy":"2022-07-07T07:31:48.944859Z","iopub.execute_input":"2022-07-07T07:31:48.945202Z","iopub.status.idle":"2022-07-07T07:31:48.953727Z","shell.execute_reply.started":"2022-07-07T07:31:48.945165Z","shell.execute_reply":"2022-07-07T07:31:48.952525Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_vip_df.shape, test_vip_df.shape","metadata":{"id":"Q2DsJoabV7YE","outputId":"e5d60d63-5697-48b6-faec-5885efcbdcde","execution":{"iopub.status.busy":"2022-07-07T07:31:48.956105Z","iopub.execute_input":"2022-07-07T07:31:48.956707Z","iopub.status.idle":"2022-07-07T07:31:48.963487Z","shell.execute_reply.started":"2022-07-07T07:31:48.95668Z","shell.execute_reply":"2022-07-07T07:31:48.962479Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_vip_df_nonull = train_vip_df.dropna(axis=0, how='any').copy()\ntrain_vip_df_null = train_vip_df.loc[train_vip_df['VIP'].isna(),:].drop('VIP', axis=1)\n\ntest_vip_df_null = test_vip_df.loc[test_vip_df['VIP'].isna(),:].drop('VIP', axis=1)\n\ntrain_vip_df_nonull.shape, train_vip_df_null.shape, test_vip_df_null.shape","metadata":{"id":"tICX68zmbYHM","outputId":"1584020a-80e9-47e9-d3a5-8ace88cbc2fe","execution":{"iopub.status.busy":"2022-07-07T07:31:48.964827Z","iopub.execute_input":"2022-07-07T07:31:48.965768Z","iopub.status.idle":"2022-07-07T07:31:48.984181Z","shell.execute_reply.started":"2022-07-07T07:31:48.96573Z","shell.execute_reply":"2022-07-07T07:31:48.983044Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_vip_null_index = train_vip_df.loc[train_vip_df['VIP'].isna(),:].index\ntest_vip_null_index = test_vip_df.loc[test_vip_df['VIP'].isna(),:].index","metadata":{"id":"_sqgsfbAhlY5","execution":{"iopub.status.busy":"2022-07-07T07:31:48.985488Z","iopub.execute_input":"2022-07-07T07:31:48.986312Z","iopub.status.idle":"2022-07-07T07:31:48.994146Z","shell.execute_reply.started":"2022-07-07T07:31:48.986276Z","shell.execute_reply":"2022-07-07T07:31:48.992995Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_vip_df_nonull.VIP.value_counts()","metadata":{"id":"7xS7tzdwWbuI","outputId":"6693d7ff-52f7-43c9-a28d-b82e40a42784","execution":{"iopub.status.busy":"2022-07-07T07:31:48.995859Z","iopub.execute_input":"2022-07-07T07:31:48.996226Z","iopub.status.idle":"2022-07-07T07:31:49.007446Z","shell.execute_reply.started":"2022-07-07T07:31:48.99619Z","shell.execute_reply":"2022-07-07T07:31:49.006452Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"encoder = LabelEncoder()\ntrain_vip_df_nonull.loc[:, 'VIP'] = encoder.fit_transform(train_vip_df_nonull.VIP)","metadata":{"id":"9EMAdUirXUj6","execution":{"iopub.status.busy":"2022-07-07T07:31:49.008989Z","iopub.execute_input":"2022-07-07T07:31:49.009706Z","iopub.status.idle":"2022-07-07T07:31:49.018635Z","shell.execute_reply.started":"2022-07-07T07:31:49.009669Z","shell.execute_reply":"2022-07-07T07:31:49.017722Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_vip = train_vip_df_nonull.drop('VIP', axis=1).values\ny_vip = train_vip_df_nonull[['VIP']].values.ravel()","metadata":{"id":"AEQXLjS3WmvY","execution":{"iopub.status.busy":"2022-07-07T07:31:49.019954Z","iopub.execute_input":"2022-07-07T07:31:49.020927Z","iopub.status.idle":"2022-07-07T07:31:49.029228Z","shell.execute_reply.started":"2022-07-07T07:31:49.020891Z","shell.execute_reply":"2022-07-07T07:31:49.028277Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sm = SMOTE(sampling_strategy=.75)\nX_vip_res, y_vip_res = sm.fit_resample(X_vip, y_vip)","metadata":{"id":"rCygzCEoW-_v","execution":{"iopub.status.busy":"2022-07-07T07:31:49.030624Z","iopub.execute_input":"2022-07-07T07:31:49.031321Z","iopub.status.idle":"2022-07-07T07:31:49.049999Z","shell.execute_reply.started":"2022-07-07T07:31:49.031279Z","shell.execute_reply":"2022-07-07T07:31:49.049073Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_vip_res.shape, y_vip_res.shape","metadata":{"id":"etUGT2d0XoXw","outputId":"28aa6d10-3efd-43cb-f818-dca1ecefdbe1","execution":{"iopub.status.busy":"2022-07-07T07:31:49.051447Z","iopub.execute_input":"2022-07-07T07:31:49.052182Z","iopub.status.idle":"2022-07-07T07:31:49.05832Z","shell.execute_reply.started":"2022-07-07T07:31:49.05214Z","shell.execute_reply":"2022-07-07T07:31:49.057385Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_vip_res.sum(), (1-y_vip_res).sum()","metadata":{"id":"d1i3MgGWXroi","outputId":"ea8e48d9-d49d-4641-96b8-01e7ede3a87f","execution":{"iopub.status.busy":"2022-07-07T07:31:49.059992Z","iopub.execute_input":"2022-07-07T07:31:49.060712Z","iopub.status.idle":"2022-07-07T07:31:49.070549Z","shell.execute_reply.started":"2022-07-07T07:31:49.060668Z","shell.execute_reply":"2022-07-07T07:31:49.069457Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scaler = StandardScaler()\nX_vip_res = scaler.fit_transform(X_vip_res)\ntrain_vip_df_null = scaler.transform(train_vip_df_null.values)\ntest_vip_df_null = scaler.transform(test_vip_df_null.values)","metadata":{"id":"gtQ898iuYehQ","execution":{"iopub.status.busy":"2022-07-07T07:31:49.071776Z","iopub.execute_input":"2022-07-07T07:31:49.072213Z","iopub.status.idle":"2022-07-07T07:31:49.083599Z","shell.execute_reply.started":"2022-07-07T07:31:49.072178Z","shell.execute_reply":"2022-07-07T07:31:49.082553Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_vip, X_val_vip, y_train_vip, y_val_vip = train_test_split(X_vip_res, y_vip_res, test_size=.2, random_state=42)\n\nX_train_vip.shape, X_val_vip.shape, y_train_vip.shape, y_val_vip.shape","metadata":{"id":"uZog-RU9X9jn","outputId":"e148022e-1fbb-46b5-95c6-ebd4d5eddc0f","execution":{"iopub.status.busy":"2022-07-07T07:31:49.085289Z","iopub.execute_input":"2022-07-07T07:31:49.086046Z","iopub.status.idle":"2022-07-07T07:31:49.097011Z","shell.execute_reply.started":"2022-07-07T07:31:49.086006Z","shell.execute_reply":"2022-07-07T07:31:49.09603Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"forest_vip_clf = RandomForestClassifier()\n\nforest_vip_clf.fit(X_train_vip, y_train_vip)","metadata":{"id":"UYy_nYJpYA1m","outputId":"0bc41294-dc4d-441c-a6a3-ac4e2194a16e","execution":{"iopub.status.busy":"2022-07-07T07:31:49.098595Z","iopub.execute_input":"2022-07-07T07:31:49.099421Z","iopub.status.idle":"2022-07-07T07:31:50.470241Z","shell.execute_reply.started":"2022-07-07T07:31:49.099384Z","shell.execute_reply":"2022-07-07T07:31:50.469315Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import classification_report\n\nprint(classification_report(y_val_vip, forest_vip_clf.predict(X_val_vip)))","metadata":{"id":"TvZ-AeBbYrLU","outputId":"c5c4672b-7e49-4f30-a6b8-36f110abc127","execution":{"iopub.status.busy":"2022-07-07T07:31:50.471706Z","iopub.execute_input":"2022-07-07T07:31:50.472151Z","iopub.status.idle":"2022-07-07T07:31:50.551592Z","shell.execute_reply.started":"2022-07-07T07:31:50.472112Z","shell.execute_reply":"2022-07-07T07:31:50.550573Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"replace_null_vip_on_train_df = forest_vip_clf.predict(train_vip_df_null)\nreplace_null_vip_on_train_df = np.where(replace_null_vip_on_train_df==0, False, True)\nreplace_null_vip_on_train_df","metadata":{"id":"aBMmr_sEaQgD","outputId":"fa926170-82ec-400b-dfa5-77b6b11b6bd1","execution":{"iopub.status.busy":"2022-07-07T07:31:50.554457Z","iopub.execute_input":"2022-07-07T07:31:50.554785Z","iopub.status.idle":"2022-07-07T07:31:50.582654Z","shell.execute_reply.started":"2022-07-07T07:31:50.554757Z","shell.execute_reply":"2022-07-07T07:31:50.581705Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"replace_null_vip_on_test_df = forest_vip_clf.predict(test_vip_df_null)\nreplace_null_vip_on_test_df = np.where(replace_null_vip_on_test_df==0, False, True)\nreplace_null_vip_on_test_df","metadata":{"id":"M_8D4CKZarvr","outputId":"1eeca168-f104-4147-95fc-b8cc90bc32c4","execution":{"iopub.status.busy":"2022-07-07T07:31:50.584345Z","iopub.execute_input":"2022-07-07T07:31:50.584982Z","iopub.status.idle":"2022-07-07T07:31:50.609663Z","shell.execute_reply.started":"2022-07-07T07:31:50.584945Z","shell.execute_reply":"2022-07-07T07:31:50.608673Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.loc[train_vip_null_index, 'VIP'] = replace_null_vip_on_train_df\ntrain_df.loc[train_vip_null_index, :].head()","metadata":{"id":"CmXUusJ0dNKO","outputId":"3ac95122-693e-4fc6-99f9-3ee911a329e3","execution":{"iopub.status.busy":"2022-07-07T07:31:50.611262Z","iopub.execute_input":"2022-07-07T07:31:50.611925Z","iopub.status.idle":"2022-07-07T07:31:50.636751Z","shell.execute_reply.started":"2022-07-07T07:31:50.611887Z","shell.execute_reply":"2022-07-07T07:31:50.635579Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_null(train_df.loc[:, cat_var_new])","metadata":{"id":"9pb38HLUepS4","outputId":"cd3ddfad-05a6-4d42-c344-c4e37601febe","execution":{"iopub.status.busy":"2022-07-07T07:31:50.638637Z","iopub.execute_input":"2022-07-07T07:31:50.639393Z","iopub.status.idle":"2022-07-07T07:31:50.8504Z","shell.execute_reply.started":"2022-07-07T07:31:50.639352Z","shell.execute_reply":"2022-07-07T07:31:50.849278Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.loc[test_vip_null_index, 'VIP'] = replace_null_vip_on_test_df\ntest_df.loc[test_vip_null_index, :].head()","metadata":{"id":"wOpfsYebew5S","outputId":"77f7bcb5-9314-4b2f-e653-5b2da72625b4","execution":{"iopub.status.busy":"2022-07-07T07:31:50.851903Z","iopub.execute_input":"2022-07-07T07:31:50.852213Z","iopub.status.idle":"2022-07-07T07:31:50.874311Z","shell.execute_reply.started":"2022-07-07T07:31:50.852187Z","shell.execute_reply":"2022-07-07T07:31:50.872978Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_null(test_df.loc[:, cat_var_new])","metadata":{"id":"yhf3q3lpe6xj","outputId":"c2d223d4-3aba-43aa-89ab-5a624c7c6eee","execution":{"iopub.status.busy":"2022-07-07T07:31:50.876537Z","iopub.execute_input":"2022-07-07T07:31:50.877528Z","iopub.status.idle":"2022-07-07T07:31:51.083328Z","shell.execute_reply.started":"2022-07-07T07:31:50.877484Z","shell.execute_reply":"2022-07-07T07:31:51.082284Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### handle CryoSleep null values","metadata":{"id":"SqfUcYLse_Yg"}},{"cell_type":"code","source":"train_cryo_df = train_df.loc[:, np.append(num_var, ['total_bill', 'HomePlanet', 'cabin_deck', 'CryoSleep'])].copy()\ntest_cryo_df = test_df.loc[:, np.append(num_var, ['total_bill', 'HomePlanet', 'cabin_deck', 'CryoSleep'])].copy()","metadata":{"id":"gpO4w1s6fU0X","execution":{"iopub.status.busy":"2022-07-07T07:31:51.085391Z","iopub.execute_input":"2022-07-07T07:31:51.086053Z","iopub.status.idle":"2022-07-07T07:31:51.094712Z","shell.execute_reply.started":"2022-07-07T07:31:51.086014Z","shell.execute_reply":"2022-07-07T07:31:51.093645Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_cryo_df.shape, test_cryo_df.shape","metadata":{"outputId":"d4ed0fa1-c595-4f04-bc58-425652180461","id":"hVkhfdd3fU0Y","execution":{"iopub.status.busy":"2022-07-07T07:31:51.10809Z","iopub.execute_input":"2022-07-07T07:31:51.108811Z","iopub.status.idle":"2022-07-07T07:31:51.115535Z","shell.execute_reply.started":"2022-07-07T07:31:51.108774Z","shell.execute_reply":"2022-07-07T07:31:51.114249Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_cryo_df_nonull = train_cryo_df.dropna(axis=0, how='any').copy()\ntrain_cryo_df_null = train_cryo_df.loc[train_cryo_df['CryoSleep'].isna(),:].drop('CryoSleep', axis=1)\n\ntest_cryo_df_nonull = test_cryo_df.dropna(axis=0, how='any').copy()\ntest_cryo_df_null = test_cryo_df.loc[test_cryo_df['CryoSleep'].isna(),:].drop('CryoSleep', axis=1)\n\ntrain_cryo_df_nonull.shape, train_cryo_df_null.shape, test_cryo_df_nonull.shape, test_cryo_df_null.shape","metadata":{"outputId":"64abe270-b31a-4e95-987a-2146b6e00567","id":"X3D_MMnxfU0Y","execution":{"iopub.status.busy":"2022-07-07T07:31:51.11711Z","iopub.execute_input":"2022-07-07T07:31:51.118066Z","iopub.status.idle":"2022-07-07T07:31:51.141386Z","shell.execute_reply.started":"2022-07-07T07:31:51.117999Z","shell.execute_reply":"2022-07-07T07:31:51.140228Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_cryo_null_index = train_cryo_df.loc[train_cryo_df['CryoSleep'].isna(),:].index\ntest_cryo_null_index = test_cryo_df.loc[test_cryo_df['CryoSleep'].isna(),:].index","metadata":{"id":"C_5uQv7JoAyC","execution":{"iopub.status.busy":"2022-07-07T07:31:51.143132Z","iopub.execute_input":"2022-07-07T07:31:51.143882Z","iopub.status.idle":"2022-07-07T07:31:51.152141Z","shell.execute_reply.started":"2022-07-07T07:31:51.143844Z","shell.execute_reply":"2022-07-07T07:31:51.150993Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_cryo_df_nonull.CryoSleep.value_counts()","metadata":{"outputId":"571deef1-68fc-40a1-cfc9-0f2ccd407ced","id":"bjsqYcF2fU0Z","execution":{"iopub.status.busy":"2022-07-07T07:31:51.15384Z","iopub.execute_input":"2022-07-07T07:31:51.155316Z","iopub.status.idle":"2022-07-07T07:31:51.166449Z","shell.execute_reply.started":"2022-07-07T07:31:51.155285Z","shell.execute_reply":"2022-07-07T07:31:51.165257Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_cryo_df_nonull.CryoSleep.value_counts()","metadata":{"id":"aiQonygllSkI","outputId":"c0236564-b878-4b6c-a7be-02e7b5853039","execution":{"iopub.status.busy":"2022-07-07T07:31:51.167873Z","iopub.execute_input":"2022-07-07T07:31:51.168208Z","iopub.status.idle":"2022-07-07T07:31:51.177939Z","shell.execute_reply.started":"2022-07-07T07:31:51.168175Z","shell.execute_reply":"2022-07-07T07:31:51.176911Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_cryo_df_nonull = pd.concat([train_cryo_df_nonull, test_cryo_df_nonull], ignore_index=True)\ntrain_cryo_df_nonull","metadata":{"id":"9so1NwFKld9Z","outputId":"e53da9a1-056d-4b5f-da8a-681398b78198","execution":{"iopub.status.busy":"2022-07-07T07:31:51.17955Z","iopub.execute_input":"2022-07-07T07:31:51.180177Z","iopub.status.idle":"2022-07-07T07:31:51.208511Z","shell.execute_reply.started":"2022-07-07T07:31:51.18014Z","shell.execute_reply":"2022-07-07T07:31:51.207529Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"classencoder = LabelEncoder()\ntrain_cryo_df_nonull.loc[:, 'CryoSleep'] = classencoder.fit_transform(train_cryo_df_nonull.CryoSleep)","metadata":{"id":"-6k_IZfbfU0Z","execution":{"iopub.status.busy":"2022-07-07T07:31:51.210087Z","iopub.execute_input":"2022-07-07T07:31:51.210749Z","iopub.status.idle":"2022-07-07T07:31:51.220892Z","shell.execute_reply.started":"2022-07-07T07:31:51.210712Z","shell.execute_reply":"2022-07-07T07:31:51.219941Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ordinalencoder = OrdinalEncoder()\n\ntrain_cryo_df_nonull.loc[:, ['HomePlanet', 'cabin_deck']] = ordinalencoder.fit_transform(train_cryo_df_nonull[['HomePlanet', 'cabin_deck']])\ntrain_cryo_df_null.loc[:, ['HomePlanet', 'cabin_deck']] = ordinalencoder.transform(train_cryo_df_null[['HomePlanet', 'cabin_deck']])\ntest_cryo_df_null.loc[:, ['HomePlanet', 'cabin_deck']] = ordinalencoder.transform(test_cryo_df_null[['HomePlanet', 'cabin_deck']])","metadata":{"id":"U8w0E7Llf0vp","execution":{"iopub.status.busy":"2022-07-07T07:31:51.222743Z","iopub.execute_input":"2022-07-07T07:31:51.223176Z","iopub.status.idle":"2022-07-07T07:31:51.253017Z","shell.execute_reply.started":"2022-07-07T07:31:51.22314Z","shell.execute_reply":"2022-07-07T07:31:51.2521Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_cryo = train_cryo_df_nonull.drop('CryoSleep', axis=1).values\ny_cryo = train_cryo_df_nonull.CryoSleep.values","metadata":{"id":"monKmtxYms73","execution":{"iopub.status.busy":"2022-07-07T07:31:51.254656Z","iopub.execute_input":"2022-07-07T07:31:51.255321Z","iopub.status.idle":"2022-07-07T07:31:51.263225Z","shell.execute_reply.started":"2022-07-07T07:31:51.255285Z","shell.execute_reply":"2022-07-07T07:31:51.262284Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scaler = StandardScaler()\n\nX_cryo = scaler.fit_transform(X_cryo)\ntrain_cryo_df_null = scaler.transform(train_cryo_df_null.values)\ntest_cryo_df_null = scaler.transform(test_cryo_df_null.values)","metadata":{"id":"g8w_hOCMmfHY","execution":{"iopub.status.busy":"2022-07-07T07:31:51.265305Z","iopub.execute_input":"2022-07-07T07:31:51.265822Z","iopub.status.idle":"2022-07-07T07:31:51.277299Z","shell.execute_reply.started":"2022-07-07T07:31:51.265782Z","shell.execute_reply":"2022-07-07T07:31:51.276125Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_cryo_train, X_cryo_val, y_cryo_train, y_cryo_val = train_test_split(X_cryo, y_cryo, test_size=.2, random_state=42)\n\nX_cryo_train.shape, X_cryo_val.shape, y_cryo_train.shape, y_cryo_val.shape","metadata":{"id":"CEQL17DNm7X3","outputId":"73d456f0-3310-42d8-eaa3-a31a536b6fec","execution":{"iopub.status.busy":"2022-07-07T07:31:51.278668Z","iopub.execute_input":"2022-07-07T07:31:51.280577Z","iopub.status.idle":"2022-07-07T07:31:51.291481Z","shell.execute_reply.started":"2022-07-07T07:31:51.280538Z","shell.execute_reply":"2022-07-07T07:31:51.290613Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"forest_cryo_clf = RandomForestClassifier()\n\nforest_cryo_clf.fit(X_cryo_train, y_cryo_train)","metadata":{"id":"d4DJ1wF2nJkl","outputId":"0303008d-67f1-41a9-864b-3b86362c362e","execution":{"iopub.status.busy":"2022-07-07T07:31:51.293428Z","iopub.execute_input":"2022-07-07T07:31:51.294156Z","iopub.status.idle":"2022-07-07T07:31:51.895464Z","shell.execute_reply.started":"2022-07-07T07:31:51.294113Z","shell.execute_reply":"2022-07-07T07:31:51.894352Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(classification_report(y_cryo_val, forest_cryo_clf.predict(X_cryo_val)))","metadata":{"id":"YtL82tMunP69","outputId":"349871a5-eab4-4614-b6d4-12cc31d6d4ca","execution":{"iopub.status.busy":"2022-07-07T07:31:51.896806Z","iopub.execute_input":"2022-07-07T07:31:51.897378Z","iopub.status.idle":"2022-07-07T07:31:51.957625Z","shell.execute_reply.started":"2022-07-07T07:31:51.897319Z","shell.execute_reply":"2022-07-07T07:31:51.956504Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"replace_null_cryo_on_train_df = forest_cryo_clf.predict(train_cryo_df_null)\nreplace_null_cryo_on_train_df = np.where(replace_null_cryo_on_train_df==0, False, True)\nreplace_null_cryo_on_train_df","metadata":{"outputId":"b29f2d8e-1601-43b3-885f-0a48a7b6da3a","id":"xygqsHiKnwL_","execution":{"iopub.status.busy":"2022-07-07T07:31:51.959374Z","iopub.execute_input":"2022-07-07T07:31:51.959774Z","iopub.status.idle":"2022-07-07T07:31:51.987858Z","shell.execute_reply.started":"2022-07-07T07:31:51.959733Z","shell.execute_reply":"2022-07-07T07:31:51.986858Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"replace_null_cryo_on_test_df = forest_cryo_clf.predict(test_cryo_df_null)\nreplace_null_cryo_on_test_df = np.where(replace_null_cryo_on_test_df==0, False, True)\nreplace_null_cryo_on_test_df","metadata":{"outputId":"92dcc23d-fd01-4936-f632-652885f2f7f8","id":"WKUg-FoLnwMA","execution":{"iopub.status.busy":"2022-07-07T07:31:51.989349Z","iopub.execute_input":"2022-07-07T07:31:51.98978Z","iopub.status.idle":"2022-07-07T07:31:52.014011Z","shell.execute_reply.started":"2022-07-07T07:31:51.989742Z","shell.execute_reply":"2022-07-07T07:31:52.013016Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.loc[train_cryo_null_index, 'CryoSleep'] = replace_null_cryo_on_train_df\ntrain_df.loc[train_cryo_null_index, :].head()","metadata":{"outputId":"248702cc-a1b1-4ee6-cf72-a97110354613","id":"oZOTF5C6nwMA","execution":{"iopub.status.busy":"2022-07-07T07:31:52.015392Z","iopub.execute_input":"2022-07-07T07:31:52.015841Z","iopub.status.idle":"2022-07-07T07:31:52.038466Z","shell.execute_reply.started":"2022-07-07T07:31:52.0158Z","shell.execute_reply":"2022-07-07T07:31:52.037417Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_null(train_df.loc[:, cat_var_new])","metadata":{"outputId":"b9e405cd-5b3e-48fe-9789-1c140b7d7232","id":"cyFk_I_ZnwMB","execution":{"iopub.status.busy":"2022-07-07T07:31:52.03996Z","iopub.execute_input":"2022-07-07T07:31:52.040804Z","iopub.status.idle":"2022-07-07T07:31:52.262349Z","shell.execute_reply.started":"2022-07-07T07:31:52.040619Z","shell.execute_reply":"2022-07-07T07:31:52.261298Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.loc[test_cryo_null_index, 'CryoSleep'] = replace_null_cryo_on_test_df\ntest_df.loc[test_cryo_null_index, :].head()","metadata":{"outputId":"5621f777-e2eb-4555-9cb3-09aa4ead2a2e","id":"MO2OO6iNnwMB","execution":{"iopub.status.busy":"2022-07-07T07:31:52.263905Z","iopub.execute_input":"2022-07-07T07:31:52.264242Z","iopub.status.idle":"2022-07-07T07:31:52.28636Z","shell.execute_reply.started":"2022-07-07T07:31:52.264206Z","shell.execute_reply":"2022-07-07T07:31:52.285225Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_null(test_df.loc[:, cat_var_new])","metadata":{"outputId":"71e567da-e07e-4312-ff46-0aec59636257","id":"W1TvtheLnwMB","execution":{"iopub.status.busy":"2022-07-07T07:31:52.289003Z","iopub.execute_input":"2022-07-07T07:31:52.28951Z","iopub.status.idle":"2022-07-07T07:31:52.492038Z","shell.execute_reply.started":"2022-07-07T07:31:52.289469Z","shell.execute_reply":"2022-07-07T07:31:52.491115Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## ALL NULL VALUES SUCCESSFULLY HANDLED","metadata":{"id":"3oAo3gNLo0pI"}},{"cell_type":"code","source":"plot_null(train_df)","metadata":{"id":"L3qPQdRDouR5","outputId":"27c08d5b-a61d-41fb-c3ee-8426a2135eae","execution":{"iopub.status.busy":"2022-07-07T07:31:52.493277Z","iopub.execute_input":"2022-07-07T07:31:52.494037Z","iopub.status.idle":"2022-07-07T07:31:52.782157Z","shell.execute_reply.started":"2022-07-07T07:31:52.493996Z","shell.execute_reply":"2022-07-07T07:31:52.781242Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_null(test_df)","metadata":{"id":"gtPWCP_Poyzi","outputId":"6a54ef0a-48e1-4cb3-cc45-81a6a9b83a4f","execution":{"iopub.status.busy":"2022-07-07T07:31:52.784376Z","iopub.execute_input":"2022-07-07T07:31:52.785506Z","iopub.status.idle":"2022-07-07T07:31:53.060319Z","shell.execute_reply.started":"2022-07-07T07:31:52.785448Z","shell.execute_reply":"2022-07-07T07:31:53.059316Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.info()","metadata":{"id":"bXEIQqoko5xH","outputId":"bc28219b-141d-426c-ddff-affbaf38a1ca","execution":{"iopub.status.busy":"2022-07-07T07:31:53.062011Z","iopub.execute_input":"2022-07-07T07:31:53.063118Z","iopub.status.idle":"2022-07-07T07:31:53.08142Z","shell.execute_reply.started":"2022-07-07T07:31:53.063076Z","shell.execute_reply":"2022-07-07T07:31:53.080377Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.info()","metadata":{"id":"kSHrcOq3pFCy","outputId":"b48ac370-52a3-408a-e3ec-5eafe11a77a8","execution":{"iopub.status.busy":"2022-07-07T07:31:53.082842Z","iopub.execute_input":"2022-07-07T07:31:53.083443Z","iopub.status.idle":"2022-07-07T07:31:53.101881Z","shell.execute_reply.started":"2022-07-07T07:31:53.083401Z","shell.execute_reply":"2022-07-07T07:31:53.100657Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Make preprocessing pipeline for modelling","metadata":{"id":"xbR11ETcqvu5"}},{"cell_type":"code","source":"class CustomOneHotEncoder(BaseEstimator, TransformerMixin):\n  def __init__(self, cat_var=cat_var_new):\n    self.cat_var = cat_var_new\n    self.encoder = OneHotEncoder()\n  \n  def fit(self, df):\n    self.encoder.fit(df.loc[:, self.cat_var])\n    return self\n  \n  def transform(self, df):\n    onehot_arr = self.encoder.transform(df.loc[:, self.cat_var]).toarray()\n    onehot_df = pd.DataFrame(onehot_arr)\n    df_new = df.drop(self.cat_var, axis=1).copy()\n    return pd.concat([onehot_df, df_new], axis=1)","metadata":{"id":"bQtwQ1XUqbVO","execution":{"iopub.status.busy":"2022-07-07T07:31:53.103455Z","iopub.execute_input":"2022-07-07T07:31:53.104013Z","iopub.status.idle":"2022-07-07T07:31:53.114754Z","shell.execute_reply.started":"2022-07-07T07:31:53.103981Z","shell.execute_reply":"2022-07-07T07:31:53.112893Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preprocess_pipeline = Pipeline([('encoder', CustomOneHotEncoder()),\n                                ('scaler', StandardScaler())])","metadata":{"id":"CyE9l_eltlOA","execution":{"iopub.status.busy":"2022-07-07T07:31:53.118668Z","iopub.execute_input":"2022-07-07T07:31:53.119749Z","iopub.status.idle":"2022-07-07T07:31:53.132004Z","shell.execute_reply.started":"2022-07-07T07:31:53.119709Z","shell.execute_reply":"2022-07-07T07:31:53.130629Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = train_df.drop('Transported', axis=1).copy()\ny = train_df.Transported.values.ravel()","metadata":{"id":"Gzb1_0Q8v5h2","execution":{"iopub.status.busy":"2022-07-07T07:31:53.13345Z","iopub.execute_input":"2022-07-07T07:31:53.134Z","iopub.status.idle":"2022-07-07T07:31:53.144809Z","shell.execute_reply.started":"2022-07-07T07:31:53.133964Z","shell.execute_reply":"2022-07-07T07:31:53.143944Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"encoder = LabelEncoder()\n\ny = encoder.fit_transform(y)","metadata":{"id":"JzONAQYQwGWG","execution":{"iopub.status.busy":"2022-07-07T07:31:53.14692Z","iopub.execute_input":"2022-07-07T07:31:53.154524Z","iopub.status.idle":"2022-07-07T07:31:53.166261Z","shell.execute_reply.started":"2022-07-07T07:31:53.154489Z","shell.execute_reply":"2022-07-07T07:31:53.163498Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = preprocess_pipeline.fit_transform(X)\nX.shape","metadata":{"id":"snwxkAolw7PY","outputId":"80fd24a7-7f61-4927-dae0-fc7256cedd02","execution":{"iopub.status.busy":"2022-07-07T07:31:53.170869Z","iopub.execute_input":"2022-07-07T07:31:53.171633Z","iopub.status.idle":"2022-07-07T07:31:53.2372Z","shell.execute_reply.started":"2022-07-07T07:31:53.171589Z","shell.execute_reply":"2022-07-07T07:31:53.236014Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_set = preprocess_pipeline.transform(test_df)\ntest_set.shape","metadata":{"id":"GO2e2x6Vw_pT","outputId":"6be28eca-ab11-408e-ace9-80b1c8fd5d94","execution":{"iopub.status.busy":"2022-07-07T07:31:53.239162Z","iopub.execute_input":"2022-07-07T07:31:53.240676Z","iopub.status.idle":"2022-07-07T07:31:53.296042Z","shell.execute_reply.started":"2022-07-07T07:31:53.240634Z","shell.execute_reply":"2022-07-07T07:31:53.294692Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Split train and val set","metadata":{"id":"ERMWszxUxXJu"}},{"cell_type":"code","source":"X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=.2, random_state=42)\n\nX_train.shape, X_val.shape, y_train.shape, y_val.shape","metadata":{"id":"VzIe1bbLxS8x","outputId":"7abb7690-2856-4f81-c691-5a91edc922cd","execution":{"iopub.status.busy":"2022-07-07T07:31:53.298618Z","iopub.execute_input":"2022-07-07T07:31:53.299102Z","iopub.status.idle":"2022-07-07T07:31:53.316396Z","shell.execute_reply.started":"2022-07-07T07:31:53.299066Z","shell.execute_reply":"2022-07-07T07:31:53.315124Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Modelling","metadata":{"id":"WMBjylVnxkZP"}},{"cell_type":"code","source":"log = LogisticRegression()\nsvc = SVC()\nrand_forest = RandomForestClassifier(n_jobs=-1)\nboost = GradientBoostingClassifier()\nknn = KNeighborsClassifier()\nvote = VotingClassifier([('log', log), ('svc', svc), ('forest', rand_forest),\n                         ('gboost', boost), ('knn', knn)],\n                        n_jobs=-1, voting='hard')\nvote.fit(X_train, y_train)","metadata":{"id":"oEfQrLTERHZM","outputId":"16c392fa-dbb5-4c46-c891-d3bb725474d7","execution":{"iopub.status.busy":"2022-07-07T07:31:53.31844Z","iopub.execute_input":"2022-07-07T07:31:53.319203Z","iopub.status.idle":"2022-07-07T07:31:58.018235Z","shell.execute_reply.started":"2022-07-07T07:31:53.319162Z","shell.execute_reply":"2022-07-07T07:31:58.017164Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(classification_report(y_val, vote.predict(X_val)))","metadata":{"id":"mYVgISMuevmk","outputId":"ad15bdfb-3dd2-463e-9869-fe12942682ce","execution":{"iopub.status.busy":"2022-07-07T07:31:58.020095Z","iopub.execute_input":"2022-07-07T07:31:58.02069Z","iopub.status.idle":"2022-07-07T07:31:58.951591Z","shell.execute_reply.started":"2022-07-07T07:31:58.020647Z","shell.execute_reply":"2022-07-07T07:31:58.949535Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"params = {'log__C':[1e-1, 1, 10, 50],\n          'svc__C':[1e-1, 1, 10, 50],\n          'svc__kernel':['poly','rbf','sigmoid'],\n          'knn__n_neighbors':[3,5,7,10],\n          'forest__n_estimators':[50,100,150]}\n\nrand_search = RandomizedSearchCV(vote, params, n_iter=20, scoring='accuracy',\n                                 cv=3, verbose=10)\nrand_search.fit(X_train, y_train)","metadata":{"id":"iEqYXIf0gHZZ","outputId":"288741b6-5758-4450-e3f2-d8bc0bc3b11a","execution":{"iopub.status.busy":"2022-07-07T07:31:58.953259Z","iopub.execute_input":"2022-07-07T07:31:58.953579Z","iopub.status.idle":"2022-07-07T07:34:51.402652Z","shell.execute_reply.started":"2022-07-07T07:31:58.953537Z","shell.execute_reply":"2022-07-07T07:34:51.401184Z"},"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rand_search.best_params_, rand_search.cv_results_['mean_test_score'].max()","metadata":{"id":"cgp_dyDbie1_","outputId":"f749228c-571a-4583-c7e6-def87fce6317","execution":{"iopub.status.busy":"2022-07-07T07:34:51.404775Z","iopub.execute_input":"2022-07-07T07:34:51.405357Z","iopub.status.idle":"2022-07-07T07:34:51.413425Z","shell.execute_reply.started":"2022-07-07T07:34:51.405297Z","shell.execute_reply":"2022-07-07T07:34:51.412158Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.DataFrame(rand_search.cv_results_).sort_values('rank_test_score').head()","metadata":{"id":"Nxke9_y7i0wo","outputId":"2628d516-043c-4442-b528-7adf6d06df7f","execution":{"iopub.status.busy":"2022-07-07T07:34:51.415391Z","iopub.execute_input":"2022-07-07T07:34:51.415966Z","iopub.status.idle":"2022-07-07T07:34:51.44683Z","shell.execute_reply.started":"2022-07-07T07:34:51.415927Z","shell.execute_reply":"2022-07-07T07:34:51.445612Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = rand_search.best_estimator_\nmodel.fit(X_train, y_train)","metadata":{"id":"Jqq-46tHjCMP","outputId":"a83162a2-6aef-46b6-f392-d888b42ca45c","execution":{"iopub.status.busy":"2022-07-07T07:34:51.449143Z","iopub.execute_input":"2022-07-07T07:34:51.449733Z","iopub.status.idle":"2022-07-07T07:34:55.172461Z","shell.execute_reply.started":"2022-07-07T07:34:51.449666Z","shell.execute_reply":"2022-07-07T07:34:55.171365Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(classification_report(y_val, model.predict(X_val)))","metadata":{"id":"Bjw7L_5yjHpi","outputId":"b5120ef3-7d9a-4b2c-b272-e2a6f9721f62","execution":{"iopub.status.busy":"2022-07-07T07:34:55.174042Z","iopub.execute_input":"2022-07-07T07:34:55.174448Z","iopub.status.idle":"2022-07-07T07:34:55.890988Z","shell.execute_reply.started":"2022-07-07T07:34:55.174407Z","shell.execute_reply":"2022-07-07T07:34:55.888914Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = (model.predict(test_set) >= 0.5)\ny_pred","metadata":{"outputId":"e39e7dfb-b238-459d-8304-e6179626cd85","id":"XESAlJspfPHS","execution":{"iopub.status.busy":"2022-07-07T07:34:55.892614Z","iopub.execute_input":"2022-07-07T07:34:55.893694Z","iopub.status.idle":"2022-07-07T07:34:57.295232Z","shell.execute_reply.started":"2022-07-07T07:34:55.893657Z","shell.execute_reply":"2022-07-07T07:34:57.294323Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"passenger_id_df['Transported'] = y_pred","metadata":{"id":"x-PGyllQfPHT","execution":{"iopub.status.busy":"2022-07-07T07:35:31.20988Z","iopub.execute_input":"2022-07-07T07:35:31.210236Z","iopub.status.idle":"2022-07-07T07:35:31.215696Z","shell.execute_reply.started":"2022-07-07T07:35:31.210209Z","shell.execute_reply":"2022-07-07T07:35:31.214719Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"passenger_id_df","metadata":{"outputId":"1fb87bdb-f167-44d6-da8c-c825005e46ba","id":"jUPbeu1FfPHU","execution":{"iopub.status.busy":"2022-07-07T07:35:33.047951Z","iopub.execute_input":"2022-07-07T07:35:33.048299Z","iopub.status.idle":"2022-07-07T07:35:33.062614Z","shell.execute_reply.started":"2022-07-07T07:35:33.048271Z","shell.execute_reply":"2022-07-07T07:35:33.061281Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"passenger_id_df.to_csv('submission.csv', index=False)","metadata":{"id":"wBImt1bXfPHU","execution":{"iopub.status.busy":"2022-07-07T07:35:33.320441Z","iopub.execute_input":"2022-07-07T07:35:33.321026Z","iopub.status.idle":"2022-07-07T07:35:33.341663Z","shell.execute_reply.started":"2022-07-07T07:35:33.320981Z","shell.execute_reply":"2022-07-07T07:35:33.340288Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"id":"paxwbrcCfS5o"},"execution_count":null,"outputs":[]}]}