{"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\nimport matplotlib.pyplot as plt\nfrom sklearn.ensemble import RandomForestClassifier\nfrom sklearn.metrics import classification_report ,confusion_matrix\nfrom sklearn.model_selection import train_test_split\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-08T14:58:58.369431Z","iopub.execute_input":"2022-08-08T14:58:58.370716Z","iopub.status.idle":"2022-08-08T14:59:00.093299Z","shell.execute_reply.started":"2022-08-08T14:58:58.370576Z","shell.execute_reply":"2022-08-08T14:59:00.091993Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train = pd.read_csv('/kaggle/input/spaceship-titanic/train.csv')\ndf_test = pd.read_csv('/kaggle/input/spaceship-titanic/test.csv')","metadata":{"execution":{"iopub.status.busy":"2022-08-08T14:59:00.095877Z","iopub.execute_input":"2022-08-08T14:59:00.096495Z","iopub.status.idle":"2022-08-08T14:59:00.174491Z","shell.execute_reply.started":"2022-08-08T14:59:00.096459Z","shell.execute_reply":"2022-08-08T14:59:00.173200Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-08T14:59:00.177552Z","iopub.execute_input":"2022-08-08T14:59:00.178018Z","iopub.status.idle":"2022-08-08T14:59:00.218056Z","shell.execute_reply.started":"2022-08-08T14:59:00.177970Z","shell.execute_reply":"2022-08-08T14:59:00.216591Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.info()","metadata":{"execution":{"iopub.status.busy":"2022-08-08T14:59:23.866386Z","iopub.execute_input":"2022-08-08T14:59:23.866891Z","iopub.status.idle":"2022-08-08T14:59:23.904941Z","shell.execute_reply.started":"2022-08-08T14:59:23.866816Z","shell.execute_reply":"2022-08-08T14:59:23.904011Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test.info()","metadata":{"execution":{"iopub.status.busy":"2022-08-08T14:59:24.889925Z","iopub.execute_input":"2022-08-08T14:59:24.890865Z","iopub.status.idle":"2022-08-08T14:59:24.907060Z","shell.execute_reply.started":"2022-08-08T14:59:24.890814Z","shell.execute_reply":"2022-08-08T14:59:24.905343Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-08T14:59:25.867659Z","iopub.execute_input":"2022-08-08T14:59:25.868082Z","iopub.status.idle":"2022-08-08T14:59:25.882604Z","shell.execute_reply.started":"2022-08-08T14:59:25.868047Z","shell.execute_reply":"2022-08-08T14:59:25.881312Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-08T14:59:27.096368Z","iopub.execute_input":"2022-08-08T14:59:27.096800Z","iopub.status.idle":"2022-08-08T14:59:27.110218Z","shell.execute_reply.started":"2022-08-08T14:59:27.096763Z","shell.execute_reply":"2022-08-08T14:59:27.108932Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.duplicated().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-08T14:59:28.065891Z","iopub.execute_input":"2022-08-08T14:59:28.066319Z","iopub.status.idle":"2022-08-08T14:59:28.087962Z","shell.execute_reply.started":"2022-08-08T14:59:28.066284Z","shell.execute_reply":"2022-08-08T14:59:28.086753Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test.duplicated().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-08T14:59:29.002462Z","iopub.execute_input":"2022-08-08T14:59:29.003763Z","iopub.status.idle":"2022-08-08T14:59:29.018609Z","shell.execute_reply.started":"2022-08-08T14:59:29.003720Z","shell.execute_reply":"2022-08-08T14:59:29.017760Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(12,6))\nsns.heatmap(df_train.corr())","metadata":{"execution":{"iopub.status.busy":"2022-08-08T14:59:30.416493Z","iopub.execute_input":"2022-08-08T14:59:30.417121Z","iopub.status.idle":"2022-08-08T14:59:30.729777Z","shell.execute_reply.started":"2022-08-08T14:59:30.417086Z","shell.execute_reply":"2022-08-08T14:59:30.728678Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"df_train['RoomService'] = df_train['RoomService'].fillna(df_train['RoomService'].mean())\ndf_train['FoodCourt'] = df_train['FoodCourt'].fillna(df_train['FoodCourt'].mean())\ndf_train['ShoppingMall'] = df_train['ShoppingMall'].fillna(df_train['ShoppingMall'].mean())\ndf_train['Spa'] = df_train['Spa'].fillna(df_train['Spa'].mean())\ndf_train['VRDeck'] = df_train['VRDeck'].fillna(df_train['VRDeck'].mean())\n\ndf_test['RoomService'] = df_test['RoomService'].fillna(df_test['RoomService'].mean())\ndf_test['FoodCourt'] = df_test['FoodCourt'].fillna(df_test['FoodCourt'].mean())\ndf_test['ShoppingMall'] = df_test['ShoppingMall'].fillna(df_test['ShoppingMall'].mean())\ndf_test['Spa'] = df_test['Spa'].fillna(df_test['Spa'].mean())\ndf_test['VRDeck'] = df_test['VRDeck'].fillna(df_test['VRDeck'].mean())","metadata":{"execution":{"iopub.status.busy":"2022-08-08T14:59:33.218604Z","iopub.execute_input":"2022-08-08T14:59:33.219083Z","iopub.status.idle":"2022-08-08T14:59:33.235539Z","shell.execute_reply.started":"2022-08-08T14:59:33.219046Z","shell.execute_reply":"2022-08-08T14:59:33.234566Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train['TotalExpense'] = df_train['RoomService']+df_train['FoodCourt']+df_train[\"ShoppingMall\"]+df_train['Spa']+df_train['VRDeck']\ndf_test['TotalExpense'] = df_test['RoomService']+df_test['FoodCourt']+df_test[\"ShoppingMall\"]+df_test['Spa']+df_test['VRDeck']","metadata":{"execution":{"iopub.status.busy":"2022-08-08T14:59:35.408624Z","iopub.execute_input":"2022-08-08T14:59:35.409329Z","iopub.status.idle":"2022-08-08T14:59:35.420330Z","shell.execute_reply.started":"2022-08-08T14:59:35.409277Z","shell.execute_reply":"2022-08-08T14:59:35.419083Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.corr()","metadata":{"execution":{"iopub.status.busy":"2022-08-08T14:59:36.021145Z","iopub.execute_input":"2022-08-08T14:59:36.021810Z","iopub.status.idle":"2022-08-08T14:59:36.041943Z","shell.execute_reply.started":"2022-08-08T14:59:36.021756Z","shell.execute_reply":"2022-08-08T14:59:36.040681Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test['Age'] = df_test['Age'].fillna(df_test['Age'].mean())\ndf_train['Age'] = df_train['Age'].fillna(df_train['Age'].mean())","metadata":{"execution":{"iopub.status.busy":"2022-08-08T14:59:36.947035Z","iopub.execute_input":"2022-08-08T14:59:36.947827Z","iopub.status.idle":"2022-08-08T14:59:36.954645Z","shell.execute_reply.started":"2022-08-08T14:59:36.947788Z","shell.execute_reply":"2022-08-08T14:59:36.953677Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train['HomePlanet'] = df_train['HomePlanet'].fillna(df_train['HomePlanet'].mode()[0])\ndf_train['CryoSleep'] = df_train['CryoSleep'].fillna(df_train['CryoSleep'].mode()[0])\ndf_train['Destination'] = df_train['Destination'].fillna(df_train['Destination'].mode()[0])\ndf_train['VIP'] = df_train['VIP'].fillna(df_train['VIP'].mode()[0])\n\ndf_test['HomePlanet'] = df_test['HomePlanet'].fillna(df_test['HomePlanet'].mode()[0])\ndf_test['CryoSleep'] = df_test['CryoSleep'].fillna(df_test['CryoSleep'].mode()[0])\ndf_test['Destination'] = df_test['Destination'].fillna(df_test['Destination'].mode()[0])\ndf_test['VIP'] = df_test['VIP'].fillna(df_test['VIP'].mode()[0])","metadata":{"execution":{"iopub.status.busy":"2022-08-08T14:59:41.966043Z","iopub.execute_input":"2022-08-08T14:59:41.966463Z","iopub.status.idle":"2022-08-08T14:59:41.995170Z","shell.execute_reply.started":"2022-08-08T14:59:41.966421Z","shell.execute_reply":"2022-08-08T14:59:41.994193Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-08T14:59:53.812275Z","iopub.execute_input":"2022-08-08T14:59:53.813051Z","iopub.status.idle":"2022-08-08T14:59:53.837935Z","shell.execute_reply.started":"2022-08-08T14:59:53.813000Z","shell.execute_reply":"2022-08-08T14:59:53.836660Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test.drop(['Name','Cabin','PassengerId'],axis=1,inplace=True)\ndf_train.drop(['Name','Cabin','PassengerId'],axis=1,inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-08-08T15:00:29.804777Z","iopub.execute_input":"2022-08-08T15:00:29.805232Z","iopub.status.idle":"2022-08-08T15:00:29.816523Z","shell.execute_reply.started":"2022-08-08T15:00:29.805194Z","shell.execute_reply":"2022-08-08T15:00:29.815514Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dummy = pd.get_dummies(df_train[['HomePlanet','Destination']],drop_first=True)\ndf_train = pd.concat([df_train,dummy],axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-08-08T15:00:32.040146Z","iopub.execute_input":"2022-08-08T15:00:32.040619Z","iopub.status.idle":"2022-08-08T15:00:32.057186Z","shell.execute_reply.started":"2022-08-08T15:00:32.040583Z","shell.execute_reply":"2022-08-08T15:00:32.055935Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dummy1 = pd.get_dummies(df_test[['HomePlanet','Destination']],drop_first=True)\ndf_test = pd.concat([df_test,dummy1],axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-08-08T15:00:33.030249Z","iopub.execute_input":"2022-08-08T15:00:33.030916Z","iopub.status.idle":"2022-08-08T15:00:33.043655Z","shell.execute_reply.started":"2022-08-08T15:00:33.030879Z","shell.execute_reply":"2022-08-08T15:00:33.042580Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.drop(['HomePlanet','Destination'],axis=1,inplace=True)\ndf_test.drop(['HomePlanet','Destination'],axis=1,inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-08-08T15:00:34.539738Z","iopub.execute_input":"2022-08-08T15:00:34.540592Z","iopub.status.idle":"2022-08-08T15:00:34.550075Z","shell.execute_reply.started":"2022-08-08T15:00:34.540549Z","shell.execute_reply":"2022-08-08T15:00:34.548945Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"raw","source":"","metadata":{"execution":{"iopub.status.busy":"2022-08-08T14:29:09.968743Z","iopub.execute_input":"2022-08-08T14:29:09.969176Z","iopub.status.idle":"2022-08-08T14:29:09.975887Z","shell.execute_reply.started":"2022-08-08T14:29:09.969132Z","shell.execute_reply":"2022-08-08T14:29:09.974657Z"}}},{"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,y,test_size=0.3)","metadata":{"execution":{"iopub.status.busy":"2022-08-08T15:00:37.892011Z","iopub.execute_input":"2022-08-08T15:00:37.892850Z","iopub.status.idle":"2022-08-08T15:00:37.903572Z","shell.execute_reply.started":"2022-08-08T15:00:37.892783Z","shell.execute_reply":"2022-08-08T15:00:37.902454Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = RandomForestClassifier(n_estimators=300)\nmodel.fit(x_train,y_train)","metadata":{"execution":{"iopub.status.busy":"2022-08-08T15:00:39.074672Z","iopub.execute_input":"2022-08-08T15:00:39.075104Z","iopub.status.idle":"2022-08-08T15:00:41.604818Z","shell.execute_reply.started":"2022-08-08T15:00:39.075068Z","shell.execute_reply":"2022-08-08T15:00:41.603611Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prediction = model.predict(x_test)","metadata":{"execution":{"iopub.status.busy":"2022-08-08T15:00:41.606792Z","iopub.execute_input":"2022-08-08T15:00:41.607500Z","iopub.status.idle":"2022-08-08T15:00:41.826898Z","shell.execute_reply.started":"2022-08-08T15:00:41.607455Z","shell.execute_reply":"2022-08-08T15:00:41.825576Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_test_unknown = df_test\ny_pred = model.predict(x_test_unknown)","metadata":{"execution":{"iopub.status.busy":"2022-08-08T15:00:43.639934Z","iopub.execute_input":"2022-08-08T15:00:43.640321Z","iopub.status.idle":"2022-08-08T15:00:43.952453Z","shell.execute_reply.started":"2022-08-08T15:00:43.640290Z","shell.execute_reply":"2022-08-08T15:00:43.951317Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"originaltest_data = pd.read_csv('/kaggle/input/spaceship-titanic/test.csv')\nfinal = pd.DataFrame({\n        \"PassengerId\": originaltest_data[\"PassengerId\"],\n        \"Transported\": y_pred\n    })","metadata":{"execution":{"iopub.status.busy":"2022-08-08T15:00:45.221687Z","iopub.execute_input":"2022-08-08T15:00:45.222090Z","iopub.status.idle":"2022-08-08T15:00:45.246663Z","shell.execute_reply.started":"2022-08-08T15:00:45.222059Z","shell.execute_reply":"2022-08-08T15:00:45.245251Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final.to_csv('submission.csv' , index=False)\nprint('Submitted')","metadata":{"execution":{"iopub.status.busy":"2022-08-08T15:00:46.669610Z","iopub.execute_input":"2022-08-08T15:00:46.670095Z","iopub.status.idle":"2022-08-08T15:00:46.685103Z","shell.execute_reply.started":"2022-08-08T15:00:46.670035Z","shell.execute_reply":"2022-08-08T15:00:46.684053Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}