{"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)\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-07-22T07:57:00.086781Z","iopub.execute_input":"2022-07-22T07:57:00.087511Z","iopub.status.idle":"2022-07-22T07:57:00.095870Z","shell.execute_reply.started":"2022-07-22T07:57:00.087470Z","shell.execute_reply":"2022-07-22T07:57:00.094899Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv(\"/kaggle/input/spaceship-titanic/train.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-07-22T07:57:02.918451Z","iopub.execute_input":"2022-07-22T07:57:02.919726Z","iopub.status.idle":"2022-07-22T07:57:02.970212Z","shell.execute_reply.started":"2022-07-22T07:57:02.919682Z","shell.execute_reply":"2022-07-22T07:57:02.969335Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-22T07:57:04.946784Z","iopub.execute_input":"2022-07-22T07:57:04.947167Z","iopub.status.idle":"2022-07-22T07:57:04.980749Z","shell.execute_reply.started":"2022-07-22T07:57:04.947136Z","shell.execute_reply":"2022-07-22T07:57:04.979812Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.describe","metadata":{"execution":{"iopub.status.busy":"2022-07-22T07:57:06.857455Z","iopub.execute_input":"2022-07-22T07:57:06.858158Z","iopub.status.idle":"2022-07-22T07:57:06.881046Z","shell.execute_reply.started":"2022-07-22T07:57:06.858119Z","shell.execute_reply":"2022-07-22T07:57:06.880031Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.corr()","metadata":{"execution":{"iopub.status.busy":"2022-07-22T07:57:09.552397Z","iopub.execute_input":"2022-07-22T07:57:09.553593Z","iopub.status.idle":"2022-07-22T07:57:09.575443Z","shell.execute_reply.started":"2022-07-22T07:57:09.553545Z","shell.execute_reply":"2022-07-22T07:57:09.574426Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport seaborn as sns\n\ntest = pd.read_csv('/kaggle/input/spaceship-titanic/test.csv')\nsub = pd.read_csv('/kaggle/input/spaceship-titanic/sample_submission.csv')\ntrain.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-22T07:57:11.863074Z","iopub.execute_input":"2022-07-22T07:57:11.863493Z","iopub.status.idle":"2022-07-22T07:57:11.909040Z","shell.execute_reply.started":"2022-07-22T07:57:11.863453Z","shell.execute_reply":"2022-07-22T07:57:11.907994Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['RoomService'].fillna(train['RoomService'].median(skipna=True), inplace=True)\ntrain['FoodCourt'].fillna(train['FoodCourt'].median(skipna=True), inplace=True)\ntrain['ShoppingMall'].fillna(train['ShoppingMall'].median(skipna=True), inplace=True)\ntrain['Spa'].fillna(train['Spa'].median(skipna=True), inplace=True)\ntrain['VRDeck'].fillna(train['VRDeck'].median(skipna=True), inplace=True)\ntrain['Age'].fillna(train['Age'].median(skipna=True), inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-22T07:57:17.720789Z","iopub.execute_input":"2022-07-22T07:57:17.721598Z","iopub.status.idle":"2022-07-22T07:57:17.741771Z","shell.execute_reply.started":"2022-07-22T07:57:17.721556Z","shell.execute_reply":"2022-07-22T07:57:17.740601Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train[\"HomePlanet\"].fillna(\"Earth\",inplace=True)\ntrain[\"CryoSleep\"].fillna(\"False\",inplace=True)\ntrain[\"CryoSleep\"]=train[\"CryoSleep\"].astype(bool)\ntrain[\"Destination\"].fillna(\"TRAPPIST-1e\",inplace=True)\ntrain[\"VIP\"].fillna(\"False\",inplace=True)\ntrain[\"VIP\"]=train[\"VIP\"].astype(bool)","metadata":{"execution":{"iopub.status.busy":"2022-07-22T07:57:19.758734Z","iopub.execute_input":"2022-07-22T07:57:19.759428Z","iopub.status.idle":"2022-07-22T07:57:19.774248Z","shell.execute_reply.started":"2022-07-22T07:57:19.759381Z","shell.execute_reply":"2022-07-22T07:57:19.773394Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-22T07:57:21.532629Z","iopub.execute_input":"2022-07-22T07:57:21.533335Z","iopub.status.idle":"2022-07-22T07:57:21.545476Z","shell.execute_reply.started":"2022-07-22T07:57:21.533274Z","shell.execute_reply":"2022-07-22T07:57:21.544358Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.corr()","metadata":{"execution":{"iopub.status.busy":"2022-07-22T07:57:23.678806Z","iopub.execute_input":"2022-07-22T07:57:23.679556Z","iopub.status.idle":"2022-07-22T07:57:23.700092Z","shell.execute_reply.started":"2022-07-22T07:57:23.679520Z","shell.execute_reply":"2022-07-22T07:57:23.699232Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train[\"OnboardService\"] = train[\"RoomService\"]+train[\"FoodCourt\"]+train[\"ShoppingMall\"]+train[\"Spa\"]+train[\"VRDeck\"]","metadata":{"execution":{"iopub.status.busy":"2022-07-22T07:57:36.751165Z","iopub.execute_input":"2022-07-22T07:57:36.751566Z","iopub.status.idle":"2022-07-22T07:57:36.759188Z","shell.execute_reply.started":"2022-07-22T07:57:36.751532Z","shell.execute_reply":"2022-07-22T07:57:36.758154Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.describe","metadata":{"execution":{"iopub.status.busy":"2022-07-22T07:57:38.416490Z","iopub.execute_input":"2022-07-22T07:57:38.417200Z","iopub.status.idle":"2022-07-22T07:57:38.437864Z","shell.execute_reply.started":"2022-07-22T07:57:38.417162Z","shell.execute_reply":"2022-07-22T07:57:38.436735Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.corr()","metadata":{"execution":{"iopub.status.busy":"2022-07-22T07:57:41.178467Z","iopub.execute_input":"2022-07-22T07:57:41.179629Z","iopub.status.idle":"2022-07-22T07:57:41.201099Z","shell.execute_reply.started":"2022-07-22T07:57:41.179585Z","shell.execute_reply":"2022-07-22T07:57:41.200010Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.dropna(how='any')","metadata":{"execution":{"iopub.status.busy":"2022-07-22T07:57:44.668782Z","iopub.execute_input":"2022-07-22T07:57:44.669906Z","iopub.status.idle":"2022-07-22T07:57:44.710972Z","shell.execute_reply.started":"2022-07-22T07:57:44.669770Z","shell.execute_reply":"2022-07-22T07:57:44.709590Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"category = ['CryoSleep','VIP','Destination','Transported']\ntraining = pd.get_dummies(train, columns=category,drop_first=True)\ntraining","metadata":{"execution":{"iopub.status.busy":"2022-07-22T07:57:50.926658Z","iopub.execute_input":"2022-07-22T07:57:50.927073Z","iopub.status.idle":"2022-07-22T07:57:50.969354Z","shell.execute_reply.started":"2022-07-22T07:57:50.927039Z","shell.execute_reply":"2022-07-22T07:57:50.968517Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"training.corr()","metadata":{"execution":{"iopub.status.busy":"2022-07-22T07:57:53.589434Z","iopub.execute_input":"2022-07-22T07:57:53.589841Z","iopub.status.idle":"2022-07-22T07:57:53.618486Z","shell.execute_reply.started":"2022-07-22T07:57:53.589806Z","shell.execute_reply":"2022-07-22T07:57:53.617367Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"training.drop(['Age','FoodCourt','ShoppingMall','Spa','OnboardService','VIP_True','Destination_PSO J318.5-22','Destination_TRAPPIST-1e'],axis=1,inplace=True)\ntraining.corr()","metadata":{"execution":{"iopub.status.busy":"2022-07-22T07:57:56.375363Z","iopub.execute_input":"2022-07-22T07:57:56.375945Z","iopub.status.idle":"2022-07-22T07:57:56.391255Z","shell.execute_reply.started":"2022-07-22T07:57:56.375903Z","shell.execute_reply":"2022-07-22T07:57:56.390366Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler\ntraining_standard = StandardScaler()\ntraining_standard.fit(training[['RoomService','VRDeck']])\ntraining_std = pd.DataFrame(training_standard.transform(training[['RoomService','VRDeck']]))\ntraining [['RoomService','VRDeck']] = training_std\ntraining","metadata":{"execution":{"iopub.status.busy":"2022-07-22T07:57:58.086252Z","iopub.execute_input":"2022-07-22T07:57:58.087261Z","iopub.status.idle":"2022-07-22T07:57:58.181680Z","shell.execute_reply.started":"2022-07-22T07:57:58.087224Z","shell.execute_reply":"2022-07-22T07:57:58.180513Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.linear_model import LogisticRegression\ncols = ['RoomService','VRDeck','CryoSleep_True'] \nX = training[cols]\ny = training['Transported_True']\n# Build a logreg and compute the feature importances\nmodel = LogisticRegression()\n# create the RFE model and select 8 attributes\nmodel.fit(X,y)","metadata":{"execution":{"iopub.status.busy":"2022-07-22T08:01:33.412573Z","iopub.execute_input":"2022-07-22T08:01:33.412955Z","iopub.status.idle":"2022-07-22T08:01:33.447521Z","shell.execute_reply.started":"2022-07-22T08:01:33.412923Z","shell.execute_reply":"2022-07-22T08:01:33.446337Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score\ntrain_predicted = model.predict(X)\naccuracy_score(train_predicted, y)","metadata":{"execution":{"iopub.status.busy":"2022-07-22T08:02:05.945355Z","iopub.execute_input":"2022-07-22T08:02:05.945734Z","iopub.status.idle":"2022-07-22T08:02:05.963134Z","shell.execute_reply.started":"2022-07-22T08:02:05.945703Z","shell.execute_reply":"2022-07-22T08:02:05.961627Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = pd.read_csv('/kaggle/input/spaceship-titanic/test.csv')\ntest.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-22T08:02:25.683450Z","iopub.execute_input":"2022-07-22T08:02:25.683829Z","iopub.status.idle":"2022-07-22T08:02:25.710745Z","shell.execute_reply.started":"2022-07-22T08:02:25.683784Z","shell.execute_reply":"2022-07-22T08:02:25.709921Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test","metadata":{"execution":{"iopub.status.busy":"2022-07-22T08:02:27.958136Z","iopub.execute_input":"2022-07-22T08:02:27.958616Z","iopub.status.idle":"2022-07-22T08:02:27.990023Z","shell.execute_reply.started":"2022-07-22T08:02:27.958578Z","shell.execute_reply":"2022-07-22T08:02:27.988843Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.drop(['PassengerId','HomePlanet','Cabin','Destination','Age','VIP','FoodCourt','ShoppingMall','Spa','Name'],axis=1,inplace=True)\ntest['RoomService'].fillna(train['RoomService'].median(skipna=True), inplace=True)\ntest['VRDeck'].fillna(train['VRDeck'].median(skipna=True), inplace=True)\ncategory = ['CryoSleep']\ntesting = pd.get_dummies(test, columns=category,drop_first=True)\ntesting.dropna(how='any')\ntest_copied = testing.copy()\ntest_std = training_standard.transform(test_copied[['RoomService','VRDeck']])\ntest_std\ntesting[['RoomService','VRDeck']] = test_std","metadata":{"execution":{"iopub.status.busy":"2022-07-22T08:02:30.641559Z","iopub.execute_input":"2022-07-22T08:02:30.642351Z","iopub.status.idle":"2022-07-22T08:02:30.659800Z","shell.execute_reply.started":"2022-07-22T08:02:30.642301Z","shell.execute_reply":"2022-07-22T08:02:30.658800Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cols = ['RoomService','VRDeck','CryoSleep_True'] \nX_test=testing[cols]\nprint(X_test.dtypes)\ntest_predicted = model.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2022-07-22T08:02:32.610748Z","iopub.execute_input":"2022-07-22T08:02:32.611571Z","iopub.status.idle":"2022-07-22T08:02:32.625150Z","shell.execute_reply.started":"2022-07-22T08:02:32.611522Z","shell.execute_reply":"2022-07-22T08:02:32.622579Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub = pd.read_csv('/kaggle/input/spaceship-titanic/sample_submission.csv')\nsub","metadata":{"execution":{"iopub.status.busy":"2022-07-22T08:02:59.111171Z","iopub.execute_input":"2022-07-22T08:02:59.111632Z","iopub.status.idle":"2022-07-22T08:02:59.133022Z","shell.execute_reply.started":"2022-07-22T08:02:59.111595Z","shell.execute_reply":"2022-07-22T08:02:59.132223Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub['Transported'] = list(map(bool, test_predicted))\nsub.to_csv('submission.csv', index=False)\nsub","metadata":{"execution":{"iopub.status.busy":"2022-07-22T08:03:26.181600Z","iopub.execute_input":"2022-07-22T08:03:26.182117Z","iopub.status.idle":"2022-07-22T08:03:26.208536Z","shell.execute_reply.started":"2022-07-22T08:03:26.182072Z","shell.execute_reply":"2022-07-22T08:03:26.207394Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}