{"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":"# Importing Libraries","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n\n%matplotlib inline","metadata":{"execution":{"iopub.status.busy":"2022-08-02T17:34:01.063905Z","iopub.execute_input":"2022-08-02T17:34:01.064741Z","iopub.status.idle":"2022-08-02T17:34:02.245815Z","shell.execute_reply.started":"2022-08-02T17:34:01.064626Z","shell.execute_reply":"2022-08-02T17:34:02.244531Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Training and Testing data","metadata":{}},{"cell_type":"markdown","source":"## Loading the Train Data","metadata":{}},{"cell_type":"code","source":"df_train = pd.read_csv(\"../input/spaceship-titanic/train.csv\")\ndf_train","metadata":{"execution":{"iopub.status.busy":"2022-08-02T17:34:02.247726Z","iopub.execute_input":"2022-08-02T17:34:02.248233Z","iopub.status.idle":"2022-08-02T17:34:02.350942Z","shell.execute_reply.started":"2022-08-02T17:34:02.248197Z","shell.execute_reply":"2022-08-02T17:34:02.349793Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.columns","metadata":{"execution":{"iopub.status.busy":"2022-08-02T17:34:02.352239Z","iopub.execute_input":"2022-08-02T17:34:02.352790Z","iopub.status.idle":"2022-08-02T17:34:02.360607Z","shell.execute_reply.started":"2022-08-02T17:34:02.352754Z","shell.execute_reply":"2022-08-02T17:34:02.359512Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.info()","metadata":{"execution":{"iopub.status.busy":"2022-08-02T17:34:02.363453Z","iopub.execute_input":"2022-08-02T17:34:02.363827Z","iopub.status.idle":"2022-08-02T17:34:02.399536Z","shell.execute_reply.started":"2022-08-02T17:34:02.363785Z","shell.execute_reply":"2022-08-02T17:34:02.398546Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.describe()","metadata":{"execution":{"iopub.status.busy":"2022-08-02T17:34:02.400841Z","iopub.execute_input":"2022-08-02T17:34:02.401211Z","iopub.status.idle":"2022-08-02T17:34:02.437747Z","shell.execute_reply.started":"2022-08-02T17:34:02.401177Z","shell.execute_reply":"2022-08-02T17:34:02.436552Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.isnull()","metadata":{"execution":{"iopub.status.busy":"2022-08-02T17:34:02.439426Z","iopub.execute_input":"2022-08-02T17:34:02.439772Z","iopub.status.idle":"2022-08-02T17:34:02.475514Z","shell.execute_reply.started":"2022-08-02T17:34:02.439738Z","shell.execute_reply":"2022-08-02T17:34:02.474131Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-02T17:34:02.477096Z","iopub.execute_input":"2022-08-02T17:34:02.478306Z","iopub.status.idle":"2022-08-02T17:34:02.498403Z","shell.execute_reply.started":"2022-08-02T17:34:02.478259Z","shell.execute_reply":"2022-08-02T17:34:02.497167Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Remove it after\ndf_train.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-02T17:34:02.500054Z","iopub.execute_input":"2022-08-02T17:34:02.500837Z","iopub.status.idle":"2022-08-02T17:34:02.527936Z","shell.execute_reply.started":"2022-08-02T17:34:02.500790Z","shell.execute_reply":"2022-08-02T17:34:02.526747Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# There is no need of Name column. Removing the Name column\ndf_train.drop(\"Name\", axis= 1, inplace= True)","metadata":{"execution":{"iopub.status.busy":"2022-08-02T17:34:02.529436Z","iopub.execute_input":"2022-08-02T17:34:02.530265Z","iopub.status.idle":"2022-08-02T17:34:02.537453Z","shell.execute_reply.started":"2022-08-02T17:34:02.530223Z","shell.execute_reply":"2022-08-02T17:34:02.536227Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.dtypes","metadata":{"execution":{"iopub.status.busy":"2022-08-02T17:34:02.542352Z","iopub.execute_input":"2022-08-02T17:34:02.543224Z","iopub.status.idle":"2022-08-02T17:34:02.559521Z","shell.execute_reply.started":"2022-08-02T17:34:02.543172Z","shell.execute_reply":"2022-08-02T17:34:02.557890Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.shape","metadata":{"execution":{"iopub.status.busy":"2022-08-02T17:34:02.561722Z","iopub.execute_input":"2022-08-02T17:34:02.562418Z","iopub.status.idle":"2022-08-02T17:34:02.569172Z","shell.execute_reply.started":"2022-08-02T17:34:02.562369Z","shell.execute_reply":"2022-08-02T17:34:02.568209Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.ndim","metadata":{"execution":{"iopub.status.busy":"2022-08-02T17:34:02.570698Z","iopub.execute_input":"2022-08-02T17:34:02.571394Z","iopub.status.idle":"2022-08-02T17:34:02.585105Z","shell.execute_reply.started":"2022-08-02T17:34:02.571348Z","shell.execute_reply":"2022-08-02T17:34:02.583879Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.size","metadata":{"execution":{"iopub.status.busy":"2022-08-02T17:34:02.586872Z","iopub.execute_input":"2022-08-02T17:34:02.587990Z","iopub.status.idle":"2022-08-02T17:34:02.599133Z","shell.execute_reply.started":"2022-08-02T17:34:02.587944Z","shell.execute_reply":"2022-08-02T17:34:02.598094Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Graph","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(10,6))\nsns.histplot(data= df_train, x= \"HomePlanet\", y= \"Destination\", label=True, hue=5);","metadata":{"execution":{"iopub.status.busy":"2022-08-02T17:34:02.600838Z","iopub.execute_input":"2022-08-02T17:34:02.601642Z","iopub.status.idle":"2022-08-02T17:34:02.821569Z","shell.execute_reply.started":"2022-08-02T17:34:02.601536Z","shell.execute_reply":"2022-08-02T17:34:02.820249Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(15,7));\nplt.grid()\nsns.histplot(data= df_train, x= \"Age\", bins=30);","metadata":{"execution":{"iopub.status.busy":"2022-08-02T17:34:02.827648Z","iopub.execute_input":"2022-08-02T17:34:02.830929Z","iopub.status.idle":"2022-08-02T17:34:03.118326Z","shell.execute_reply.started":"2022-08-02T17:34:02.830867Z","shell.execute_reply":"2022-08-02T17:34:03.117320Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10,6))\nsns.jointplot(data = df_train, x = 'Age', color='blue', kind= 'kde');","metadata":{"execution":{"iopub.status.busy":"2022-08-02T17:34:03.122323Z","iopub.execute_input":"2022-08-02T17:34:03.122783Z","iopub.status.idle":"2022-08-02T17:34:03.607590Z","shell.execute_reply.started":"2022-08-02T17:34:03.122725Z","shell.execute_reply":"2022-08-02T17:34:03.606125Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Handling Null Values","metadata":{}},{"cell_type":"code","source":"sns.heatmap(df_train.isnull(), yticklabels=False, cbar=False, cmap='viridis');","metadata":{"execution":{"iopub.status.busy":"2022-08-02T17:34:03.609116Z","iopub.execute_input":"2022-08-02T17:34:03.609571Z","iopub.status.idle":"2022-08-02T17:34:03.923518Z","shell.execute_reply.started":"2022-08-02T17:34:03.609526Z","shell.execute_reply":"2022-08-02T17:34:03.922290Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Filling up the null values\nfor i in df_train.columns:\n    if df_train[i].dtypes == 'object':\n        df_train[i].fillna(df_train[i].mode()[0], inplace=True)\n    else:\n        df_train[i].fillna(df_train[i].median(), inplace=True)\nprint(df_train)","metadata":{"execution":{"iopub.status.busy":"2022-08-02T17:34:05.759303Z","iopub.execute_input":"2022-08-02T17:34:05.759693Z","iopub.status.idle":"2022-08-02T17:34:05.802617Z","shell.execute_reply.started":"2022-08-02T17:34:05.759662Z","shell.execute_reply":"2022-08-02T17:34:05.801653Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Separating the values of Cabin to Deck and Side\ndf_train[\"Cabin_Deck\"] = df_train[\"Cabin\"].str.split(\"/\", expand=True)[0]\ndf_train[\"Cabin_Side\"] = df_train[\"Cabin\"].str.split(\"/\", expand=True)[2]","metadata":{"execution":{"iopub.status.busy":"2022-08-02T17:34:06.439276Z","iopub.execute_input":"2022-08-02T17:34:06.440100Z","iopub.status.idle":"2022-08-02T17:34:06.478875Z","shell.execute_reply.started":"2022-08-02T17:34:06.439999Z","shell.execute_reply":"2022-08-02T17:34:06.477879Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Separating the values of PassengerId to Group and No. of passenger within group\n\ndf_train[\"Group\"] = df_train[\"PassengerId\"].str.split(\"_\", expand=True)[0]\ndf_train[\"Num_within_Group\"] = df_train[\"PassengerId\"].str.split(\"_\", expand=True)[1]\n","metadata":{"execution":{"iopub.status.busy":"2022-08-02T17:34:06.480745Z","iopub.execute_input":"2022-08-02T17:34:06.481179Z","iopub.status.idle":"2022-08-02T17:34:06.518024Z","shell.execute_reply.started":"2022-08-02T17:34:06.481148Z","shell.execute_reply":"2022-08-02T17:34:06.517109Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# As we can see from the heatmap now, no null values are present now.\nsns.heatmap(df_train.isnull(), yticklabels=False, cbar=False, cmap='viridis');","metadata":{"execution":{"iopub.status.busy":"2022-08-02T17:34:06.545056Z","iopub.execute_input":"2022-08-02T17:34:06.545562Z","iopub.status.idle":"2022-08-02T17:34:06.906178Z","shell.execute_reply.started":"2022-08-02T17:34:06.545523Z","shell.execute_reply":"2022-08-02T17:34:06.905032Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train[\"Destination\"].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-08-02T17:34:06.908104Z","iopub.execute_input":"2022-08-02T17:34:06.908547Z","iopub.status.idle":"2022-08-02T17:34:06.917933Z","shell.execute_reply.started":"2022-08-02T17:34:06.908513Z","shell.execute_reply":"2022-08-02T17:34:06.916760Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train[\"Cabin\"].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-08-02T17:34:06.919492Z","iopub.execute_input":"2022-08-02T17:34:06.919933Z","iopub.status.idle":"2022-08-02T17:34:06.935618Z","shell.execute_reply.started":"2022-08-02T17:34:06.919892Z","shell.execute_reply":"2022-08-02T17:34:06.934474Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Categorical columns data to Numerical data","metadata":{}},{"cell_type":"code","source":"from sklearn.preprocessing import LabelEncoder","metadata":{"execution":{"iopub.status.busy":"2022-08-02T17:34:06.937007Z","iopub.execute_input":"2022-08-02T17:34:06.937746Z","iopub.status.idle":"2022-08-02T17:34:07.198533Z","shell.execute_reply.started":"2022-08-02T17:34:06.937711Z","shell.execute_reply":"2022-08-02T17:34:07.197328Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"le = LabelEncoder()\ndf_train['HomePlanet'] = le.fit_transform(df_train['HomePlanet'])","metadata":{"execution":{"iopub.status.busy":"2022-08-02T17:34:07.201193Z","iopub.execute_input":"2022-08-02T17:34:07.201859Z","iopub.status.idle":"2022-08-02T17:34:07.210839Z","shell.execute_reply.started":"2022-08-02T17:34:07.201809Z","shell.execute_reply":"2022-08-02T17:34:07.209248Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train['CryoSleep'] = le.fit_transform(df_train['CryoSleep'])","metadata":{"execution":{"iopub.status.busy":"2022-08-02T17:34:07.294535Z","iopub.execute_input":"2022-08-02T17:34:07.294933Z","iopub.status.idle":"2022-08-02T17:34:07.301041Z","shell.execute_reply.started":"2022-08-02T17:34:07.294901Z","shell.execute_reply":"2022-08-02T17:34:07.300194Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train['Cabin'] = le.fit_transform(df_train['Cabin'])","metadata":{"execution":{"iopub.status.busy":"2022-08-02T17:34:07.303126Z","iopub.execute_input":"2022-08-02T17:34:07.303435Z","iopub.status.idle":"2022-08-02T17:34:07.335403Z","shell.execute_reply.started":"2022-08-02T17:34:07.303407Z","shell.execute_reply":"2022-08-02T17:34:07.334152Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train['VIP'] = le.fit_transform(df_train['VIP'])","metadata":{"execution":{"iopub.status.busy":"2022-08-02T17:34:07.336878Z","iopub.execute_input":"2022-08-02T17:34:07.337590Z","iopub.status.idle":"2022-08-02T17:34:07.344282Z","shell.execute_reply.started":"2022-08-02T17:34:07.337543Z","shell.execute_reply":"2022-08-02T17:34:07.343017Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train['Cabin_Deck'] = le.fit_transform(df_train['Cabin_Deck'])","metadata":{"execution":{"iopub.status.busy":"2022-08-02T17:34:07.346528Z","iopub.execute_input":"2022-08-02T17:34:07.347700Z","iopub.status.idle":"2022-08-02T17:34:07.358910Z","shell.execute_reply.started":"2022-08-02T17:34:07.347653Z","shell.execute_reply":"2022-08-02T17:34:07.357918Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train['Cabin_Side'] = le.fit_transform(df_train['Cabin_Side'])","metadata":{"execution":{"iopub.status.busy":"2022-08-02T17:34:07.361171Z","iopub.execute_input":"2022-08-02T17:34:07.361604Z","iopub.status.idle":"2022-08-02T17:34:07.374434Z","shell.execute_reply.started":"2022-08-02T17:34:07.361573Z","shell.execute_reply":"2022-08-02T17:34:07.373577Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train['Destination'] = le.fit_transform(df_train['Destination'])","metadata":{"execution":{"iopub.status.busy":"2022-08-02T17:34:07.426339Z","iopub.execute_input":"2022-08-02T17:34:07.426838Z","iopub.status.idle":"2022-08-02T17:34:07.436202Z","shell.execute_reply.started":"2022-08-02T17:34:07.426791Z","shell.execute_reply":"2022-08-02T17:34:07.434898Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train['Transported'] = le.fit_transform(df_train['Transported'])","metadata":{"execution":{"iopub.status.busy":"2022-08-02T17:34:07.438724Z","iopub.execute_input":"2022-08-02T17:34:07.439447Z","iopub.status.idle":"2022-08-02T17:34:07.451460Z","shell.execute_reply.started":"2022-08-02T17:34:07.439402Z","shell.execute_reply":"2022-08-02T17:34:07.450483Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-02T17:34:07.452877Z","iopub.execute_input":"2022-08-02T17:34:07.453275Z","iopub.status.idle":"2022-08-02T17:34:07.480337Z","shell.execute_reply.started":"2022-08-02T17:34:07.453243Z","shell.execute_reply":"2022-08-02T17:34:07.479532Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Correlation Matrix","metadata":{}},{"cell_type":"code","source":"df_train.corr()","metadata":{"execution":{"iopub.status.busy":"2022-08-02T17:34:07.481618Z","iopub.execute_input":"2022-08-02T17:34:07.481946Z","iopub.status.idle":"2022-08-02T17:34:07.513400Z","shell.execute_reply.started":"2022-08-02T17:34:07.481914Z","shell.execute_reply":"2022-08-02T17:34:07.512149Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"corr_matrix = df_train.corr()\nfig, ax = plt.subplots(figsize = (20, 10))\n\nax = sns.heatmap(corr_matrix,\n                 annot=True,\n                 linewidths=0.5,\n                fmt=\".2f\",\n                cmap=\"RdYlBu\")","metadata":{"execution":{"iopub.status.busy":"2022-08-02T17:34:07.517172Z","iopub.execute_input":"2022-08-02T17:34:07.517592Z","iopub.status.idle":"2022-08-02T17:34:08.661561Z","shell.execute_reply.started":"2022-08-02T17:34:07.517559Z","shell.execute_reply":"2022-08-02T17:34:08.660134Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Loading the Test data","metadata":{}},{"cell_type":"code","source":"df_test = pd.read_csv(\"../input/spaceship-titanic/test.csv\")\ndf_test.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-02T17:34:08.663131Z","iopub.execute_input":"2022-08-02T17:34:08.664117Z","iopub.status.idle":"2022-08-02T17:34:08.709428Z","shell.execute_reply.started":"2022-08-02T17:34:08.664051Z","shell.execute_reply":"2022-08-02T17:34:08.708451Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test.columns","metadata":{"execution":{"iopub.status.busy":"2022-08-02T17:34:08.710942Z","iopub.execute_input":"2022-08-02T17:34:08.711303Z","iopub.status.idle":"2022-08-02T17:34:08.717697Z","shell.execute_reply.started":"2022-08-02T17:34:08.711269Z","shell.execute_reply":"2022-08-02T17:34:08.716882Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test.info()","metadata":{"execution":{"iopub.status.busy":"2022-08-02T17:34:08.718766Z","iopub.execute_input":"2022-08-02T17:34:08.719599Z","iopub.status.idle":"2022-08-02T17:34:08.740943Z","shell.execute_reply.started":"2022-08-02T17:34:08.719566Z","shell.execute_reply":"2022-08-02T17:34:08.739805Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test.describe()","metadata":{"execution":{"iopub.status.busy":"2022-08-02T17:34:08.742300Z","iopub.execute_input":"2022-08-02T17:34:08.742746Z","iopub.status.idle":"2022-08-02T17:34:08.774305Z","shell.execute_reply.started":"2022-08-02T17:34:08.742713Z","shell.execute_reply":"2022-08-02T17:34:08.773200Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test.isnull()","metadata":{"execution":{"iopub.status.busy":"2022-08-02T17:34:08.775831Z","iopub.execute_input":"2022-08-02T17:34:08.776643Z","iopub.status.idle":"2022-08-02T17:34:08.802402Z","shell.execute_reply.started":"2022-08-02T17:34:08.776608Z","shell.execute_reply":"2022-08-02T17:34:08.800826Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-02T17:34:08.803652Z","iopub.execute_input":"2022-08-02T17:34:08.804475Z","iopub.status.idle":"2022-08-02T17:34:08.816136Z","shell.execute_reply.started":"2022-08-02T17:34:08.804428Z","shell.execute_reply":"2022-08-02T17:34:08.814932Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# There is no need of Name column. Removing the Name column\ndf_test.drop(\"Name\", axis= 1, inplace= True)","metadata":{"execution":{"iopub.status.busy":"2022-08-02T17:34:08.817677Z","iopub.execute_input":"2022-08-02T17:34:08.818681Z","iopub.status.idle":"2022-08-02T17:34:08.826267Z","shell.execute_reply.started":"2022-08-02T17:34:08.818648Z","shell.execute_reply":"2022-08-02T17:34:08.825405Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test.dtypes","metadata":{"execution":{"iopub.status.busy":"2022-08-02T17:34:08.827467Z","iopub.execute_input":"2022-08-02T17:34:08.827944Z","iopub.status.idle":"2022-08-02T17:34:08.841152Z","shell.execute_reply.started":"2022-08-02T17:34:08.827911Z","shell.execute_reply":"2022-08-02T17:34:08.840315Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test.shape","metadata":{"execution":{"iopub.status.busy":"2022-08-02T17:34:08.846260Z","iopub.execute_input":"2022-08-02T17:34:08.846865Z","iopub.status.idle":"2022-08-02T17:34:08.859365Z","shell.execute_reply.started":"2022-08-02T17:34:08.846831Z","shell.execute_reply":"2022-08-02T17:34:08.858457Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test.ndim","metadata":{"execution":{"iopub.status.busy":"2022-08-02T17:34:08.861578Z","iopub.execute_input":"2022-08-02T17:34:08.862141Z","iopub.status.idle":"2022-08-02T17:34:08.874651Z","shell.execute_reply.started":"2022-08-02T17:34:08.862097Z","shell.execute_reply":"2022-08-02T17:34:08.873267Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test.size","metadata":{"execution":{"iopub.status.busy":"2022-08-02T17:34:08.876160Z","iopub.execute_input":"2022-08-02T17:34:08.877390Z","iopub.status.idle":"2022-08-02T17:34:08.888575Z","shell.execute_reply.started":"2022-08-02T17:34:08.877356Z","shell.execute_reply":"2022-08-02T17:34:08.887356Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Graph","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(10,6))\nsns.histplot(data= df_test, x= \"HomePlanet\", y= \"Destination\", label=True, hue=5);","metadata":{"execution":{"iopub.status.busy":"2022-08-02T17:34:08.890168Z","iopub.execute_input":"2022-08-02T17:34:08.890909Z","iopub.status.idle":"2022-08-02T17:34:09.078536Z","shell.execute_reply.started":"2022-08-02T17:34:08.890875Z","shell.execute_reply":"2022-08-02T17:34:09.077138Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(15,7));\nplt.grid()\nsns.histplot(data= df_test, x= \"Age\", bins=30);","metadata":{"execution":{"iopub.status.busy":"2022-08-02T17:34:09.083827Z","iopub.execute_input":"2022-08-02T17:34:09.084418Z","iopub.status.idle":"2022-08-02T17:34:09.450476Z","shell.execute_reply.started":"2022-08-02T17:34:09.084368Z","shell.execute_reply":"2022-08-02T17:34:09.449314Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10,6))\nsns.jointplot(data = df_test, x = 'Age', color='blue', kind= 'kde');","metadata":{"execution":{"iopub.status.busy":"2022-08-02T17:34:09.451970Z","iopub.execute_input":"2022-08-02T17:34:09.452452Z","iopub.status.idle":"2022-08-02T17:34:09.941889Z","shell.execute_reply.started":"2022-08-02T17:34:09.452407Z","shell.execute_reply":"2022-08-02T17:34:09.941088Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Handling Null Values","metadata":{}},{"cell_type":"code","source":"sns.heatmap(df_test.isnull(), yticklabels=False, cbar=False, cmap='viridis');","metadata":{"execution":{"iopub.status.busy":"2022-08-02T17:34:09.943036Z","iopub.execute_input":"2022-08-02T17:34:09.943660Z","iopub.status.idle":"2022-08-02T17:34:10.168530Z","shell.execute_reply.started":"2022-08-02T17:34:09.943626Z","shell.execute_reply":"2022-08-02T17:34:10.167500Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Filling up the null values\nfor i in df_test.columns:\n    if df_test[i].dtypes == 'object':\n        df_test[i].fillna(df_test[i].mode()[0], inplace=True)\n    else:\n        df_test[i].fillna(df_test[i].median(), inplace=True)\nprint(df_test)","metadata":{"execution":{"iopub.status.busy":"2022-08-02T17:34:10.169609Z","iopub.execute_input":"2022-08-02T17:34:10.170643Z","iopub.status.idle":"2022-08-02T17:34:10.201959Z","shell.execute_reply.started":"2022-08-02T17:34:10.170605Z","shell.execute_reply":"2022-08-02T17:34:10.200756Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Separating the values of Cabin to Deck and Side\ndf_test[\"Cabin_Deck\"] = df_test[\"Cabin\"].str.split(\"/\", expand=True)[0]\ndf_test[\"Cabin_Side\"] = df_test[\"Cabin\"].str.split(\"/\", expand=True)[2]","metadata":{"execution":{"iopub.status.busy":"2022-08-02T17:34:10.203373Z","iopub.execute_input":"2022-08-02T17:34:10.204158Z","iopub.status.idle":"2022-08-02T17:34:10.227310Z","shell.execute_reply.started":"2022-08-02T17:34:10.204112Z","shell.execute_reply":"2022-08-02T17:34:10.225943Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Separating the values of PassengerId to Group and No. of passenger within group\n\ndf_test[\"Group\"] = df_test[\"PassengerId\"].str.split(\"_\", expand=True)[0]\ndf_test[\"Num_within_Group\"] = df_test[\"PassengerId\"].str.split(\"_\", expand=True)[1]\n","metadata":{"execution":{"iopub.status.busy":"2022-08-02T17:34:10.228774Z","iopub.execute_input":"2022-08-02T17:34:10.229215Z","iopub.status.idle":"2022-08-02T17:34:10.255740Z","shell.execute_reply.started":"2022-08-02T17:34:10.229171Z","shell.execute_reply":"2022-08-02T17:34:10.254734Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# As we can see from the heatmap now, no null values are present now.\nsns.heatmap(df_test.isnull(), yticklabels=False, cbar=False, cmap='viridis');","metadata":{"execution":{"iopub.status.busy":"2022-08-02T17:34:10.257614Z","iopub.execute_input":"2022-08-02T17:34:10.258361Z","iopub.status.idle":"2022-08-02T17:34:10.535939Z","shell.execute_reply.started":"2022-08-02T17:34:10.258315Z","shell.execute_reply":"2022-08-02T17:34:10.534960Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test[\"Destination\"].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-08-02T17:34:10.537642Z","iopub.execute_input":"2022-08-02T17:34:10.538215Z","iopub.status.idle":"2022-08-02T17:34:10.551429Z","shell.execute_reply.started":"2022-08-02T17:34:10.538168Z","shell.execute_reply":"2022-08-02T17:34:10.550231Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test[\"Cabin\"].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-08-02T17:34:10.552984Z","iopub.execute_input":"2022-08-02T17:34:10.554267Z","iopub.status.idle":"2022-08-02T17:34:10.569326Z","shell.execute_reply.started":"2022-08-02T17:34:10.554217Z","shell.execute_reply":"2022-08-02T17:34:10.568290Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Categorical columns data to Numerical data","metadata":{}},{"cell_type":"code","source":"from sklearn.preprocessing import LabelEncoder","metadata":{"execution":{"iopub.status.busy":"2022-08-02T17:34:10.570571Z","iopub.execute_input":"2022-08-02T17:34:10.571109Z","iopub.status.idle":"2022-08-02T17:34:10.579127Z","shell.execute_reply.started":"2022-08-02T17:34:10.571054Z","shell.execute_reply":"2022-08-02T17:34:10.578207Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"le = LabelEncoder()\ndf_test['HomePlanet'] = le.fit_transform(df_test['HomePlanet'])","metadata":{"execution":{"iopub.status.busy":"2022-08-02T17:34:10.580402Z","iopub.execute_input":"2022-08-02T17:34:10.580912Z","iopub.status.idle":"2022-08-02T17:34:10.601362Z","shell.execute_reply.started":"2022-08-02T17:34:10.580880Z","shell.execute_reply":"2022-08-02T17:34:10.600166Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test['CryoSleep'] = le.fit_transform(df_test['CryoSleep'])","metadata":{"execution":{"iopub.status.busy":"2022-08-02T17:34:10.603821Z","iopub.execute_input":"2022-08-02T17:34:10.604217Z","iopub.status.idle":"2022-08-02T17:34:10.615089Z","shell.execute_reply.started":"2022-08-02T17:34:10.604182Z","shell.execute_reply":"2022-08-02T17:34:10.613823Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test['Cabin'] = le.fit_transform(df_test['Cabin'])","metadata":{"execution":{"iopub.status.busy":"2022-08-02T17:34:10.617264Z","iopub.execute_input":"2022-08-02T17:34:10.617620Z","iopub.status.idle":"2022-08-02T17:34:10.637577Z","shell.execute_reply.started":"2022-08-02T17:34:10.617586Z","shell.execute_reply":"2022-08-02T17:34:10.636358Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test['VIP'] = le.fit_transform(df_test['VIP'])","metadata":{"execution":{"iopub.status.busy":"2022-08-02T17:34:10.639325Z","iopub.execute_input":"2022-08-02T17:34:10.639728Z","iopub.status.idle":"2022-08-02T17:34:10.648089Z","shell.execute_reply.started":"2022-08-02T17:34:10.639685Z","shell.execute_reply":"2022-08-02T17:34:10.646893Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test['Cabin_Deck'] = le.fit_transform(df_test['Cabin_Deck'])","metadata":{"execution":{"iopub.status.busy":"2022-08-02T17:34:10.649459Z","iopub.execute_input":"2022-08-02T17:34:10.650532Z","iopub.status.idle":"2022-08-02T17:34:10.661849Z","shell.execute_reply.started":"2022-08-02T17:34:10.650488Z","shell.execute_reply":"2022-08-02T17:34:10.660956Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test['Cabin_Side'] = le.fit_transform(df_test['Cabin_Side'])","metadata":{"execution":{"iopub.status.busy":"2022-08-02T17:34:10.663556Z","iopub.execute_input":"2022-08-02T17:34:10.664291Z","iopub.status.idle":"2022-08-02T17:34:10.675592Z","shell.execute_reply.started":"2022-08-02T17:34:10.664247Z","shell.execute_reply":"2022-08-02T17:34:10.674521Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test['Destination'] = le.fit_transform(df_test['Destination'])","metadata":{"execution":{"iopub.status.busy":"2022-08-02T17:34:10.677170Z","iopub.execute_input":"2022-08-02T17:34:10.678153Z","iopub.status.idle":"2022-08-02T17:34:10.692143Z","shell.execute_reply.started":"2022-08-02T17:34:10.678105Z","shell.execute_reply":"2022-08-02T17:34:10.690939Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-02T17:34:10.693845Z","iopub.execute_input":"2022-08-02T17:34:10.694558Z","iopub.status.idle":"2022-08-02T17:34:10.720628Z","shell.execute_reply.started":"2022-08-02T17:34:10.694514Z","shell.execute_reply":"2022-08-02T17:34:10.719735Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Correlation Matrix","metadata":{}},{"cell_type":"code","source":"df_test.corr()","metadata":{"execution":{"iopub.status.busy":"2022-08-02T17:34:10.721854Z","iopub.execute_input":"2022-08-02T17:34:10.722599Z","iopub.status.idle":"2022-08-02T17:34:10.755334Z","shell.execute_reply.started":"2022-08-02T17:34:10.722558Z","shell.execute_reply":"2022-08-02T17:34:10.754363Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"corr_matrix = df_test.corr()\nfig, ax = plt.subplots(figsize = (20, 10))\n\nax = sns.heatmap(corr_matrix,\n                 annot=True,\n                 linewidths=0.5,\n                fmt=\".2f\",\n                cmap=\"RdYlBu\")","metadata":{"execution":{"iopub.status.busy":"2022-08-02T17:34:10.756659Z","iopub.execute_input":"2022-08-02T17:34:10.757231Z","iopub.status.idle":"2022-08-02T17:34:11.936172Z","shell.execute_reply.started":"2022-08-02T17:34:10.757197Z","shell.execute_reply":"2022-08-02T17:34:11.935183Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Machine Learning","metadata":{}},{"cell_type":"markdown","source":"## Training and Testing Data","metadata":{}},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split","metadata":{"execution":{"iopub.status.busy":"2022-08-02T17:34:11.937425Z","iopub.execute_input":"2022-08-02T17:34:11.938139Z","iopub.status.idle":"2022-08-02T17:34:12.006543Z","shell.execute_reply.started":"2022-08-02T17:34:11.938088Z","shell.execute_reply":"2022-08-02T17:34:12.005468Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.columns","metadata":{"execution":{"iopub.status.busy":"2022-08-02T17:34:12.007890Z","iopub.execute_input":"2022-08-02T17:34:12.008250Z","iopub.status.idle":"2022-08-02T17:34:12.015019Z","shell.execute_reply.started":"2022-08-02T17:34:12.008216Z","shell.execute_reply":"2022-08-02T17:34:12.014150Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = df_train.drop(\"Transported\", axis=1)\ny = df_train[\"Transported\"]\nX_train, X_test, y_train, y_test = train_test_split(X,\n                                                    y,\n                                                    random_state=101)","metadata":{"execution":{"iopub.status.busy":"2022-08-02T17:34:12.016180Z","iopub.execute_input":"2022-08-02T17:34:12.017318Z","iopub.status.idle":"2022-08-02T17:34:12.033985Z","shell.execute_reply.started":"2022-08-02T17:34:12.017281Z","shell.execute_reply":"2022-08-02T17:34:12.032783Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Model","metadata":{}},{"cell_type":"code","source":"from sklearn.ensemble import RandomForestRegressor, RandomForestClassifier","metadata":{"execution":{"iopub.status.busy":"2022-08-02T17:34:12.041377Z","iopub.execute_input":"2022-08-02T17:34:12.041758Z","iopub.status.idle":"2022-08-02T17:34:12.276415Z","shell.execute_reply.started":"2022-08-02T17:34:12.041723Z","shell.execute_reply":"2022-08-02T17:34:12.275253Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rfr = RandomForestClassifier(random_state = 1, n_estimators = 10, min_samples_split = 2)\nrfr.fit(X_train, y_train)","metadata":{"execution":{"iopub.status.busy":"2022-08-02T17:34:12.277790Z","iopub.execute_input":"2022-08-02T17:34:12.278135Z","iopub.status.idle":"2022-08-02T17:34:12.425790Z","shell.execute_reply.started":"2022-08-02T17:34:12.278103Z","shell.execute_reply":"2022-08-02T17:34:12.424594Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Predictions\npred = rfr.predict(X_test)\n\ndef transported(n):\n    if n > 0.7:\n        return True\n    else:\n        return False\n\nresult = pd.DataFrame({'y_result': y_test,\n                       'prediction': pred})\nresult.prediction = result.prediction.apply(transported)\nresult['Equal'] = np.where(result[\"y_result\"] == result[\"prediction\"], True, False)\nresult['Equal'].value_counts(normalize =True)","metadata":{"execution":{"iopub.status.busy":"2022-08-02T17:34:12.429250Z","iopub.execute_input":"2022-08-02T17:34:12.429780Z","iopub.status.idle":"2022-08-02T17:34:12.456029Z","shell.execute_reply.started":"2022-08-02T17:34:12.429746Z","shell.execute_reply":"2022-08-02T17:34:12.454854Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"RandomForestClassifier()","metadata":{"execution":{"iopub.status.busy":"2022-08-02T17:34:12.457247Z","iopub.execute_input":"2022-08-02T17:34:12.457735Z","iopub.status.idle":"2022-08-02T17:34:12.463988Z","shell.execute_reply.started":"2022-08-02T17:34:12.457705Z","shell.execute_reply":"2022-08-02T17:34:12.463122Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import RandomizedSearchCV","metadata":{"execution":{"iopub.status.busy":"2022-08-02T17:34:12.465356Z","iopub.execute_input":"2022-08-02T17:34:12.465914Z","iopub.status.idle":"2022-08-02T17:34:12.476465Z","shell.execute_reply.started":"2022-08-02T17:34:12.465882Z","shell.execute_reply":"2022-08-02T17:34:12.475424Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"param_grid = {\n    \"n_estimators\": np.arange(10, 1000, 50),\n          \"max_depth\": [None, 3, 5, 10],\n          \"min_samples_split\": np.arange(2, 20, 2),\n          \"min_samples_leaf\": np.arange(1, 20, 2)\n}","metadata":{"execution":{"iopub.status.busy":"2022-08-02T17:34:12.477753Z","iopub.execute_input":"2022-08-02T17:34:12.478134Z","iopub.status.idle":"2022-08-02T17:34:12.488613Z","shell.execute_reply.started":"2022-08-02T17:34:12.478095Z","shell.execute_reply":"2022-08-02T17:34:12.487538Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.random.seed(42)\n\n# Setup random hyperparameter search for LogisticRegression\nrs_rf = RandomizedSearchCV(RandomForestClassifier(),\n                          param_distributions=param_grid,\n                          cv=5,\n                          n_iter=20,\n                          verbose = True)\n\n\n# Fit random hyperparameter search model for LogisticRegression\nrs_rf.fit(X_train, y_train)","metadata":{"execution":{"iopub.status.busy":"2022-08-02T17:34:12.489994Z","iopub.execute_input":"2022-08-02T17:34:12.490644Z","iopub.status.idle":"2022-08-02T17:39:18.106511Z","shell.execute_reply.started":"2022-08-02T17:34:12.490601Z","shell.execute_reply":"2022-08-02T17:39:18.105487Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rs_rf.best_params_","metadata":{"execution":{"iopub.status.busy":"2022-08-02T17:39:18.108048Z","iopub.execute_input":"2022-08-02T17:39:18.108690Z","iopub.status.idle":"2022-08-02T17:39:18.115657Z","shell.execute_reply.started":"2022-08-02T17:39:18.108655Z","shell.execute_reply":"2022-08-02T17:39:18.114571Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rs_rf.score(X_test, y_test)","metadata":{"execution":{"iopub.status.busy":"2022-08-02T17:39:18.116815Z","iopub.execute_input":"2022-08-02T17:39:18.117209Z","iopub.status.idle":"2022-08-02T17:39:18.278757Z","shell.execute_reply.started":"2022-08-02T17:39:18.117177Z","shell.execute_reply":"2022-08-02T17:39:18.277695Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Prediction for the test dataset","metadata":{}},{"cell_type":"code","source":"y_df = rs_rf.predict(df_test)\ny_df","metadata":{"execution":{"iopub.status.busy":"2022-08-02T17:39:18.280389Z","iopub.execute_input":"2022-08-02T17:39:18.280692Z","iopub.status.idle":"2022-08-02T17:39:18.516512Z","shell.execute_reply.started":"2022-08-02T17:39:18.280664Z","shell.execute_reply":"2022-08-02T17:39:18.515289Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Saving to submission.csv","metadata":{}},{"cell_type":"code","source":"ls = []\nfor i in range(len(y_df)):\n    if y_df[i] == 1:\n        ls.append('True')\n    else:\n        ls.append('False')\nls\n\n# Length of the list ls\narr = np.array(ls)\nlen(arr)\n\n# Converting the data into the DataFrame\nentry = pd.DataFrame(\n    {'PassengerId':df_test[\"PassengerId\"] ,\n     'Transported': arr},columns=['PassengerId', 'Transported'])\n\n# Saving the DataFrame into submission.csv\nentry.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-08-02T17:40:15.178666Z","iopub.execute_input":"2022-08-02T17:40:15.179100Z","iopub.status.idle":"2022-08-02T17:40:15.200420Z","shell.execute_reply.started":"2022-08-02T17:40:15.179036Z","shell.execute_reply":"2022-08-02T17:40:15.199449Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}