{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":34377,"databundleVersionId":3220602,"sourceType":"competition"}],"dockerImageVersionId":30648,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"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)\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":"2024-02-03T10:16:53.819685Z","iopub.execute_input":"2024-02-03T10:16:53.820115Z","iopub.status.idle":"2024-02-03T10:16:53.829040Z","shell.execute_reply.started":"2024-02-03T10:16:53.820082Z","shell.execute_reply":"2024-02-03T10:16:53.827813Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train=pd.read_csv(\"/kaggle/input/spaceship-titanic/train.csv\")\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2024-02-03T10:16:53.830956Z","iopub.execute_input":"2024-02-03T10:16:53.831295Z","iopub.status.idle":"2024-02-03T10:16:53.882139Z","shell.execute_reply.started":"2024-02-03T10:16:53.831265Z","shell.execute_reply":"2024-02-03T10:16:53.880989Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test=pd.read_csv(\"/kaggle/input/spaceship-titanic/test.csv\")\ntest.head()","metadata":{"execution":{"iopub.status.busy":"2024-02-03T10:16:53.883932Z","iopub.execute_input":"2024-02-03T10:16:53.884253Z","iopub.status.idle":"2024-02-03T10:16:53.920716Z","shell.execute_reply.started":"2024-02-03T10:16:53.884226Z","shell.execute_reply":"2024-02-03T10:16:53.919512Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.interpolate('linear',limit_direction='forward',inplace=True)\ntrain.fillna(method='bfill',inplace=True)\ntrain.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2024-02-03T10:16:53.922160Z","iopub.execute_input":"2024-02-03T10:16:53.922542Z","iopub.status.idle":"2024-02-03T10:16:53.959048Z","shell.execute_reply.started":"2024-02-03T10:16:53.922472Z","shell.execute_reply":"2024-02-03T10:16:53.958085Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train[['DeckName','DeckNumber','DeckSide']]=train['Cabin'].str.split('/',expand=True)\ntrain[['Group','ID']]=train['PassengerId'].str.split('_',expand=True)\ntrain['DeckName']=train['DeckName'].map({'B':1,'F':2,'A':3,'G':4,'E':5,'D':6,'C':7,'T':8}).astype('int8')\ntrain['DeckSide']=train['DeckSide'].map({'P':1,'S':2}).astype('int8')\ntrain['HomePlanet']=train['HomePlanet'].map({'Europa':1,'Earth':2,'Mars':3}).astype('int8')\ntrain['Destination']=train['Destination'].map({'TRAPPIST-1e':1,'PSO J318.5-22':2,'55 Cancri e':3}).astype('int8')\ntrain","metadata":{"execution":{"iopub.status.busy":"2024-02-03T10:16:53.961300Z","iopub.execute_input":"2024-02-03T10:16:53.961697Z","iopub.status.idle":"2024-02-03T10:16:54.038675Z","shell.execute_reply.started":"2024-02-03T10:16:53.961660Z","shell.execute_reply":"2024-02-03T10:16:54.037596Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train=train[['Group','HomePlanet','CryoSleep','Destination','Age','VIP','RoomService','FoodCourt','ShoppingMall','Spa','VRDeck','DeckName','DeckNumber','DeckSide']]\ny_train=train['Transported']","metadata":{"execution":{"iopub.status.busy":"2024-02-03T10:17:11.749922Z","iopub.execute_input":"2024-02-03T10:17:11.750335Z","iopub.status.idle":"2024-02-03T10:17:11.759152Z","shell.execute_reply.started":"2024-02-03T10:17:11.750302Z","shell.execute_reply":"2024-02-03T10:17:11.758008Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nX_train,X_test,Y_train,Y_test=train_test_split(x_train,y_train,test_size=0.33,random_state=42)","metadata":{"execution":{"iopub.status.busy":"2024-02-03T10:17:22.408770Z","iopub.execute_input":"2024-02-03T10:17:22.409196Z","iopub.status.idle":"2024-02-03T10:17:22.420414Z","shell.execute_reply.started":"2024-02-03T10:17:22.409163Z","shell.execute_reply":"2024-02-03T10:17:22.419155Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.ensemble import RandomForestClassifier\nrf=RandomForestClassifier(100,max_depth=25,criterion='gini',min_samples_leaf=1,max_features='auto')\nrf.fit(x_train,y_train)\nprint(\"score on test\",end='')\nprint(str(rf.score(X_test,Y_test)))\nprint(\"score on train\",end='')\nprint(str(rf.score(X_train,Y_train)))","metadata":{"execution":{"iopub.status.busy":"2024-02-03T10:17:37.094522Z","iopub.execute_input":"2024-02-03T10:17:37.094925Z","iopub.status.idle":"2024-02-03T10:17:38.749533Z","shell.execute_reply.started":"2024-02-03T10:17:37.094890Z","shell.execute_reply":"2024-02-03T10:17:38.748336Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.interpolate(method='linear',inplace=True)\ntest.fillna(method='bfill',inplace=True)","metadata":{"execution":{"iopub.status.busy":"2024-02-03T10:17:55.365799Z","iopub.execute_input":"2024-02-03T10:17:55.366158Z","iopub.status.idle":"2024-02-03T10:17:55.384179Z","shell.execute_reply.started":"2024-02-03T10:17:55.366131Z","shell.execute_reply":"2024-02-03T10:17:55.383153Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test[['DeckName','DeckNumber','DeckSide']]=test['Cabin'].str.split('/',expand=True)\ntest[['Group','ID']]=test['PassengerId'].str.split('_',expand=True)\ntest['DeckName']=test['DeckName'].map({'B':1,'F':2,'A':3,'G':4,'E':5,'D':6,'C':7,'T':8}).astype('int8')\ntest['DeckSide']=test['DeckSide'].map({'P':1,'S':2}).astype('int8')\ntest['HomePlanet']=test['HomePlanet'].map({'Europa':1,'Earth':2,'Mars':3}).astype('int8')\ntest['Destination']=test['Destination'].map({'TRAPPIST-1e':1,'PSO J318.5-22':2,'55 Cancri e':3}).astype('int8')\ntest['CryoSleep']=test['CryoSleep'].astype(int)\ntest['VIP']=test['VIP'].astype(int)","metadata":{"execution":{"iopub.status.busy":"2024-02-03T10:18:07.013150Z","iopub.execute_input":"2024-02-03T10:18:07.013584Z","iopub.status.idle":"2024-02-03T10:18:07.049649Z","shell.execute_reply.started":"2024-02-03T10:18:07.013551Z","shell.execute_reply":"2024-02-03T10:18:07.048431Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_test=test[['Group','HomePlanet','CryoSleep','Destination','Age','VIP','RoomService','FoodCourt','ShoppingMall','Spa','VRDeck','DeckName','DeckNumber','DeckSide']]\npsngr=test['PassengerId']\ny_test=rf.predict(x_test)\nfinal=pd.DataFrame(data=(psngr),columns=['PassengerId'])\nfinal.loc[:,'Transported']=y_test.astype('bool')\nfinal.head()","metadata":{"execution":{"iopub.status.busy":"2024-02-03T10:18:28.371234Z","iopub.execute_input":"2024-02-03T10:18:28.371671Z","iopub.status.idle":"2024-02-03T10:18:28.485605Z","shell.execute_reply.started":"2024-02-03T10:18:28.371636Z","shell.execute_reply":"2024-02-03T10:18:28.484583Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final.to_csv('submission',index=False)\nsub=pd.read_csv('./submission')\nsub.head()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}}]}