{"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":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport seaborn as sns\nfrom xgboost import XGBClassifier\nfrom sklearn.linear_model import LogisticRegression\nfrom catboost import CatBoostClassifier\nfrom sklearn.ensemble import GradientBoostingClassifier,VotingClassifier\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-08-05T14:02:06.997317Z","iopub.execute_input":"2022-08-05T14:02:06.998257Z","iopub.status.idle":"2022-08-05T14:02:08.378082Z","shell.execute_reply.started":"2022-08-05T14:02:06.998132Z","shell.execute_reply":"2022-08-05T14:02:08.376708Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"space_tin_train=pd.read_csv('../input/spaceship-titanic/train.csv')\nspace_tin_test=pd.read_csv('../input/spaceship-titanic/test.csv')\nspace_tin_train.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-05T14:02:25.852739Z","iopub.execute_input":"2022-08-05T14:02:25.853189Z","iopub.status.idle":"2022-08-05T14:02:25.966814Z","shell.execute_reply.started":"2022-08-05T14:02:25.853155Z","shell.execute_reply":"2022-08-05T14:02:25.965517Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"space_tin_train.info()","metadata":{"execution":{"iopub.status.busy":"2022-08-05T14:02:30.735697Z","iopub.execute_input":"2022-08-05T14:02:30.736104Z","iopub.status.idle":"2022-08-05T14:02:30.769878Z","shell.execute_reply.started":"2022-08-05T14:02:30.736071Z","shell.execute_reply":"2022-08-05T14:02:30.768458Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"null_vals=pd.DataFrame(space_tin_train.isna().sum()) \nnull_vals","metadata":{"execution":{"iopub.status.busy":"2022-08-05T14:02:32.261027Z","iopub.execute_input":"2022-08-05T14:02:32.261834Z","iopub.status.idle":"2022-08-05T14:02:32.279240Z","shell.execute_reply.started":"2022-08-05T14:02:32.261779Z","shell.execute_reply":"2022-08-05T14:02:32.277687Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Filling the data based on some EDA done before and making some assumptions on the data","metadata":{}},{"cell_type":"code","source":"#REPLACING HOMEPLANET WITH EARTH\nspace_tin_train['HomePlanet']=space_tin_train['HomePlanet'].fillna('Earth')\n\n#REPLACING CRYOSLEEP WITH TRUE \nspace_tin_train['CryoSleep']=space_tin_train['CryoSleep'].fillna(True)\n\n#REPLACING CABINDECK WITH \"T/0/S\" WHERE T AND S ARE CABIN DECK AND CABIN LETTER(I DON'T REALLY KNOW WHAT TO \n# LABEL IT OTHERWISE) AND CABIN NO WITH 0\nspace_tin_train['Cabin']=space_tin_train['Cabin'].fillna('T/0/S')\nspace_tin_train['CabinDeck']=space_tin_train['Cabin'].apply(lambda x : x.split('/')[0])\nspace_tin_train[\"CabinNo\"]=space_tin_train[\"Cabin\"].apply(lambda x : int(x.split('/')[1]))\nspace_tin_train['CabinLetter']=space_tin_train['Cabin'].apply(lambda x : x.split('/')[2])\n\n#REPLACING DESTINATION WITH LEAST VALUE \"PSO J318.5-22\" BASED ON ASSUMPTIONS MADE THROUGH EDA\nspace_tin_train['Destination']=space_tin_train['Destination'].fillna(\"PSO J318.5-22\")\n\n#REPLACING AGE WITH MEDIAN AND ROOM SERVICE,FOODCOURT,SHOPPINGMALL,SPA,VRDECK WITH 0\nspace_tin_train['Age']=space_tin_train['Age'].fillna(space_tin_train['Age'].median())\nspace_tin_train[['RoomService','FoodCourt','ShoppingMall','Spa','VRDeck']]=space_tin_train[['RoomService','FoodCourt','ShoppingMall','Spa','VRDeck']].fillna(0)\n\n# DROP NAME,PASSENGERID\nspace_tin_train=space_tin_train.drop(['Name','Cabin'],axis=1)\nspace_tin_train.set_index('PassengerId',inplace=True)\nspace_tin_train=pd.get_dummies(space_tin_train,columns=['CryoSleep','VIP','CabinDeck','CabinLetter','HomePlanet','Destination'],drop_first=True)","metadata":{"execution":{"iopub.status.busy":"2022-08-05T14:25:33.042840Z","iopub.execute_input":"2022-08-05T14:25:33.043293Z","iopub.status.idle":"2022-08-05T14:25:33.111954Z","shell.execute_reply.started":"2022-08-05T14:25:33.043243Z","shell.execute_reply":"2022-08-05T14:25:33.110464Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## PERFROMING THE SAME EDA ON TEST AS DONE ON THE TRAIN","metadata":{}},{"cell_type":"code","source":"#REPLACING HOMEPLANET WITH EARTH\nspace_tin_test['HomePlanet']=space_tin_test['HomePlanet'].fillna('Earth')\n\n#REPLACING CRYOSLEEP WITH TRUE\nspace_tin_test['CryoSleep']=space_tin_test['CryoSleep'].fillna(True)\n\n#REPLACING CABINDECK WITH \"T/0/S\" WHERE T AND S ARE CABIN DECK AND CABIN LETTER(I DON'T REALLY KNOW WHAT TO \n# LABEL IT OTHERWISE) AND CABIN NO WITH 0\nspace_tin_test['Cabin']=space_tin_test['Cabin'].fillna('T/0/S')\nspace_tin_test['CabinDeck']=space_tin_test['Cabin'].apply(lambda x : x.split('/')[0])\nspace_tin_test[\"CabinNo\"]=space_tin_test[\"Cabin\"].apply(lambda x : int(x.split('/')[1]))\nspace_tin_test['CabinLetter']=space_tin_test['Cabin'].apply(lambda x : x.split('/')[2])\n\n#REPLACING DESTINATION WITH LEAST VALUE \"PSO J318.5-22\" BASED ON ASSUMPTIONS MADE THROUGH EDA\nspace_tin_test['Destination']=space_tin_test['Destination'].fillna(\"PSO J318.5-22\")\n\n#REPLACING AGE WITH MEDIAN AND ROOM SERVICE,FOODCOURT,SHOPPINGMALL,SPA,VRDECK WITH 0\nspace_tin_test['Age']=space_tin_test['Age'].fillna(space_tin_test['Age'].median())\nspace_tin_test[['RoomService','FoodCourt','ShoppingMall','Spa','VRDeck']]=space_tin_test[['RoomService','FoodCourt','ShoppingMall','Spa','VRDeck']].fillna(0)\n\n#DROP NAME,PASSENGERID\nspace_tin_test=space_tin_test.drop(['Name','Cabin'],axis=1)\nspace_tin_test.set_index('PassengerId',inplace=True)\nspace_tin_test=pd.get_dummies(space_tin_test,columns=['CryoSleep','VIP','CabinDeck','CabinLetter','HomePlanet','Destination'],drop_first=True)","metadata":{"execution":{"iopub.status.busy":"2022-08-05T14:30:31.848691Z","iopub.execute_input":"2022-08-05T14:30:31.849220Z","iopub.status.idle":"2022-08-05T14:30:31.897838Z","shell.execute_reply.started":"2022-08-05T14:30:31.849184Z","shell.execute_reply":"2022-08-05T14:30:31.896839Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nX=space_tin_train.drop('Transported',axis=1)\ny=space_tin_train['Transported']\nX_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.15,stratify=y,random_state=101)","metadata":{"execution":{"iopub.status.busy":"2022-08-05T14:30:50.310945Z","iopub.execute_input":"2022-08-05T14:30:50.311411Z","iopub.status.idle":"2022-08-05T14:30:50.332232Z","shell.execute_reply.started":"2022-08-05T14:30:50.311375Z","shell.execute_reply":"2022-08-05T14:30:50.331179Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## A BUNCH OF CLASSIFIERS WERE TRIED ALONG WITH GRID SEARCH AND GRADIENT BOOST GAVE THE BEST RESULTS","metadata":{}},{"cell_type":"code","source":"#First Submission \n# xgc=XGBClassifier()\n# xgc.fit(X_train,y_train)\n\n# #Second Submission\n# logr=LogisticRegression()\n# logr.fit(X_train,y_train)\n\n#Third Submission\n# cat=CatBoostClassifier()\n# cat.fit(X,y)\n\n#Fourth Submission\n\ngb=GradientBoostingClassifier()\ngb.fit(X_train,y_train)\n","metadata":{"execution":{"iopub.status.busy":"2022-08-05T14:33:13.731040Z","iopub.execute_input":"2022-08-05T14:33:13.731985Z","iopub.status.idle":"2022-08-05T14:33:14.806506Z","shell.execute_reply.started":"2022-08-05T14:33:13.731928Z","shell.execute_reply":"2022-08-05T14:33:14.805195Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import confusion_matrix\ny_pred=gb.predict(X_test)\nprint(confusion_matrix(y_test,y_pred))","metadata":{"execution":{"iopub.status.busy":"2022-08-05T14:33:16.552148Z","iopub.execute_input":"2022-08-05T14:33:16.553231Z","iopub.status.idle":"2022-08-05T14:33:16.566725Z","shell.execute_reply.started":"2022-08-05T14:33:16.553167Z","shell.execute_reply":"2022-08-05T14:33:16.565359Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gb.fit(X,y)\n\nfinal_res=gb.predict(space_tin_test)\n\nfinal_res=pd.DataFrame({'Transported':final_res},index=space_tin_test.index)\n\nfinal_res.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-05T14:34:26.294723Z","iopub.execute_input":"2022-08-05T14:34:26.295147Z","iopub.status.idle":"2022-08-05T14:34:27.537081Z","shell.execute_reply.started":"2022-08-05T14:34:26.295113Z","shell.execute_reply":"2022-08-05T14:34:27.535941Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_res.to_csv('submission_21')","metadata":{"execution":{"iopub.status.busy":"2022-08-03T08:59:49.463712Z","iopub.execute_input":"2022-08-03T08:59:49.464197Z","iopub.status.idle":"2022-08-03T08:59:49.479067Z","shell.execute_reply.started":"2022-08-03T08:59:49.464156Z","shell.execute_reply":"2022-08-03T08:59:49.477568Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}