{"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":"## Import libraries and data","metadata":{}},{"cell_type":"code","source":"import os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-07-20T08:52:44.598877Z","iopub.execute_input":"2022-07-20T08:52:44.599583Z","iopub.status.idle":"2022-07-20T08:52:44.621539Z","shell.execute_reply.started":"2022-07-20T08:52:44.599171Z","shell.execute_reply":"2022-07-20T08:52:44.620564Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:52:44.638633Z","iopub.execute_input":"2022-07-20T08:52:44.639041Z","iopub.status.idle":"2022-07-20T08:52:45.433064Z","shell.execute_reply.started":"2022-07-20T08:52:44.639014Z","shell.execute_reply":"2022-07-20T08:52:45.432101Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"url = \"../input/spaceship-titanic/\"\ndf = pd.read_csv(url+\"train.csv\")\ntest = pd.read_csv(url+\"test.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:52:45.435312Z","iopub.execute_input":"2022-07-20T08:52:45.436044Z","iopub.status.idle":"2022-07-20T08:52:45.519188Z","shell.execute_reply.started":"2022-07-20T08:52:45.436005Z","shell.execute_reply":"2022-07-20T08:52:45.518099Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Explore data","metadata":{}},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:52:45.520661Z","iopub.execute_input":"2022-07-20T08:52:45.521009Z","iopub.status.idle":"2022-07-20T08:52:45.550480Z","shell.execute_reply.started":"2022-07-20T08:52:45.520976Z","shell.execute_reply":"2022-07-20T08:52:45.549431Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:52:45.553237Z","iopub.execute_input":"2022-07-20T08:52:45.553829Z","iopub.status.idle":"2022-07-20T08:52:45.582963Z","shell.execute_reply.started":"2022-07-20T08:52:45.553788Z","shell.execute_reply":"2022-07-20T08:52:45.581737Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:52:45.584619Z","iopub.execute_input":"2022-07-20T08:52:45.585292Z","iopub.status.idle":"2022-07-20T08:52:45.601017Z","shell.execute_reply.started":"2022-07-20T08:52:45.585256Z","shell.execute_reply":"2022-07-20T08:52:45.600090Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.drop(\"Name\",axis=1,inplace=True)\ntest.drop(\"Name\",axis=1,inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:52:45.602319Z","iopub.execute_input":"2022-07-20T08:52:45.603178Z","iopub.status.idle":"2022-07-20T08:52:45.612404Z","shell.execute_reply.started":"2022-07-20T08:52:45.603143Z","shell.execute_reply":"2022-07-20T08:52:45.611274Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.HomePlanet.value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:52:45.613735Z","iopub.execute_input":"2022-07-20T08:52:45.615280Z","iopub.status.idle":"2022-07-20T08:52:45.625241Z","shell.execute_reply.started":"2022-07-20T08:52:45.615244Z","shell.execute_reply":"2022-07-20T08:52:45.623990Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def count_plot(data):\n    cat_cols = data.select_dtypes(include=\"object\").columns\n    nrows = int(np.ceil(len(cat_cols)/2))\n    fig,axes = plt.subplots(nrows=nrows,ncols=2,figsize=(20,15))\n    \n    if len(cat_cols)%2==1:\n        axes[nrows-1][1].axis(\"off\")\n    \n    i=0\n    for j in range(nrows):\n        for k in range(2):\n            if i==len(cat_cols):\n                break\n            sns.countplot(data=data,x=cat_cols[i],ax=axes[j][k])\n            i+=1\n        if i==len(cat_cols):\n            break","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:52:45.626844Z","iopub.execute_input":"2022-07-20T08:52:45.627204Z","iopub.status.idle":"2022-07-20T08:52:45.634717Z","shell.execute_reply.started":"2022-07-20T08:52:45.627170Z","shell.execute_reply":"2022-07-20T08:52:45.633740Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"count_plot(df.drop([\"PassengerId\",\"Cabin\"],axis=1))","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:52:45.636013Z","iopub.execute_input":"2022-07-20T08:52:45.636717Z","iopub.status.idle":"2022-07-20T08:52:46.129873Z","shell.execute_reply.started":"2022-07-20T08:52:45.636682Z","shell.execute_reply":"2022-07-20T08:52:46.128766Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def hist_plot(data,kde=False,include_int=False,hue=None,bins=10):\n    if include_int:\n        cols = list(data.select_dtypes(include=[\"float\",\"int\"]).columns)\n    else:\n        cols = list(data.select_dtypes(include=\"float\").columns)\n    \n    nrows = int(np.ceil(len(cols)/2))\n    fig,axes = plt.subplots(nrows=nrows,ncols=2,figsize=(20,15))\n    \n    if len(cols)%2==1:\n        axes[nrows-1][1].axis(\"off\")\n    \n    i=0\n    for j in range(nrows):\n        for k in range(2):\n            if i==len(cols):\n                break\n            sns.histplot(data=data,x=cols[i],ax=axes[j][k],kde=kde,hue=hue,bins=bins)\n            i+=1\n        if i==len(cols):\n            break","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:52:46.136012Z","iopub.execute_input":"2022-07-20T08:52:46.136334Z","iopub.status.idle":"2022-07-20T08:52:46.146750Z","shell.execute_reply.started":"2022-07-20T08:52:46.136308Z","shell.execute_reply":"2022-07-20T08:52:46.145592Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"hist_plot(df,kde=True,hue=\"HomePlanet\")","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:52:46.148014Z","iopub.execute_input":"2022-07-20T08:52:46.148815Z","iopub.status.idle":"2022-07-20T08:52:47.860996Z","shell.execute_reply.started":"2022-07-20T08:52:46.148777Z","shell.execute_reply":"2022-07-20T08:52:47.860008Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"hist_plot(df,kde=True,hue=\"Transported\",bins=3)","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:52:47.862064Z","iopub.execute_input":"2022-07-20T08:52:47.862412Z","iopub.status.idle":"2022-07-20T08:52:49.264159Z","shell.execute_reply.started":"2022-07-20T08:52:47.862378Z","shell.execute_reply":"2022-07-20T08:52:49.263208Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Seems like people below 20 have a higher probability of being transported","metadata":{}},{"cell_type":"code","source":"def scatter_plot(data,x_axis,hue=None):\n    \n    cols = list(data.select_dtypes(include=[\"float\",\"int\"]).columns)\n    cols.remove(x_axis)\n    \n    nrows = int(np.ceil(len(cols)/2))\n    fig,axes = plt.subplots(nrows=nrows,ncols=2,figsize=(20,15))\n    \n    if len(cols)%2==1:\n        axes[nrows-1][1].axis(\"off\")\n    \n    i=0\n    for j in range(nrows):\n        for k in range(2):\n            if i==len(cols):\n                break\n            sns.scatterplot(data=data,x=x_axis,y=cols[i],ax=axes[j][k],hue=hue)\n            i+=1\n        if i==len(cols):\n            break","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:52:49.265468Z","iopub.execute_input":"2022-07-20T08:52:49.266237Z","iopub.status.idle":"2022-07-20T08:52:49.276094Z","shell.execute_reply.started":"2022-07-20T08:52:49.266199Z","shell.execute_reply":"2022-07-20T08:52:49.275168Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scatter_plot(df,x_axis=\"Spa\",hue=\"Transported\")","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:52:49.277406Z","iopub.execute_input":"2022-07-20T08:52:49.280036Z","iopub.status.idle":"2022-07-20T08:52:52.251592Z","shell.execute_reply.started":"2022-07-20T08:52:49.279997Z","shell.execute_reply":"2022-07-20T08:52:52.248774Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scatter_plot(df,x_axis=\"FoodCourt\",hue=\"Transported\")","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:52:52.253466Z","iopub.execute_input":"2022-07-20T08:52:52.254541Z","iopub.status.idle":"2022-07-20T08:52:55.324855Z","shell.execute_reply.started":"2022-07-20T08:52:52.254490Z","shell.execute_reply":"2022-07-20T08:52:55.323899Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scatter_plot(df,\"VRDeck\",hue=\"Transported\")","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:52:55.326516Z","iopub.execute_input":"2022-07-20T08:52:55.327197Z","iopub.status.idle":"2022-07-20T08:52:57.271002Z","shell.execute_reply.started":"2022-07-20T08:52:55.327158Z","shell.execute_reply":"2022-07-20T08:52:57.269927Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scatter_plot(df,\"RoomService\",hue=\"Transported\")","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:52:57.272788Z","iopub.execute_input":"2022-07-20T08:52:57.273172Z","iopub.status.idle":"2022-07-20T08:52:59.307975Z","shell.execute_reply.started":"2022-07-20T08:52:57.273139Z","shell.execute_reply":"2022-07-20T08:52:59.306920Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Forming the group attribute","metadata":{}},{"cell_type":"code","source":"df[\"group\"] = np.zeros(shape=(len(df),1))\ntest[\"group\"] = np.zeros(shape=(len(test),1))","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:52:59.309789Z","iopub.execute_input":"2022-07-20T08:52:59.310163Z","iopub.status.idle":"2022-07-20T08:52:59.318238Z","shell.execute_reply.started":"2022-07-20T08:52:59.310130Z","shell.execute_reply":"2022-07-20T08:52:59.317037Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.columns,df.shape,test.columns,test.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:52:59.319977Z","iopub.execute_input":"2022-07-20T08:52:59.320868Z","iopub.status.idle":"2022-07-20T08:52:59.328439Z","shell.execute_reply.started":"2022-07-20T08:52:59.320833Z","shell.execute_reply":"2022-07-20T08:52:59.327452Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(len(df)):\n    df.iat[i,13] = int(df.iloc[i].PassengerId.split(\"_\")[0])\n\nfor i in range(len(test)):\n    test.iat[i,12] = int(test.iloc[i].PassengerId.split(\"_\")[0])\n\ndf.head(5)","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:52:59.330354Z","iopub.execute_input":"2022-07-20T08:52:59.331186Z","iopub.status.idle":"2022-07-20T08:53:02.651030Z","shell.execute_reply.started":"2022-07-20T08:52:59.331148Z","shell.execute_reply":"2022-07-20T08:53:02.649990Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head(5)","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:53:02.652443Z","iopub.execute_input":"2022-07-20T08:53:02.653145Z","iopub.status.idle":"2022-07-20T08:53:02.675940Z","shell.execute_reply.started":"2022-07-20T08:53:02.653107Z","shell.execute_reply":"2022-07-20T08:53:02.675092Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Data Imputation","metadata":{}},{"cell_type":"code","source":"df.set_index(\"PassengerId\",inplace=True)\ntest.set_index(\"PassengerId\",inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:53:02.677521Z","iopub.execute_input":"2022-07-20T08:53:02.677905Z","iopub.status.idle":"2022-07-20T08:53:02.684582Z","shell.execute_reply.started":"2022-07-20T08:53:02.677871Z","shell.execute_reply":"2022-07-20T08:53:02.683387Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = df.drop(\"Transported\",axis=1)\ntrain = pd.concat([train,test],axis=0)\ntrain.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:53:02.686631Z","iopub.execute_input":"2022-07-20T08:53:02.687176Z","iopub.status.idle":"2022-07-20T08:53:02.702666Z","shell.execute_reply.started":"2022-07-20T08:53:02.687140Z","shell.execute_reply":"2022-07-20T08:53:02.701316Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"age_null_grp = set(train[train[\"Age\"].isnull()].group.value_counts().index)","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:53:02.704419Z","iopub.execute_input":"2022-07-20T08:53:02.704785Z","iopub.status.idle":"2022-07-20T08:53:02.714218Z","shell.execute_reply.started":"2022-07-20T08:53:02.704748Z","shell.execute_reply":"2022-07-20T08:53:02.713099Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"grp_with_more_than_one = set(train.group.value_counts()[train.group.value_counts()>1].index)","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:53:02.715638Z","iopub.execute_input":"2022-07-20T08:53:02.716127Z","iopub.status.idle":"2022-07-20T08:53:02.727710Z","shell.execute_reply.started":"2022-07-20T08:53:02.716088Z","shell.execute_reply":"2022-07-20T08:53:02.726663Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"common_grps = list(age_null_grp.intersection(grp_with_more_than_one))","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:53:02.729004Z","iopub.execute_input":"2022-07-20T08:53:02.729504Z","iopub.status.idle":"2022-07-20T08:53:02.734910Z","shell.execute_reply.started":"2022-07-20T08:53:02.729467Z","shell.execute_reply":"2022-07-20T08:53:02.733769Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_temp = train[train[\"group\"]==common_grps[0]]","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:53:02.736793Z","iopub.execute_input":"2022-07-20T08:53:02.737587Z","iopub.status.idle":"2022-07-20T08:53:02.743454Z","shell.execute_reply.started":"2022-07-20T08:53:02.737550Z","shell.execute_reply":"2022-07-20T08:53:02.742363Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_temp[train_temp.Age.isnull()].index","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:53:02.753009Z","iopub.execute_input":"2022-07-20T08:53:02.753915Z","iopub.status.idle":"2022-07-20T08:53:02.763824Z","shell.execute_reply.started":"2022-07-20T08:53:02.753871Z","shell.execute_reply":"2022-07-20T08:53:02.762430Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ind_age = {}\nfor i in common_grps:\n    train_temp = train[train[\"group\"]==i]\n    temp_mean = train_temp.Age.mean()\n    age_null_ind = train_temp[train_temp.Age.isnull()].index[0]\n    if not pd.isna(temp_mean):\n        ind_age[age_null_ind] = int(np.round(temp_mean,0))\n    else:\n        ind_age[age_null_ind] = int(np.round(train.Age.mean(),0))\nind_age","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:53:02.765303Z","iopub.execute_input":"2022-07-20T08:53:02.765844Z","iopub.status.idle":"2022-07-20T08:53:02.882762Z","shell.execute_reply.started":"2022-07-20T08:53:02.765808Z","shell.execute_reply":"2022-07-20T08:53:02.881821Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.columns","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:53:02.885899Z","iopub.execute_input":"2022-07-20T08:53:02.886199Z","iopub.status.idle":"2022-07-20T08:53:02.893776Z","shell.execute_reply.started":"2022-07-20T08:53:02.886174Z","shell.execute_reply":"2022-07-20T08:53:02.892595Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" for i in ind_age.keys():\n        train.at[i,\"Age\"] = ind_age[i]\ntrain.Age.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:53:02.896587Z","iopub.execute_input":"2022-07-20T08:53:02.897394Z","iopub.status.idle":"2022-07-20T08:53:02.910709Z","shell.execute_reply.started":"2022-07-20T08:53:02.897353Z","shell.execute_reply":"2022-07-20T08:53:02.909687Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"planet_null_grps = set(train[train[\"HomePlanet\"].isnull()].group.value_counts().index)","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:53:02.912103Z","iopub.execute_input":"2022-07-20T08:53:02.913004Z","iopub.status.idle":"2022-07-20T08:53:02.922819Z","shell.execute_reply.started":"2022-07-20T08:53:02.912966Z","shell.execute_reply":"2022-07-20T08:53:02.921781Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"common_grps = list(planet_null_grps.intersection(grp_with_more_than_one))","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:53:02.926399Z","iopub.execute_input":"2022-07-20T08:53:02.928224Z","iopub.status.idle":"2022-07-20T08:53:02.932264Z","shell.execute_reply.started":"2022-07-20T08:53:02.928198Z","shell.execute_reply":"2022-07-20T08:53:02.931093Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ind_planet = {}\nfor i in common_grps:\n    train_temp = train[train[\"group\"]==i]\n    if len(train_temp.HomePlanet.mode().values)>0:\n        temp_planet = train_temp.HomePlanet.mode().values[0]\n        planet_null_ind = train_temp[train_temp.HomePlanet.isnull()].index[0]\n        ind_planet[planet_null_ind] = temp_planet\nind_planet","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:53:02.933922Z","iopub.execute_input":"2022-07-20T08:53:02.935306Z","iopub.status.idle":"2022-07-20T08:53:03.083733Z","shell.execute_reply.started":"2022-07-20T08:53:02.935233Z","shell.execute_reply":"2022-07-20T08:53:03.082760Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" for i in ind_planet.keys():\n        train.at[i,\"HomePlanet\"] = ind_planet[i]\ntrain.HomePlanet.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:53:03.085243Z","iopub.execute_input":"2022-07-20T08:53:03.085828Z","iopub.status.idle":"2022-07-20T08:53:03.096756Z","shell.execute_reply.started":"2022-07-20T08:53:03.085790Z","shell.execute_reply":"2022-07-20T08:53:03.095670Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:53:03.098021Z","iopub.execute_input":"2022-07-20T08:53:03.099161Z","iopub.status.idle":"2022-07-20T08:53:03.115741Z","shell.execute_reply.started":"2022-07-20T08:53:03.099125Z","shell.execute_reply":"2022-07-20T08:53:03.114806Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mean_planet_age = train.groupby(\"HomePlanet\").mean()[\"Age\"].to_dict()","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:53:03.117216Z","iopub.execute_input":"2022-07-20T08:53:03.117805Z","iopub.status.idle":"2022-07-20T08:53:03.128275Z","shell.execute_reply.started":"2022-07-20T08:53:03.117770Z","shell.execute_reply":"2022-07-20T08:53:03.127176Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def fill_age(x,y):\n    if pd.isna(x):\n        if pd.isna(y):\n            return int(train.Age.mean())\n        else:\n            return int(mean_planet_age[y])\n    return x","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:53:03.129758Z","iopub.execute_input":"2022-07-20T08:53:03.130975Z","iopub.status.idle":"2022-07-20T08:53:03.137208Z","shell.execute_reply.started":"2022-07-20T08:53:03.130935Z","shell.execute_reply":"2022-07-20T08:53:03.136150Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train[\"Age\"] = train.apply(lambda a: fill_age(a[\"Age\"],a[\"HomePlanet\"]),axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:53:03.138746Z","iopub.execute_input":"2022-07-20T08:53:03.139355Z","iopub.status.idle":"2022-07-20T08:53:03.367243Z","shell.execute_reply.started":"2022-07-20T08:53:03.139263Z","shell.execute_reply":"2022-07-20T08:53:03.366248Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:53:03.368479Z","iopub.execute_input":"2022-07-20T08:53:03.368830Z","iopub.status.idle":"2022-07-20T08:53:03.387310Z","shell.execute_reply.started":"2022-07-20T08:53:03.368796Z","shell.execute_reply":"2022-07-20T08:53:03.386308Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cryosleep_null = set(train[train[\"CryoSleep\"].isnull()].group.value_counts().index)","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:53:03.388743Z","iopub.execute_input":"2022-07-20T08:53:03.389784Z","iopub.status.idle":"2022-07-20T08:53:03.398481Z","shell.execute_reply.started":"2022-07-20T08:53:03.389747Z","shell.execute_reply":"2022-07-20T08:53:03.397389Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train[train[\"CryoSleep\"].isnull()].group","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:53:03.399927Z","iopub.execute_input":"2022-07-20T08:53:03.400997Z","iopub.status.idle":"2022-07-20T08:53:03.412977Z","shell.execute_reply.started":"2022-07-20T08:53:03.400961Z","shell.execute_reply":"2022-07-20T08:53:03.411681Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"common_grps = list(cryosleep_null.intersection(grp_with_more_than_one))","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:53:03.414917Z","iopub.execute_input":"2022-07-20T08:53:03.415692Z","iopub.status.idle":"2022-07-20T08:53:03.421336Z","shell.execute_reply.started":"2022-07-20T08:53:03.415650Z","shell.execute_reply":"2022-07-20T08:53:03.420240Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ind_cs = {}\nfor i in common_grps:\n    train_temp = train[train[\"group\"]==i]\n    if len(train_temp.CryoSleep.mode().values)>0:\n        temp_cs = train_temp.CryoSleep.mode().values[0]\n        cs_null_ind = train_temp[train_temp.CryoSleep.isnull()].index[0]\n        ind_cs[cs_null_ind] = temp_cs\nlen(ind_cs)","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:53:03.423239Z","iopub.execute_input":"2022-07-20T08:53:03.424017Z","iopub.status.idle":"2022-07-20T08:53:03.625217Z","shell.execute_reply.started":"2022-07-20T08:53:03.423953Z","shell.execute_reply":"2022-07-20T08:53:03.624123Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.columns","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:53:03.626667Z","iopub.execute_input":"2022-07-20T08:53:03.627504Z","iopub.status.idle":"2022-07-20T08:53:03.635122Z","shell.execute_reply.started":"2022-07-20T08:53:03.627470Z","shell.execute_reply":"2022-07-20T08:53:03.633580Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" for i in ind_cs.keys():\n        train.at[i,\"CryoSleep\"] = ind_cs[i]\ntrain.CryoSleep.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:53:03.637254Z","iopub.execute_input":"2022-07-20T08:53:03.638027Z","iopub.status.idle":"2022-07-20T08:53:03.651252Z","shell.execute_reply.started":"2022-07-20T08:53:03.637977Z","shell.execute_reply":"2022-07-20T08:53:03.650279Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:53:03.653177Z","iopub.execute_input":"2022-07-20T08:53:03.653935Z","iopub.status.idle":"2022-07-20T08:53:03.672941Z","shell.execute_reply.started":"2022-07-20T08:53:03.653896Z","shell.execute_reply":"2022-07-20T08:53:03.672085Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cabin_null = list(train[train[\"Cabin\"].isnull()].group.value_counts().index)","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:53:03.675030Z","iopub.execute_input":"2022-07-20T08:53:03.675382Z","iopub.status.idle":"2022-07-20T08:53:03.685574Z","shell.execute_reply.started":"2022-07-20T08:53:03.675349Z","shell.execute_reply":"2022-07-20T08:53:03.684623Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def fill_nas(column):\n    print(\"Before filling: \",train[column].isnull().sum())\n    col_null = set(train[train[column].isnull()].group.value_counts().index)\n    common_grps = list(col_null.intersection(grp_with_more_than_one))\n    ind_nulls = {}\n    for i in common_grps:\n        train_temp = train[train[\"group\"]==i]\n        if len(train_temp[column].mode().values)>0:\n            temp_val = train_temp[column].mode().values[0]\n            temp_null_ind = train_temp[train_temp[column].isnull()].index[0]\n            ind_nulls[temp_null_ind] = temp_val\n    for i in ind_nulls.keys():\n        train.at[i,column] = ind_nulls[i]\n    print(\"After filling: \",train[column].isnull().sum())","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:53:03.688651Z","iopub.execute_input":"2022-07-20T08:53:03.688922Z","iopub.status.idle":"2022-07-20T08:53:03.697790Z","shell.execute_reply.started":"2022-07-20T08:53:03.688899Z","shell.execute_reply":"2022-07-20T08:53:03.696507Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:53:03.699480Z","iopub.execute_input":"2022-07-20T08:53:03.700018Z","iopub.status.idle":"2022-07-20T08:53:03.719678Z","shell.execute_reply.started":"2022-07-20T08:53:03.699983Z","shell.execute_reply":"2022-07-20T08:53:03.718529Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#VIP, Cabin, Destination\nfill_nas(\"VIP\")\nfill_nas(\"Cabin\")\nfill_nas(\"Destination\")","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:53:03.721284Z","iopub.execute_input":"2022-07-20T08:53:03.721657Z","iopub.status.idle":"2022-07-20T08:53:04.126902Z","shell.execute_reply.started":"2022-07-20T08:53:03.721623Z","shell.execute_reply":"2022-07-20T08:53:04.126015Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train[\"RoomService\"] = train[\"RoomService\"].fillna(0)\ntrain[\"VRDeck\"] = train[\"VRDeck\"].fillna(0)\ntrain[\"Spa\"] = train[\"Spa\"].fillna(0)\ntrain[\"ShoppingMall\"] = train[\"ShoppingMall\"].fillna(0)","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:53:04.128444Z","iopub.execute_input":"2022-07-20T08:53:04.128805Z","iopub.status.idle":"2022-07-20T08:53:04.137604Z","shell.execute_reply.started":"2022-07-20T08:53:04.128770Z","shell.execute_reply":"2022-07-20T08:53:04.136515Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def fill_foodcourt(x,y):\n    if pd.isna(x):\n        if y in [\"Earth\",\"Mars\"]:\n            return 0.0\n        else:\n            return 11.0\n    return x","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:53:04.139155Z","iopub.execute_input":"2022-07-20T08:53:04.139661Z","iopub.status.idle":"2022-07-20T08:53:04.144917Z","shell.execute_reply.started":"2022-07-20T08:53:04.139627Z","shell.execute_reply":"2022-07-20T08:53:04.143834Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train[\"FoodCourt\"] = train.apply(lambda a: fill_foodcourt(a[\"FoodCourt\"],a[\"HomePlanet\"]),axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:53:04.146436Z","iopub.execute_input":"2022-07-20T08:53:04.146843Z","iopub.status.idle":"2022-07-20T08:53:04.378502Z","shell.execute_reply.started":"2022-07-20T08:53:04.146806Z","shell.execute_reply":"2022-07-20T08:53:04.377521Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.groupby(\"HomePlanet\").describe().transpose().loc[\"RoomService\":\"VRDeck\"]","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:53:04.379884Z","iopub.execute_input":"2022-07-20T08:53:04.380255Z","iopub.status.idle":"2022-07-20T08:53:04.458783Z","shell.execute_reply.started":"2022-07-20T08:53:04.380218Z","shell.execute_reply":"2022-07-20T08:53:04.457751Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def fill_planet(rs,fc,planet):\n    if pd.isna(planet):\n        if rs <22 and fc<5:\n            return \"Earth\"\n        elif rs < 22 and fc>5:\n            return \"Europa\"\n        else:\n            return \"Mars\"\n    return planet","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:53:04.460585Z","iopub.execute_input":"2022-07-20T08:53:04.461100Z","iopub.status.idle":"2022-07-20T08:53:04.468303Z","shell.execute_reply.started":"2022-07-20T08:53:04.461042Z","shell.execute_reply":"2022-07-20T08:53:04.466935Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train[\"HomePlanet\"] = train.apply(lambda a:fill_planet(a[\"RoomService\"],a[\"FoodCourt\"],a[\"HomePlanet\"]),axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:53:04.470945Z","iopub.execute_input":"2022-07-20T08:53:04.472122Z","iopub.status.idle":"2022-07-20T08:53:04.766943Z","shell.execute_reply.started":"2022-07-20T08:53:04.472084Z","shell.execute_reply":"2022-07-20T08:53:04.765986Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:53:04.768369Z","iopub.execute_input":"2022-07-20T08:53:04.768830Z","iopub.status.idle":"2022-07-20T08:53:04.787191Z","shell.execute_reply.started":"2022-07-20T08:53:04.768791Z","shell.execute_reply":"2022-07-20T08:53:04.786230Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def start_dest(x,y):\n    if pd.isna(y):\n        return np.nan\n    else:\n        return x+\"_\"+y\ntrain[\"Start_Destination\"] = train.apply(lambda a:start_dest(a[\"HomePlanet\"],a[\"Destination\"]),axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:53:04.788484Z","iopub.execute_input":"2022-07-20T08:53:04.788916Z","iopub.status.idle":"2022-07-20T08:53:05.018595Z","shell.execute_reply.started":"2022-07-20T08:53:04.788880Z","shell.execute_reply":"2022-07-20T08:53:05.017356Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train[\"Start_Destination\"].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:53:05.020161Z","iopub.execute_input":"2022-07-20T08:53:05.020516Z","iopub.status.idle":"2022-07-20T08:53:05.033086Z","shell.execute_reply.started":"2022-07-20T08:53:05.020480Z","shell.execute_reply":"2022-07-20T08:53:05.031783Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.groupby(\"Start_Destination\").describe().T","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:53:05.034932Z","iopub.execute_input":"2022-07-20T08:53:05.035307Z","iopub.status.idle":"2022-07-20T08:53:05.210030Z","shell.execute_reply.started":"2022-07-20T08:53:05.035273Z","shell.execute_reply":"2022-07-20T08:53:05.209073Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.groupby(\"Destination\").describe().T","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:53:05.211739Z","iopub.execute_input":"2022-07-20T08:53:05.212477Z","iopub.status.idle":"2022-07-20T08:53:05.289672Z","shell.execute_reply.started":"2022-07-20T08:53:05.212433Z","shell.execute_reply":"2022-07-20T08:53:05.288561Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"From the above dataframe *VRDeck's* mean seems to be a good feature based on which we can impute the *Destination* feature. The ```std``` of the values indicate that there is little overlap in the values","metadata":{}},{"cell_type":"code","source":"train.groupby(\"Destination\").describe().T.loc[(\"VRDeck\",[\"mean\",\"std\"]),:]","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:53:05.291399Z","iopub.execute_input":"2022-07-20T08:53:05.291761Z","iopub.status.idle":"2022-07-20T08:53:05.365024Z","shell.execute_reply.started":"2022-07-20T08:53:05.291723Z","shell.execute_reply":"2022-07-20T08:53:05.364106Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def fill_dest(x,y):\n    if pd.isna(x):\n        if y in range(200,300):\n            return \"TRAPPIST-1e\"\n        elif y in range(130,180):\n            return \"PSO J318.5-22\"\n        else:\n            return \"55 Cancri e\"\n    return x","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:53:05.366407Z","iopub.execute_input":"2022-07-20T08:53:05.367496Z","iopub.status.idle":"2022-07-20T08:53:05.374316Z","shell.execute_reply.started":"2022-07-20T08:53:05.367458Z","shell.execute_reply":"2022-07-20T08:53:05.373221Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train[\"Destination\"] = train.apply(lambda a:fill_dest(a[\"Destination\"],a[\"VRDeck\"]),axis=1)\ntrain.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:53:05.376163Z","iopub.execute_input":"2022-07-20T08:53:05.377762Z","iopub.status.idle":"2022-07-20T08:53:05.627999Z","shell.execute_reply.started":"2022-07-20T08:53:05.377713Z","shell.execute_reply":"2022-07-20T08:53:05.627095Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train[\"Start_Destination\"] = train.apply(lambda a:start_dest(a[\"HomePlanet\"],a[\"Destination\"]),axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:53:05.629588Z","iopub.execute_input":"2022-07-20T08:53:05.629963Z","iopub.status.idle":"2022-07-20T08:53:05.861675Z","shell.execute_reply.started":"2022-07-20T08:53:05.629927Z","shell.execute_reply":"2022-07-20T08:53:05.860657Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def deck(x):\n    if not pd.isna(x):\n        return x.split(\"/\")[0]\n    return np.nan\ndef num(x):\n    if not pd.isna(x):\n        return x.split(\"/\")[1]\n    return np.nan\ndef side(x):\n    if not pd.isna(x):\n        return x.split(\"/\")[2]\n    return np.nan\n\ntrain[\"Deck\"] = train[\"Cabin\"].apply(deck)\ntrain[\"Num\"] = train[\"Cabin\"].apply(num)\ntrain[\"Side\"] = train[\"Cabin\"].apply(side)","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:53:05.862983Z","iopub.execute_input":"2022-07-20T08:53:05.863795Z","iopub.status.idle":"2022-07-20T08:53:05.918772Z","shell.execute_reply.started":"2022-07-20T08:53:05.863756Z","shell.execute_reply":"2022-07-20T08:53:05.917851Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train[\"Deck\"].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:53:05.920246Z","iopub.execute_input":"2022-07-20T08:53:05.921004Z","iopub.status.idle":"2022-07-20T08:53:05.932351Z","shell.execute_reply.started":"2022-07-20T08:53:05.920964Z","shell.execute_reply":"2022-07-20T08:53:05.931161Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.drop(\"Cabin\",axis=1,inplace=True)\ntrain.drop(\"Num\",axis=1,inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:53:05.933803Z","iopub.execute_input":"2022-07-20T08:53:05.934536Z","iopub.status.idle":"2022-07-20T08:53:05.946335Z","shell.execute_reply.started":"2022-07-20T08:53:05.934499Z","shell.execute_reply":"2022-07-20T08:53:05.945335Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.groupby(\"Deck\").describe().T.loc[(slice(None),[\"mean\",\"std\",\"50%\"]),:]","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:53:05.948098Z","iopub.execute_input":"2022-07-20T08:53:05.948656Z","iopub.status.idle":"2022-07-20T08:53:06.091987Z","shell.execute_reply.started":"2022-07-20T08:53:05.948620Z","shell.execute_reply":"2022-07-20T08:53:06.091101Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Mean and std values for 'G' and 'F' decks are very low compared to the other decks. This can be used for cabin imputation.","metadata":{}},{"cell_type":"code","source":"train[\"TotalSum\"] = train.RoomService.fillna(0) + train.ShoppingMall.fillna(0) + train.Spa.fillna(0) + train.VRDeck.fillna(0) \\\n                    + train.FoodCourt.fillna(0)","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:53:06.093640Z","iopub.execute_input":"2022-07-20T08:53:06.094005Z","iopub.status.idle":"2022-07-20T08:53:06.103550Z","shell.execute_reply.started":"2022-07-20T08:53:06.093969Z","shell.execute_reply":"2022-07-20T08:53:06.102565Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Using models to impute data","metadata":{}},{"cell_type":"code","source":"train.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:53:06.104927Z","iopub.execute_input":"2022-07-20T08:53:06.107306Z","iopub.status.idle":"2022-07-20T08:53:06.126043Z","shell.execute_reply.started":"2022-07-20T08:53:06.107281Z","shell.execute_reply":"2022-07-20T08:53:06.125116Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"missing_cols = [\"CryoSleep\",\"VIP\",\"Deck\",\"Side\"]","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:53:06.127343Z","iopub.execute_input":"2022-07-20T08:53:06.128229Z","iopub.status.idle":"2022-07-20T08:53:06.134389Z","shell.execute_reply.started":"2022-07-20T08:53:06.128191Z","shell.execute_reply":"2022-07-20T08:53:06.133133Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Imputing *CryoSleep*","metadata":{}},{"cell_type":"code","source":"missing_cols[1:]","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:53:06.136383Z","iopub.execute_input":"2022-07-20T08:53:06.136990Z","iopub.status.idle":"2022-07-20T08:53:06.144366Z","shell.execute_reply.started":"2022-07-20T08:53:06.136945Z","shell.execute_reply":"2022-07-20T08:53:06.143263Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train = train.drop(missing_cols[1:],axis=1).copy()\ndf_train.shape,df_train.columns","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:53:06.145806Z","iopub.execute_input":"2022-07-20T08:53:06.146340Z","iopub.status.idle":"2022-07-20T08:53:06.157831Z","shell.execute_reply.started":"2022-07-20T08:53:06.146305Z","shell.execute_reply":"2022-07-20T08:53:06.156631Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler,LabelEncoder\n\ndf_train.drop([\"HomePlanet\",\"Destination\"],axis=1,inplace=True)\none_hot_cols = [\"Start_Destination\"]\nscale_cols = [\"RoomService\",\"FoodCourt\",\"ShoppingMall\",\"Spa\",\"VRDeck\",\"TotalSum\"]\nencode_cols = [\"Age\",\"group\"]\ndf_train[\"CryoSleep\"] = df_train[\"CryoSleep\"].map({True:1,False:0})\n# df_train[\"Transported\"] = df_train[\"Transported\"].map({True:1,False:0})\n\nfor col in one_hot_cols:\n    temp = pd.get_dummies(df_train[col],drop_first=True,prefix=col)\n    df_train.drop(col,axis=1,inplace=True)\n    df_train = pd.concat([df_train,temp],axis=1)\n\nfor col in scale_cols:\n    sc = StandardScaler()\n    x = sc.fit_transform(df_train[col].values.reshape(-1,1))\n    df_train.drop(col,axis=1,inplace=True)\n    df_train[col] = x\n\nfor col in encode_cols:\n    lab_enc = LabelEncoder()\n    x = lab_enc.fit_transform(df_train[col].values.reshape(-1,1))\n    df_train.drop(col,axis=1,inplace=True)\n    df_train[col] = x","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:53:06.159362Z","iopub.execute_input":"2022-07-20T08:53:06.159730Z","iopub.status.idle":"2022-07-20T08:53:06.244133Z","shell.execute_reply.started":"2022-07-20T08:53:06.159695Z","shell.execute_reply":"2022-07-20T08:53:06.243116Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cryosleep_test = df_train[df_train[\"CryoSleep\"].isna()].copy()\ncryosleep_test.drop(\"CryoSleep\",axis=1,inplace=True)\ncryosleep_test","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:53:06.245532Z","iopub.execute_input":"2022-07-20T08:53:06.246530Z","iopub.status.idle":"2022-07-20T08:53:06.272864Z","shell.execute_reply.started":"2022-07-20T08:53:06.246487Z","shell.execute_reply":"2022-07-20T08:53:06.271626Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cryosleep_train = df_train.dropna().copy()\ncryosleep_train.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:53:06.274705Z","iopub.execute_input":"2022-07-20T08:53:06.275400Z","iopub.status.idle":"2022-07-20T08:53:06.287619Z","shell.execute_reply.started":"2022-07-20T08:53:06.275361Z","shell.execute_reply":"2022-07-20T08:53:06.286712Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nfrom sklearn.ensemble import RandomForestClassifier\nfrom sklearn.metrics import roc_auc_score,precision_score,recall_score\ndef impute_using_model(train_data,test_data,train_cols,target_col):\n    X_train,X_test,y_train,y_test = train_test_split(train_data[train_cols],train_data[target_col],\n                                                    test_size=0.2,random_state=42,stratify=train_data[target_col])\n    rf = RandomForestClassifier(n_estimators=200,max_depth=10)\n    rf = rf.fit(X_train,y_train)\n    y_pred_proba = rf.predict_proba(X_test)\n    if y_test.nunique()==2:\n        print(f\"ROC AUC for {target_col} is {roc_auc_score(y_test,y_pred_proba[:,1])}\")\n    else:\n        y_pred = np.argmax(y_pred_proba,axis=1)\n        prec = precision_score(y_test,y_pred,average=\"macro\",zero_division=0)\n        rec = recall_score(y_test,y_pred,average=\"macro\")\n        print(f\"Precision for {target_col} is {prec}\")\n        print(f\"Recall for {target_col} is {rec}\")\n    rf = rf.fit(train_data[train_cols],train_data[target_col])\n    return rf.predict(test_data)","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:53:06.289033Z","iopub.execute_input":"2022-07-20T08:53:06.289969Z","iopub.status.idle":"2022-07-20T08:53:06.483228Z","shell.execute_reply.started":"2022-07-20T08:53:06.289928Z","shell.execute_reply":"2022-07-20T08:53:06.482265Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cs = impute_using_model(cryosleep_train,cryosleep_test,cryosleep_train.drop(\"CryoSleep\",axis=1).columns,\n                       \"CryoSleep\")","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:53:06.484798Z","iopub.execute_input":"2022-07-20T08:53:06.485193Z","iopub.status.idle":"2022-07-20T08:53:09.753787Z","shell.execute_reply.started":"2022-07-20T08:53:06.485156Z","shell.execute_reply":"2022-07-20T08:53:09.752854Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cryosleep_test[\"CryoSleep\"] = cs","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:53:09.755308Z","iopub.execute_input":"2022-07-20T08:53:09.755666Z","iopub.status.idle":"2022-07-20T08:53:09.763697Z","shell.execute_reply.started":"2022-07-20T08:53:09.755631Z","shell.execute_reply":"2022-07-20T08:53:09.762751Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cryosleep_train.shape[0] + cryosleep_test.shape[0] == df_train.shape[0]","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:53:09.777925Z","iopub.execute_input":"2022-07-20T08:53:09.778192Z","iopub.status.idle":"2022-07-20T08:53:09.786716Z","shell.execute_reply.started":"2022-07-20T08:53:09.778170Z","shell.execute_reply":"2022-07-20T08:53:09.785741Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.drop(\"CryoSleep\",axis=1,inplace=True)\ndf_train = df_train.join(pd.concat([cryosleep_train[\"CryoSleep\"],cryosleep_test[\"CryoSleep\"]],axis=0))","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:53:09.787884Z","iopub.execute_input":"2022-07-20T08:53:09.788643Z","iopub.status.idle":"2022-07-20T08:53:09.802373Z","shell.execute_reply.started":"2022-07-20T08:53:09.788608Z","shell.execute_reply":"2022-07-20T08:53:09.801389Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train = df_train.join(train[\"VIP\"])","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:53:09.803546Z","iopub.execute_input":"2022-07-20T08:53:09.804342Z","iopub.status.idle":"2022-07-20T08:53:09.813140Z","shell.execute_reply.started":"2022-07-20T08:53:09.804309Z","shell.execute_reply":"2022-07-20T08:53:09.812095Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(df_train.VIP.value_counts())\ndf_train[\"VIP\"] = df_train[\"VIP\"].map({True:1,False:0})","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:53:09.814478Z","iopub.execute_input":"2022-07-20T08:53:09.815000Z","iopub.status.idle":"2022-07-20T08:53:09.826925Z","shell.execute_reply.started":"2022-07-20T08:53:09.814945Z","shell.execute_reply":"2022-07-20T08:53:09.825838Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vip_train = df_train.dropna(subset=[\"VIP\"]).copy()\nvip_test = df_train[df_train.VIP.isna()].drop(\"VIP\",axis=1).copy()","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:53:09.829196Z","iopub.execute_input":"2022-07-20T08:53:09.829713Z","iopub.status.idle":"2022-07-20T08:53:09.843512Z","shell.execute_reply.started":"2022-07-20T08:53:09.829674Z","shell.execute_reply":"2022-07-20T08:53:09.842589Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.drop(\"VIP\",axis=1,inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:53:09.844665Z","iopub.execute_input":"2022-07-20T08:53:09.845582Z","iopub.status.idle":"2022-07-20T08:53:09.851523Z","shell.execute_reply.started":"2022-07-20T08:53:09.845549Z","shell.execute_reply":"2022-07-20T08:53:09.850504Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vip = impute_using_model(vip_train,vip_test,vip_train.drop(\"VIP\",axis=1).columns,\"VIP\")\nvip_test[\"VIP\"] = vip","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:53:09.852836Z","iopub.execute_input":"2022-07-20T08:53:09.853632Z","iopub.status.idle":"2022-07-20T08:53:13.873910Z","shell.execute_reply.started":"2022-07-20T08:53:09.853596Z","shell.execute_reply":"2022-07-20T08:53:13.872869Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train = df_train.join(pd.concat([vip_train.VIP,vip_test.VIP],axis=0))","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:53:13.875277Z","iopub.execute_input":"2022-07-20T08:53:13.875636Z","iopub.status.idle":"2022-07-20T08:53:13.886939Z","shell.execute_reply.started":"2022-07-20T08:53:13.875602Z","shell.execute_reply":"2022-07-20T08:53:13.885983Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"missing_cols.remove(\"CryoSleep\")\nmissing_cols.remove(\"VIP\")\nmissing_cols","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:53:13.888995Z","iopub.execute_input":"2022-07-20T08:53:13.889411Z","iopub.status.idle":"2022-07-20T08:53:13.897813Z","shell.execute_reply.started":"2022-07-20T08:53:13.889372Z","shell.execute_reply":"2022-07-20T08:53:13.896680Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train = df_train.join(train[\"Deck\"])","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:53:13.899871Z","iopub.execute_input":"2022-07-20T08:53:13.900413Z","iopub.status.idle":"2022-07-20T08:53:13.910866Z","shell.execute_reply.started":"2022-07-20T08:53:13.900380Z","shell.execute_reply":"2022-07-20T08:53:13.909795Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"deck_train = df_train.dropna(subset=[\"Deck\"]).copy()\ndeck_test = df_train[df_train.Deck.isnull()].copy()\ndeck_test.drop(\"Deck\",axis=1,inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:53:13.911869Z","iopub.execute_input":"2022-07-20T08:53:13.913238Z","iopub.status.idle":"2022-07-20T08:53:13.927105Z","shell.execute_reply.started":"2022-07-20T08:53:13.913201Z","shell.execute_reply":"2022-07-20T08:53:13.926227Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"deck_train.Deck.unique()","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:53:13.928458Z","iopub.execute_input":"2022-07-20T08:53:13.928803Z","iopub.status.idle":"2022-07-20T08:53:13.936762Z","shell.execute_reply.started":"2022-07-20T08:53:13.928770Z","shell.execute_reply":"2022-07-20T08:53:13.935652Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"deck_train[\"Deck\"] = deck_train[\"Deck\"].map({\"A\":0,\"B\":1,\"C\":2,\"D\":3,\"E\":4,\"F\":5,\"G\":6,\"T\":7})","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:53:13.938502Z","iopub.execute_input":"2022-07-20T08:53:13.939210Z","iopub.status.idle":"2022-07-20T08:53:13.947087Z","shell.execute_reply.started":"2022-07-20T08:53:13.939176Z","shell.execute_reply":"2022-07-20T08:53:13.945861Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"deck = impute_using_model(deck_train,deck_test,deck_train.drop(\"Deck\",axis=1).columns,\"Deck\")\ndeck_test[\"Deck\"] = deck","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:53:13.948829Z","iopub.execute_input":"2022-07-20T08:53:13.949213Z","iopub.status.idle":"2022-07-20T08:53:18.039655Z","shell.execute_reply.started":"2022-07-20T08:53:13.949178Z","shell.execute_reply":"2022-07-20T08:53:18.038681Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.drop(\"Deck\",axis=1,inplace=True)\ndf_train = df_train.join(pd.concat([deck_train[\"Deck\"],deck_test[\"Deck\"]]))","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:53:18.040949Z","iopub.execute_input":"2022-07-20T08:53:18.041435Z","iopub.status.idle":"2022-07-20T08:53:18.054932Z","shell.execute_reply.started":"2022-07-20T08:53:18.041399Z","shell.execute_reply":"2022-07-20T08:53:18.053745Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train[\"Side\"] = train[\"Side\"].values","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:53:18.056664Z","iopub.execute_input":"2022-07-20T08:53:18.057523Z","iopub.status.idle":"2022-07-20T08:53:18.064096Z","shell.execute_reply.started":"2022-07-20T08:53:18.057483Z","shell.execute_reply":"2022-07-20T08:53:18.063102Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(df_train.Side.value_counts())\ndf_train[\"Side\"] = df_train[\"Side\"].map({\"S\":0,\"P\":1})\nside_train = df_train.dropna(subset=[\"Side\"]).copy()\nside_test = df_train[df_train[\"Side\"].isnull()].copy()\nside_test.drop(\"Side\",axis=1,inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:53:18.065767Z","iopub.execute_input":"2022-07-20T08:53:18.067176Z","iopub.status.idle":"2022-07-20T08:53:18.086694Z","shell.execute_reply.started":"2022-07-20T08:53:18.067132Z","shell.execute_reply":"2022-07-20T08:53:18.085832Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"side = impute_using_model(side_train,side_test,side_test.columns,\"Side\")\nside_test[\"Side\"] = side","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:53:18.087940Z","iopub.execute_input":"2022-07-20T08:53:18.088833Z","iopub.status.idle":"2022-07-20T08:53:22.072234Z","shell.execute_reply.started":"2022-07-20T08:53:18.088796Z","shell.execute_reply":"2022-07-20T08:53:22.071194Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train[\"Deck\"] = df_train[\"Deck\"].map({0:\"A\",1:\"B\",2:\"C\",3:\"D\",4:\"E\",5:\"F\",6:\"G\",7:\"T\"})","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:53:22.073754Z","iopub.execute_input":"2022-07-20T08:53:22.074160Z","iopub.status.idle":"2022-07-20T08:53:22.081571Z","shell.execute_reply.started":"2022-07-20T08:53:22.074120Z","shell.execute_reply":"2022-07-20T08:53:22.080610Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.drop(\"Side\",axis=1,inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:53:22.083155Z","iopub.execute_input":"2022-07-20T08:53:22.083787Z","iopub.status.idle":"2022-07-20T08:53:22.091571Z","shell.execute_reply.started":"2022-07-20T08:53:22.083746Z","shell.execute_reply":"2022-07-20T08:53:22.090396Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train = df_train.join(pd.concat([side_train.Side,side_test.Side]))","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:53:22.093309Z","iopub.execute_input":"2022-07-20T08:53:22.093948Z","iopub.status.idle":"2022-07-20T08:53:22.105941Z","shell.execute_reply.started":"2022-07-20T08:53:22.093909Z","shell.execute_reply":"2022-07-20T08:53:22.104954Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:53:22.107527Z","iopub.execute_input":"2022-07-20T08:53:22.108157Z","iopub.status.idle":"2022-07-20T08:53:22.120646Z","shell.execute_reply.started":"2022-07-20T08:53:22.108123Z","shell.execute_reply":"2022-07-20T08:53:22.119625Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.drop(\"group\",axis=1,inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:53:22.122204Z","iopub.execute_input":"2022-07-20T08:53:22.122918Z","iopub.status.idle":"2022-07-20T08:53:22.129857Z","shell.execute_reply.started":"2022-07-20T08:53:22.122873Z","shell.execute_reply":"2022-07-20T08:53:22.128827Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train = pd.concat([df_train,pd.get_dummies(df_train[\"Deck\"],prefix=\"Deck\")],axis=1)\ndf_train.drop(\"Deck\",axis=1,inplace=True)\ndf_train.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:53:22.132019Z","iopub.execute_input":"2022-07-20T08:53:22.134309Z","iopub.status.idle":"2022-07-20T08:53:22.164597Z","shell.execute_reply.started":"2022-07-20T08:53:22.134272Z","shell.execute_reply":"2022-07-20T08:53:22.163725Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.columns","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:53:22.165772Z","iopub.execute_input":"2022-07-20T08:53:22.167318Z","iopub.status.idle":"2022-07-20T08:53:22.177035Z","shell.execute_reply.started":"2022-07-20T08:53:22.167277Z","shell.execute_reply":"2022-07-20T08:53:22.175727Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.describe()[[\"RoomService\",\"FoodCourt\",\"ShoppingMall\",\"Spa\",\"VRDeck\",\"TotalSum\"]]","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:53:22.178277Z","iopub.execute_input":"2022-07-20T08:53:22.178913Z","iopub.status.idle":"2022-07-20T08:53:22.256177Z","shell.execute_reply.started":"2022-07-20T08:53:22.178884Z","shell.execute_reply":"2022-07-20T08:53:22.253583Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"1. Numerical attributes have been scaled.\n2. Categorical and ordinal attributes have been one-hot and label encoded.\n\nLet's the separate the train and test data and start the modelling experiments.","metadata":{}},{"cell_type":"code","source":"train_ind = df.index\ntest_ind = test.index\nlen(df_train.loc[train_ind,:]),len(df_train.loc[test_ind])","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:53:22.258687Z","iopub.execute_input":"2022-07-20T08:53:22.259358Z","iopub.status.idle":"2022-07-20T08:53:22.275454Z","shell.execute_reply.started":"2022-07-20T08:53:22.259319Z","shell.execute_reply":"2022-07-20T08:53:22.273883Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_train = df_train.loc[train_ind,:]\nmodel_test = df_train.loc[test_ind,:]","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:53:22.277186Z","iopub.execute_input":"2022-07-20T08:53:22.278285Z","iopub.status.idle":"2022-07-20T08:53:22.290324Z","shell.execute_reply.started":"2022-07-20T08:53:22.278250Z","shell.execute_reply":"2022-07-20T08:53:22.289456Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_train[\"Transported\"] = df.loc[train_ind,\"Transported\"].values","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:53:22.291836Z","iopub.execute_input":"2022-07-20T08:53:22.292796Z","iopub.status.idle":"2022-07-20T08:53:22.302192Z","shell.execute_reply.started":"2022-07-20T08:53:22.292758Z","shell.execute_reply":"2022-07-20T08:53:22.301240Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_train.Transported.value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:53:22.304632Z","iopub.execute_input":"2022-07-20T08:53:22.304981Z","iopub.status.idle":"2022-07-20T08:53:22.315770Z","shell.execute_reply.started":"2022-07-20T08:53:22.304947Z","shell.execute_reply":"2022-07-20T08:53:22.314811Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Modelling","metadata":{}},{"cell_type":"code","source":"import xgboost as xgb\n\nX,y = model_train.drop(\"Transported\",axis=1),model_train.Transported\nX_train,X_test,y_train,y_test = train_test_split(X.values,y.values,test_size=0.2,stratify=y,random_state=7)\n\ndtrain = xgb.DMatrix(data=X_train,\n                    label=y_train,\n                    feature_names=X.columns)\n\ndtest = xgb.DMatrix(data=X_test,\n                   label=y_test,\n                   feature_names=X.columns)","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:53:22.317351Z","iopub.execute_input":"2022-07-20T08:53:22.317874Z","iopub.status.idle":"2022-07-20T08:53:24.806365Z","shell.execute_reply.started":"2022-07-20T08:53:22.317837Z","shell.execute_reply":"2022-07-20T08:53:24.805527Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"params = {\n    \"learning_rate\" : 0.1,\n    \"objective\" : \"binary:logistic\",\n    \"seed\" : 12,\n    \"subsample\" : 0.9,\n    \"colsample_bytree\" : 0.8,\n    \"min_child_weight\" : 5,\n    \"max_depth\" : 7\n}\n\neval_cv = xgb.cv(params=params,\n                nfold=5,\n                metrics=\"auc\",\n                dtrain=dtrain,\n                early_stopping_rounds=50,\n                num_boost_round=1000)","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:53:24.807835Z","iopub.execute_input":"2022-07-20T08:53:24.808538Z","iopub.status.idle":"2022-07-20T08:53:31.065251Z","shell.execute_reply.started":"2022-07-20T08:53:24.808500Z","shell.execute_reply":"2022-07-20T08:53:31.064442Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"eval_cv","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:53:31.069765Z","iopub.execute_input":"2022-07-20T08:53:31.071950Z","iopub.status.idle":"2022-07-20T08:53:31.088140Z","shell.execute_reply.started":"2022-07-20T08:53:31.071915Z","shell.execute_reply":"2022-07-20T08:53:31.087126Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# X.columns[:17]\ndtrain_without_weak = xgb.DMatrix(data=X_train[:,:17],\n                                 label=y_train,\n                                 feature_names=X.columns[:17])\ndtest_without_weak = xgb.DMatrix(data=X_test[:,:17],\n                                label=y_test,\n                                feature_names=X.columns[:17])","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:53:31.089592Z","iopub.execute_input":"2022-07-20T08:53:31.090597Z","iopub.status.idle":"2022-07-20T08:53:31.101987Z","shell.execute_reply.started":"2022-07-20T08:53:31.090560Z","shell.execute_reply":"2022-07-20T08:53:31.101105Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"eval_cv_without_weak = xgb.cv(params=params,\n                             nfold=5,\n                             early_stopping_rounds=100,\n                             dtrain=dtrain_without_weak,\n                             num_boost_round=1000,\n                             metrics=\"auc\")","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:53:31.103457Z","iopub.execute_input":"2022-07-20T08:53:31.104520Z","iopub.status.idle":"2022-07-20T08:53:36.018809Z","shell.execute_reply.started":"2022-07-20T08:53:31.104482Z","shell.execute_reply":"2022-07-20T08:53:36.018017Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"eval_cv_without_weak","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:53:36.022822Z","iopub.execute_input":"2022-07-20T08:53:36.025013Z","iopub.status.idle":"2022-07-20T08:53:36.044096Z","shell.execute_reply.started":"2022-07-20T08:53:36.024969Z","shell.execute_reply":"2022-07-20T08:53:36.042976Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = xgb.train(dtrain=dtrain,\n                    params=params,\n                 num_boost_round=1000)","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:53:36.045795Z","iopub.execute_input":"2022-07-20T08:53:36.046342Z","iopub.status.idle":"2022-07-20T08:53:44.040410Z","shell.execute_reply.started":"2022-07-20T08:53:36.046314Z","shell.execute_reply":"2022-07-20T08:53:44.039481Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig,axis = plt.subplots(2,1,figsize=(20,15))\nxgb.plot_importance(model,ax=axis[0],importance_type=\"weight\")\nxgb.plot_importance(model,ax=axis[1],importance_type=\"gain\")","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:53:44.044644Z","iopub.execute_input":"2022-07-20T08:53:44.046753Z","iopub.status.idle":"2022-07-20T08:53:44.962933Z","shell.execute_reply.started":"2022-07-20T08:53:44.046719Z","shell.execute_reply":"2022-07-20T08:53:44.961732Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = model.predict(dtest)","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:53:44.964547Z","iopub.execute_input":"2022-07-20T08:53:44.965173Z","iopub.status.idle":"2022-07-20T08:53:45.060755Z","shell.execute_reply.started":"2022-07-20T08:53:44.965133Z","shell.execute_reply":"2022-07-20T08:53:45.058952Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = np.round(pred,0)","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:53:45.061822Z","iopub.execute_input":"2022-07-20T08:53:45.062199Z","iopub.status.idle":"2022-07-20T08:53:45.073752Z","shell.execute_reply.started":"2022-07-20T08:53:45.062164Z","shell.execute_reply":"2022-07-20T08:53:45.072605Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn import metrics\nprint(metrics.classification_report(y_test,pred))","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:53:45.075728Z","iopub.execute_input":"2022-07-20T08:53:45.081292Z","iopub.status.idle":"2022-07-20T08:53:45.123089Z","shell.execute_reply.started":"2022-07-20T08:53:45.081252Z","shell.execute_reply":"2022-07-20T08:53:45.122111Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dtrain = xgb.DMatrix(data=X,\n                    feature_names=X.columns,\n                    label=y)","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:53:45.130034Z","iopub.execute_input":"2022-07-20T08:53:45.132289Z","iopub.status.idle":"2022-07-20T08:53:45.173821Z","shell.execute_reply.started":"2022-07-20T08:53:45.132252Z","shell.execute_reply":"2022-07-20T08:53:45.172701Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dtest = xgb.DMatrix(data=model_test,feature_names=model_test.columns)","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:53:45.175330Z","iopub.execute_input":"2022-07-20T08:53:45.175683Z","iopub.status.idle":"2022-07-20T08:53:45.206280Z","shell.execute_reply.started":"2022-07-20T08:53:45.175649Z","shell.execute_reply":"2022-07-20T08:53:45.195689Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from skopt import BayesSearchCV\nfrom skopt.space import Real,Integer,Categorical\nfrom sklearn import metrics\n\ncustom_metric = metrics.make_scorer(metrics.roc_auc_score,greater_is_better=True,\n                                    needs_threshold=True)\n\n\nopt = BayesSearchCV(estimator=xgb.XGBClassifier(n_jobs=-1),\n                   search_spaces={\n                       \"learning_rate\" : Real(0.001,0.5,name=\"learning_rate\"),\n                       \"n_estimators\" : Integer(100,1000,name=\"n_estimators\"),\n                       \"max_depth\" : Integer(3,15,name=\"max_depth\"),\n                       \"subsample\" : Real(0.6,1.0,name=\"subsample\"),\n                       \"gamma\" : Real(0,0.6,name=\"gamma\"),\n                       \"colsample_bytree\" : Real(0.6,1.0,name=\"colsample_bytree\"),\n                       \"min_child_weight\" : Integer(3,12,name=\"min_child_weight\")\n                   },\n                   n_iter=50,\n                   verbose=1,\n                   cv=5,\n                   scoring=custom_metric)\n\nopt.fit(X_train,y_train)","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:54:05.877793Z","iopub.execute_input":"2022-07-20T08:54:05.878172Z","iopub.status.idle":"2022-07-20T09:11:28.703470Z","shell.execute_reply.started":"2022-07-20T08:54:05.878141Z","shell.execute_reply":"2022-07-20T09:11:28.702718Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"opt.best_score_","metadata":{"execution":{"iopub.status.busy":"2022-07-20T09:11:28.705523Z","iopub.execute_input":"2022-07-20T09:11:28.705913Z","iopub.status.idle":"2022-07-20T09:11:28.712836Z","shell.execute_reply.started":"2022-07-20T09:11:28.705875Z","shell.execute_reply":"2022-07-20T09:11:28.711808Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"opt.best_estimator_","metadata":{"execution":{"iopub.status.busy":"2022-07-20T09:11:28.714666Z","iopub.execute_input":"2022-07-20T09:11:28.715399Z","iopub.status.idle":"2022-07-20T09:11:28.735885Z","shell.execute_reply.started":"2022-07-20T09:11:28.715361Z","shell.execute_reply":"2022-07-20T09:11:28.734859Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from skopt.plots import plot_objective\nplot_objective(result=opt.optimizer_results_[0])","metadata":{"execution":{"iopub.status.busy":"2022-07-20T09:11:28.738480Z","iopub.execute_input":"2022-07-20T09:11:28.739357Z","iopub.status.idle":"2022-07-20T09:11:58.976906Z","shell.execute_reply.started":"2022-07-20T09:11:28.739320Z","shell.execute_reply":"2022-07-20T09:11:58.975972Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-20T09:11:58.978539Z","iopub.execute_input":"2022-07-20T09:11:58.979222Z","iopub.status.idle":"2022-07-20T09:11:58.986153Z","shell.execute_reply.started":"2022-07-20T09:11:58.979185Z","shell.execute_reply":"2022-07-20T09:11:58.984979Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow.keras import layers","metadata":{"execution":{"iopub.status.busy":"2022-07-20T09:11:58.988263Z","iopub.execute_input":"2022-07-20T09:11:58.989069Z","iopub.status.idle":"2022-07-20T09:12:03.673919Z","shell.execute_reply.started":"2022-07-20T09:11:58.989015Z","shell.execute_reply":"2022-07-20T09:12:03.672920Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_tf = tf.keras.models.Sequential([\n    layers.InputLayer(input_shape=(26)),\n    layers.BatchNormalization(),\n    layers.Dense(300,activation=\"elu\",kernel_initializer=\"he_normal\"),\n    layers.BatchNormalization(),\n    layers.Dense(200,activation=\"elu\",kernel_initializer=\"he_normal\"),\n    layers.BatchNormalization(),\n    layers.Dense(100,activation=\"elu\",kernel_initializer=\"he_normal\"),\n    layers.BatchNormalization(),\n    layers.Dense(50,activation=\"elu\",kernel_initializer=\"he_normal\"),\n    layers.BatchNormalization(),\n    layers.Dense(30,activation=\"elu\",kernel_initializer=\"he_normal\"),\n    layers.BatchNormalization(),\n    layers.Dense(10,activation=\"elu\",kernel_initializer=\"he_normal\"),\n    layers.BatchNormalization(),\n    layers.Dense(1,activation=\"sigmoid\",kernel_initializer=\"he_normal\")\n],name=\"tf_1\")\n\nmodel_tf.compile(loss=\"binary_crossentropy\",\n                optimizer=tf.keras.optimizers.Adam(learning_rate=2e-4),\n                metrics=\"accuracy\")\n\nearly_cb = tf.keras.callbacks.EarlyStopping(patience=10,restore_best_weights=True)\n\nhistory_1 = model_tf.fit(X_train,y_train,\n                        epochs=100,\n                        validation_data=(X_test,y_test),\n                        callbacks=[early_cb])","metadata":{"execution":{"iopub.status.busy":"2022-07-20T09:12:03.675993Z","iopub.execute_input":"2022-07-20T09:12:03.677164Z","iopub.status.idle":"2022-07-20T09:12:32.651373Z","shell.execute_reply.started":"2022-07-20T09:12:03.677124Z","shell.execute_reply":"2022-07-20T09:12:32.650325Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_tf_2 = tf.keras.models.Sequential([\n    layers.InputLayer(input_shape=(26)),\n    layers.BatchNormalization(),\n    layers.Dense(300,kernel_initializer=\"he_normal\"),layers.BatchNormalization(),layers.LeakyReLU(),\n    layers.Dense(200,kernel_initializer=\"he_normal\"),layers.BatchNormalization(),layers.LeakyReLU(),\n    layers.Dense(100,kernel_initializer=\"he_normal\"),layers.BatchNormalization(),layers.LeakyReLU(),\n    layers.Dense(50,kernel_initializer=\"he_normal\"),layers.BatchNormalization(),layers.LeakyReLU(),\n    layers.Dense(30,kernel_initializer=\"he_normal\"),layers.BatchNormalization(),layers.LeakyReLU(),\n    layers.Dense(10,kernel_initializer=\"he_normal\"),layers.BatchNormalization(),layers.LeakyReLU(),\n    layers.Dense(1,activation=\"sigmoid\")\n],name=\"tf_2\")\n\nmodel_tf_2.compile(loss=\"binary_crossentropy\",\n                optimizer=tf.keras.optimizers.Adam(learning_rate=1e-4),\n                metrics=\"accuracy\")\n\nearly_cb = tf.keras.callbacks.EarlyStopping(patience=10,restore_best_weights=True)\n\nhistory_2 = model_tf_2.fit(X_train,y_train,\n                        epochs=100,\n                        validation_data=(X_test,y_test),\n                        callbacks=[early_cb])","metadata":{"execution":{"iopub.status.busy":"2022-07-20T09:12:32.652751Z","iopub.execute_input":"2022-07-20T09:12:32.653118Z","iopub.status.idle":"2022-07-20T09:13:25.990459Z","shell.execute_reply.started":"2022-07-20T09:12:32.653082Z","shell.execute_reply":"2022-07-20T09:13:25.989479Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"K = tf.keras.backend\n\nclass ExponentialDecay(tf.keras.callbacks.Callback):\n    \n    def __init__(self,s=40000):\n        super().__init__()\n        self.s = s\n    \n    def on_batch_begin(self,batch,logs=None):\n        lr = K.get_value(self.model.optimizer.learning_rate)\n        K.set_value(self.model.optimizer.learning_rate,lr*0.1**(1/self.s))\n    \n    def on_epoch_end(self,epoch,logs=None):\n        logs = logs or {}\n        logs[\"lr\"] = K.get_value(self.model.optimizer.learning_rate)","metadata":{"execution":{"iopub.status.busy":"2022-07-20T09:13:25.991819Z","iopub.execute_input":"2022-07-20T09:13:25.992187Z","iopub.status.idle":"2022-07-20T09:13:25.999802Z","shell.execute_reply.started":"2022-07-20T09:13:25.992153Z","shell.execute_reply":"2022-07-20T09:13:25.998804Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_tf_3 = tf.keras.models.Sequential([\n    layers.InputLayer(input_shape=(26)),\n    layers.BatchNormalization(),\n    layers.Dense(400,activation=\"selu\",kernel_initializer=\"lecun_normal\"),\n    layers.Dense(300,activation=\"selu\",kernel_initializer=\"lecun_normal\"),\n    layers.Dense(200,activation=\"selu\",kernel_initializer=\"lecun_normal\"),\n    layers.Dense(100,activation=\"selu\",kernel_initializer=\"lecun_normal\"),\n    layers.Dense(50,activation=\"selu\",kernel_initializer=\"lecun_normal\"),\n    layers.Dense(30,activation=\"selu\",kernel_initializer=\"lecun_normal\"),\n    layers.Dense(10,activation=\"selu\",kernel_initializer=\"lecun_normal\"),\n    layers.Dense(5,activation=\"selu\",kernel_initializer=\"lecun_normal\"),\n    layers.Dense(1,activation=\"sigmoid\")\n])\n\nmodel_tf_3.compile(loss=\"binary_crossentropy\",\n                optimizer=tf.keras.optimizers.Adam(learning_rate=1e-4),\n                metrics=\"accuracy\")\n\nearly_cb = tf.keras.callbacks.EarlyStopping(patience=10,restore_best_weights=True)\nexp_decay = ExponentialDecay()\n\nhistory_3 = model_tf_3.fit(X_train,y_train,\n                        epochs=100,\n                        validation_data=(X_test,y_test),\n                        callbacks=[early_cb,exp_decay])","metadata":{"execution":{"iopub.status.busy":"2022-07-20T09:13:26.003414Z","iopub.execute_input":"2022-07-20T09:13:26.004166Z","iopub.status.idle":"2022-07-20T09:13:52.910785Z","shell.execute_reply.started":"2022-07-20T09:13:26.004129Z","shell.execute_reply":"2022-07-20T09:13:52.909669Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"full_hist = model_tf_3.fit(np.r_[X_train,X_test],np.r_[y_train,y_test])","metadata":{"execution":{"iopub.status.busy":"2022-07-20T09:13:52.912422Z","iopub.execute_input":"2022-07-20T09:13:52.912777Z","iopub.status.idle":"2022-07-20T09:13:55.433812Z","shell.execute_reply.started":"2022-07-20T09:13:52.912743Z","shell.execute_reply":"2022-07-20T09:13:55.432747Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.DataFrame(history_1.history).plot()\npd.DataFrame(history_2.history).plot()\npd.DataFrame(history_3.history).plot()","metadata":{"execution":{"iopub.status.busy":"2022-07-20T09:15:23.876874Z","iopub.execute_input":"2022-07-20T09:15:23.877561Z","iopub.status.idle":"2022-07-20T09:15:24.814116Z","shell.execute_reply.started":"2022-07-20T09:15:23.877518Z","shell.execute_reply":"2022-07-20T09:15:24.812649Z"},"trusted":true},"execution_count":null,"outputs":[]}]}