{"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-08-11T11:47:23.160311Z","iopub.execute_input":"2022-08-11T11:47:23.161261Z","iopub.status.idle":"2022-08-11T11:47:23.198085Z","shell.execute_reply.started":"2022-08-11T11:47:23.161146Z","shell.execute_reply":"2022-08-11T11:47:23.196751Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\ntrain=pd.read_csv(\"/kaggle/input/spaceship-titanic/train.csv\")\ntest=pd.read_csv(\"/kaggle/input/spaceship-titanic/test.csv\")\nsubmission=pd.read_csv(\"/kaggle/input/spaceship-titanic/sample_submission.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-08-11T11:47:23.200772Z","iopub.execute_input":"2022-08-11T11:47:23.201574Z","iopub.status.idle":"2022-08-11T11:47:23.317428Z","shell.execute_reply.started":"2022-08-11T11:47:23.201527Z","shell.execute_reply":"2022-08-11T11:47:23.316102Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-11T11:47:23.321713Z","iopub.execute_input":"2022-08-11T11:47:23.322867Z","iopub.status.idle":"2022-08-11T11:47:23.361329Z","shell.execute_reply.started":"2022-08-11T11:47:23.322819Z","shell.execute_reply":"2022-08-11T11:47:23.360440Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-11T11:47:23.363327Z","iopub.execute_input":"2022-08-11T11:47:23.363884Z","iopub.status.idle":"2022-08-11T11:47:23.383731Z","shell.execute_reply.started":"2022-08-11T11:47:23.363851Z","shell.execute_reply":"2022-08-11T11:47:23.382848Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission","metadata":{"execution":{"iopub.status.busy":"2022-08-11T11:47:23.385308Z","iopub.execute_input":"2022-08-11T11:47:23.385972Z","iopub.status.idle":"2022-08-11T11:47:23.407838Z","shell.execute_reply.started":"2022-08-11T11:47:23.385938Z","shell.execute_reply":"2022-08-11T11:47:23.406610Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Preprocess data","metadata":{}},{"cell_type":"code","source":"train.corr()","metadata":{"execution":{"iopub.status.busy":"2022-08-11T11:47:23.409843Z","iopub.execute_input":"2022-08-11T11:47:23.410596Z","iopub.status.idle":"2022-08-11T11:47:23.433066Z","shell.execute_reply.started":"2022-08-11T11:47:23.410549Z","shell.execute_reply":"2022-08-11T11:47:23.432057Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.columns ","metadata":{"execution":{"iopub.status.busy":"2022-08-11T11:47:23.434299Z","iopub.execute_input":"2022-08-11T11:47:23.434842Z","iopub.status.idle":"2022-08-11T11:47:23.441147Z","shell.execute_reply.started":"2022-08-11T11:47:23.434801Z","shell.execute_reply":"2022-08-11T11:47:23.440067Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Passenger id is an id so we should keep it <br>\nHome Planet can be a deciding factor so we can convert it to numerical <br>\nCryosleep too determines whether transportation was done or not- convert to numerical <br>\nCabin lets see <br>\nDestination - lets see <br>\nAge - lets see <br>\nVIP -lets see <br>\nAll expenses can be combined into a single feature - expense (RoomService\tFoodCourt\tShoppingMall\tSpa\tVRDeck) <br>\nName is irrelevant - remove it <br>\nTransported - label to be predicted\n","metadata":{}},{"cell_type":"markdown","source":"# Home Planet","metadata":{}},{"cell_type":"code","source":"train[\"HomePlanet\"].unique()","metadata":{"execution":{"iopub.status.busy":"2022-08-11T11:47:23.442482Z","iopub.execute_input":"2022-08-11T11:47:23.443233Z","iopub.status.idle":"2022-08-11T11:47:23.460366Z","shell.execute_reply.started":"2022-08-11T11:47:23.443201Z","shell.execute_reply":"2022-08-11T11:47:23.459309Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"onehot_HomePlanet=pd.get_dummies(train, columns = [\"HomePlanet\"])","metadata":{"execution":{"iopub.status.busy":"2022-08-11T11:47:23.463961Z","iopub.execute_input":"2022-08-11T11:47:23.464319Z","iopub.status.idle":"2022-08-11T11:47:23.481574Z","shell.execute_reply.started":"2022-08-11T11:47:23.464289Z","shell.execute_reply":"2022-08-11T11:47:23.480351Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"onehot_HomePlanet","metadata":{"execution":{"iopub.status.busy":"2022-08-11T11:47:23.482901Z","iopub.execute_input":"2022-08-11T11:47:23.483410Z","iopub.status.idle":"2022-08-11T11:47:23.512803Z","shell.execute_reply.started":"2022-08-11T11:47:23.483380Z","shell.execute_reply":"2022-08-11T11:47:23.511850Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train","metadata":{"execution":{"iopub.status.busy":"2022-08-11T11:47:23.513854Z","iopub.execute_input":"2022-08-11T11:47:23.514608Z","iopub.status.idle":"2022-08-11T11:47:23.546098Z","shell.execute_reply.started":"2022-08-11T11:47:23.514577Z","shell.execute_reply":"2022-08-11T11:47:23.545099Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Cryosleep","metadata":{}},{"cell_type":"code","source":"train[\"CryoSleep\"].unique()","metadata":{"execution":{"iopub.status.busy":"2022-08-11T11:47:23.547415Z","iopub.execute_input":"2022-08-11T11:47:23.548466Z","iopub.status.idle":"2022-08-11T11:47:23.556939Z","shell.execute_reply.started":"2022-08-11T11:47:23.548433Z","shell.execute_reply":"2022-08-11T11:47:23.555913Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Lets label True as 1 and False as 0; ignore nan and assume False ie 0","metadata":{}},{"cell_type":"code","source":"def fill_cryo(row):\n    if row[\"CryoSleep\"]==True:\n        return 1\n    else:\n        return 0","metadata":{"execution":{"iopub.status.busy":"2022-08-11T11:47:23.558521Z","iopub.execute_input":"2022-08-11T11:47:23.559407Z","iopub.status.idle":"2022-08-11T11:47:23.566189Z","shell.execute_reply.started":"2022-08-11T11:47:23.559354Z","shell.execute_reply":"2022-08-11T11:47:23.564826Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_new=onehot_HomePlanet\n","metadata":{"execution":{"iopub.status.busy":"2022-08-11T11:47:23.568605Z","iopub.execute_input":"2022-08-11T11:47:23.569285Z","iopub.status.idle":"2022-08-11T11:47:23.579701Z","shell.execute_reply.started":"2022-08-11T11:47:23.569241Z","shell.execute_reply":"2022-08-11T11:47:23.578744Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_new[\"Cryo\"]=train_new.apply(lambda row: fill_cryo(row), axis=1)\ntrain_new","metadata":{"execution":{"iopub.status.busy":"2022-08-11T11:47:23.581385Z","iopub.execute_input":"2022-08-11T11:47:23.582013Z","iopub.status.idle":"2022-08-11T11:47:23.721441Z","shell.execute_reply.started":"2022-08-11T11:47:23.581979Z","shell.execute_reply":"2022-08-11T11:47:23.720308Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_new =train_new.drop(\"CryoSleep\", axis =1)","metadata":{"execution":{"iopub.status.busy":"2022-08-11T11:47:23.723146Z","iopub.execute_input":"2022-08-11T11:47:23.723531Z","iopub.status.idle":"2022-08-11T11:47:23.731569Z","shell.execute_reply.started":"2022-08-11T11:47:23.723497Z","shell.execute_reply":"2022-08-11T11:47:23.730275Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_new","metadata":{"execution":{"iopub.status.busy":"2022-08-11T11:47:23.732953Z","iopub.execute_input":"2022-08-11T11:47:23.733543Z","iopub.status.idle":"2022-08-11T11:47:23.771912Z","shell.execute_reply.started":"2022-08-11T11:47:23.733511Z","shell.execute_reply":"2022-08-11T11:47:23.770867Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Cabin","metadata":{}},{"cell_type":"code","source":"print(train[\"Cabin\"].unique())\nprint(train[\"Cabin\"].value_counts())","metadata":{"execution":{"iopub.status.busy":"2022-08-11T11:47:23.773194Z","iopub.execute_input":"2022-08-11T11:47:23.773707Z","iopub.status.idle":"2022-08-11T11:47:23.788182Z","shell.execute_reply.started":"2022-08-11T11:47:23.773677Z","shell.execute_reply":"2022-08-11T11:47:23.787003Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Break the cabin into 3 parts: deck / num/ side","metadata":{}},{"cell_type":"code","source":"train_cabin=pd.DataFrame().assign(Cabin=train['Cabin'], Transported=train['Transported'])\ntrain_cabin\n","metadata":{"execution":{"iopub.status.busy":"2022-08-11T11:47:23.789534Z","iopub.execute_input":"2022-08-11T11:47:23.790083Z","iopub.status.idle":"2022-08-11T11:47:23.807369Z","shell.execute_reply.started":"2022-08-11T11:47:23.790051Z","shell.execute_reply":"2022-08-11T11:47:23.806088Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_cabin[['deck', 'num', 'side']] = train_cabin['Cabin'].str.split(\"/\",expand=True)\ntrain_cabin","metadata":{"execution":{"iopub.status.busy":"2022-08-11T11:47:23.809269Z","iopub.execute_input":"2022-08-11T11:47:23.810057Z","iopub.status.idle":"2022-08-11T11:47:23.845990Z","shell.execute_reply.started":"2022-08-11T11:47:23.810013Z","shell.execute_reply":"2022-08-11T11:47:23.845011Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_cabin.describe()","metadata":{"execution":{"iopub.status.busy":"2022-08-11T11:47:23.847235Z","iopub.execute_input":"2022-08-11T11:47:23.847743Z","iopub.status.idle":"2022-08-11T11:47:23.877684Z","shell.execute_reply.started":"2022-08-11T11:47:23.847712Z","shell.execute_reply":"2022-08-11T11:47:23.876841Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#replace nan with top values from df.describe()\nimport numpy as np\ntrain_cabin['deck'] = train_cabin['deck'].replace(np.nan, \"F\")\ntrain_cabin['num'] = train_cabin['num'].replace(np.nan, \"82\")\ntrain_cabin['side'] = train_cabin['side'].replace(np.nan, \"S\")\ntrain_cabin","metadata":{"execution":{"iopub.status.busy":"2022-08-11T11:47:23.878968Z","iopub.execute_input":"2022-08-11T11:47:23.879456Z","iopub.status.idle":"2022-08-11T11:47:23.901097Z","shell.execute_reply.started":"2022-08-11T11:47:23.879426Z","shell.execute_reply":"2022-08-11T11:47:23.900097Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn import preprocessing\n  \n# label_encoder object knows how to understand word labels.\nlabel_encoder = preprocessing.LabelEncoder()\n\n# conversion functions to numerical categories\ndef conv_num(row):\n    return int(row[\"num\"])\ndef conv_side(row):\n    if row[\"side\"]==\"S\":\n        return 0\n    else:\n        return 1\ntrain_cabin['deck']= label_encoder.fit_transform(train_cabin['deck'])\ntrain_cabin[\"num\"]=train_cabin.apply(lambda row: conv_num(row), axis=1)\ntrain_cabin[\"side\"]=train_cabin.apply(lambda row: conv_side(row), axis=1)\ntrain_cabin\n\n","metadata":{"execution":{"iopub.status.busy":"2022-08-11T11:47:23.907350Z","iopub.execute_input":"2022-08-11T11:47:23.907927Z","iopub.status.idle":"2022-08-11T11:47:25.344726Z","shell.execute_reply.started":"2022-08-11T11:47:23.907892Z","shell.execute_reply":"2022-08-11T11:47:25.343570Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_cabin=train_cabin.drop(\"Cabin\", axis=1)\ntrain_cabin","metadata":{"execution":{"iopub.status.busy":"2022-08-11T11:47:25.346196Z","iopub.execute_input":"2022-08-11T11:47:25.346568Z","iopub.status.idle":"2022-08-11T11:47:25.363498Z","shell.execute_reply.started":"2022-08-11T11:47:25.346538Z","shell.execute_reply":"2022-08-11T11:47:25.362341Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_cabin.corr()","metadata":{"execution":{"iopub.status.busy":"2022-08-11T11:47:25.364970Z","iopub.execute_input":"2022-08-11T11:47:25.365462Z","iopub.status.idle":"2022-08-11T11:47:25.379478Z","shell.execute_reply.started":"2022-08-11T11:47:25.365431Z","shell.execute_reply":"2022-08-11T11:47:25.377970Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"When testing we can try dropping each of these columns to try if it matters at all","metadata":{}},{"cell_type":"code","source":"train_new","metadata":{"execution":{"iopub.status.busy":"2022-08-11T11:47:25.380957Z","iopub.execute_input":"2022-08-11T11:47:25.381531Z","iopub.status.idle":"2022-08-11T11:47:25.414613Z","shell.execute_reply.started":"2022-08-11T11:47:25.381489Z","shell.execute_reply":"2022-08-11T11:47:25.413587Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_new=train_new.drop(\"Cabin\", axis=1)\ntrain_new[\"deck\"]=train_cabin[\"deck\"]\ntrain_new[\"num\"]=train_cabin[\"num\"]\ntrain_new[\"side\"]=train_cabin[\"side\"]\ntrain_new","metadata":{"execution":{"iopub.status.busy":"2022-08-11T11:47:25.416008Z","iopub.execute_input":"2022-08-11T11:47:25.416386Z","iopub.status.idle":"2022-08-11T11:47:25.457460Z","shell.execute_reply.started":"2022-08-11T11:47:25.416352Z","shell.execute_reply":"2022-08-11T11:47:25.455083Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Destination","metadata":{}},{"cell_type":"code","source":"train_new[\"Destination\"].unique()","metadata":{"execution":{"iopub.status.busy":"2022-08-11T11:47:25.459435Z","iopub.execute_input":"2022-08-11T11:47:25.460285Z","iopub.status.idle":"2022-08-11T11:47:25.468116Z","shell.execute_reply.started":"2022-08-11T11:47:25.460231Z","shell.execute_reply":"2022-08-11T11:47:25.466773Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_new[\"Destination\"].describe()","metadata":{"execution":{"iopub.status.busy":"2022-08-11T11:47:25.469252Z","iopub.execute_input":"2022-08-11T11:47:25.470062Z","iopub.status.idle":"2022-08-11T11:47:25.485851Z","shell.execute_reply.started":"2022-08-11T11:47:25.470029Z","shell.execute_reply":"2022-08-11T11:47:25.484683Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Replace nan with top value\ntrain_new[\"Destination\"] = train_new[\"Destination\"].replace(np.nan, \"TRAPPIST-1e\")\n#Convert into numerical category\ntrain_new=pd.get_dummies(train_new, columns = [\"Destination\"])\n","metadata":{"execution":{"iopub.status.busy":"2022-08-11T11:47:25.487229Z","iopub.execute_input":"2022-08-11T11:47:25.488021Z","iopub.status.idle":"2022-08-11T11:47:25.503509Z","shell.execute_reply.started":"2022-08-11T11:47:25.487989Z","shell.execute_reply":"2022-08-11T11:47:25.502557Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_new","metadata":{"execution":{"iopub.status.busy":"2022-08-11T11:47:25.504816Z","iopub.execute_input":"2022-08-11T11:47:25.505319Z","iopub.status.idle":"2022-08-11T11:47:25.536749Z","shell.execute_reply.started":"2022-08-11T11:47:25.505290Z","shell.execute_reply":"2022-08-11T11:47:25.535838Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Age","metadata":{}},{"cell_type":"code","source":"train.corr()","metadata":{"execution":{"iopub.status.busy":"2022-08-11T11:47:25.537952Z","iopub.execute_input":"2022-08-11T11:47:25.538436Z","iopub.status.idle":"2022-08-11T11:47:25.556293Z","shell.execute_reply.started":"2022-08-11T11:47:25.538407Z","shell.execute_reply":"2022-08-11T11:47:25.555249Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Age is not correlated so let it be","metadata":{}},{"cell_type":"markdown","source":"# VIP","metadata":{}},{"cell_type":"code","source":"def fill_VIP(row):\n    if row[\"VIP\"]==True:\n        return 1\n    else:\n        return 0","metadata":{"execution":{"iopub.status.busy":"2022-08-11T11:47:25.557795Z","iopub.execute_input":"2022-08-11T11:47:25.558328Z","iopub.status.idle":"2022-08-11T11:47:25.562746Z","shell.execute_reply.started":"2022-08-11T11:47:25.558297Z","shell.execute_reply":"2022-08-11T11:47:25.561841Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_new[\"VIP\"]=train_new.apply(lambda row: fill_VIP(row), axis=1)\ntrain_new","metadata":{"execution":{"iopub.status.busy":"2022-08-11T11:47:25.564327Z","iopub.execute_input":"2022-08-11T11:47:25.565404Z","iopub.status.idle":"2022-08-11T11:47:25.706419Z","shell.execute_reply.started":"2022-08-11T11:47:25.565349Z","shell.execute_reply":"2022-08-11T11:47:25.705096Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Expenses","metadata":{}},{"cell_type":"code","source":"def fill_expense(row):\n    return row[\"RoomService\"]+row[\"FoodCourt\"]+ row[\"ShoppingMall\"]+ row[\"Spa\"]+row[\"VRDeck\"]","metadata":{"execution":{"iopub.status.busy":"2022-08-11T11:47:25.707921Z","iopub.execute_input":"2022-08-11T11:47:25.709005Z","iopub.status.idle":"2022-08-11T11:47:25.714955Z","shell.execute_reply.started":"2022-08-11T11:47:25.708967Z","shell.execute_reply":"2022-08-11T11:47:25.713171Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_new[\"Exp\"]=train_new.apply(lambda row: fill_expense(row), axis=1)\ntrain_new =train_new.drop(\"RoomService\", axis =1)\ntrain_new =train_new.drop(\"FoodCourt\", axis =1)\ntrain_new =train_new.drop(\"ShoppingMall\", axis =1)\ntrain_new =train_new.drop(\"Spa\", axis =1)\ntrain_new =train_new.drop(\"VRDeck\", axis =1)\n\nY= train_new[\"Transported\"]\nX=train_new.drop(\"Transported\", axis =1)\nX=X.drop(\"PassengerId\", axis =1)\nX=X.drop(\"Name\", axis =1)","metadata":{"execution":{"iopub.status.busy":"2022-08-11T11:47:25.716429Z","iopub.execute_input":"2022-08-11T11:47:25.716829Z","iopub.status.idle":"2022-08-11T11:47:25.987849Z","shell.execute_reply.started":"2022-08-11T11:47:25.716793Z","shell.execute_reply":"2022-08-11T11:47:25.986840Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X","metadata":{"execution":{"iopub.status.busy":"2022-08-11T11:48:13.918482Z","iopub.execute_input":"2022-08-11T11:48:13.918887Z","iopub.status.idle":"2022-08-11T11:48:13.943845Z","shell.execute_reply.started":"2022-08-11T11:48:13.918855Z","shell.execute_reply":"2022-08-11T11:48:13.942713Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y","metadata":{"execution":{"iopub.status.busy":"2022-08-11T11:48:18.429857Z","iopub.execute_input":"2022-08-11T11:48:18.430245Z","iopub.status.idle":"2022-08-11T11:48:18.440638Z","shell.execute_reply.started":"2022-08-11T11:48:18.430215Z","shell.execute_reply":"2022-08-11T11:48:18.439413Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Training","metadata":{}},{"cell_type":"code","source":"import numpy as np\nfrom sklearn.impute import SimpleImputer\nimp = SimpleImputer(missing_values=np.nan, strategy='most_frequent')\nX_1=imp.fit_transform(X)","metadata":{"execution":{"iopub.status.busy":"2022-08-11T11:48:30.292651Z","iopub.execute_input":"2022-08-11T11:48:30.293691Z","iopub.status.idle":"2022-08-11T11:48:30.314869Z","shell.execute_reply.started":"2022-08-11T11:48:30.293644Z","shell.execute_reply":"2022-08-11T11:48:30.313853Z"},"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_1,Y, test_size=0.25, random_state = 0)","metadata":{"execution":{"iopub.status.busy":"2022-08-11T11:48:37.611084Z","iopub.execute_input":"2022-08-11T11:48:37.611489Z","iopub.status.idle":"2022-08-11T11:48:37.621399Z","shell.execute_reply.started":"2022-08-11T11:48:37.611457Z","shell.execute_reply":"2022-08-11T11:48:37.620372Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.svm import SVC\nfrom sklearn.metrics import accuracy_score\n\nmodel=SVC(C=1000, kernel=\"rbf\")\nmodel.fit(X_train, Y_train)\nY_pred=model.predict(X_test)\nacc=accuracy_score(Y_test, Y_pred)\nprint(acc)","metadata":{"execution":{"iopub.status.busy":"2022-08-11T11:49:48.477985Z","iopub.execute_input":"2022-08-11T11:49:48.478525Z","iopub.status.idle":"2022-08-11T11:50:08.437378Z","shell.execute_reply.started":"2022-08-11T11:49:48.478478Z","shell.execute_reply":"2022-08-11T11:50:08.436109Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.svm import LinearSVC\nfrom sklearn.metrics import accuracy_score\n\nmodel=LinearSVC(C=1000)\nmodel.fit(X_train, Y_train)\nY_pred=model.predict(X_test)\nacc=accuracy_score(Y_test, Y_pred)\nprint(acc)","metadata":{"execution":{"iopub.status.busy":"2022-08-11T11:50:11.384956Z","iopub.execute_input":"2022-08-11T11:50:11.385343Z","iopub.status.idle":"2022-08-11T11:50:11.855157Z","shell.execute_reply.started":"2022-08-11T11:50:11.385314Z","shell.execute_reply":"2022-08-11T11:50:11.853223Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.naive_bayes import GaussianNB\nfrom sklearn.metrics import accuracy_score\n\nmodel=GaussianNB()\nmodel.fit(X_train, Y_train)\nY_pred=model.predict(X_test)\nacc=accuracy_score(Y_test, Y_pred)\nprint(acc)","metadata":{"execution":{"iopub.status.busy":"2022-08-11T11:50:13.653015Z","iopub.execute_input":"2022-08-11T11:50:13.653412Z","iopub.status.idle":"2022-08-11T11:50:13.670411Z","shell.execute_reply.started":"2022-08-11T11:50:13.653382Z","shell.execute_reply":"2022-08-11T11:50:13.669042Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.neighbors import KNeighborsClassifier\nmodel = KNeighborsClassifier(n_neighbors=1000)\nmodel.fit(X_train, Y_train)\nY_pred=model.predict(X_test)\nacc=accuracy_score(Y_test, Y_pred)\nprint(acc)","metadata":{"execution":{"iopub.status.busy":"2022-08-11T11:50:15.024869Z","iopub.execute_input":"2022-08-11T11:50:15.025291Z","iopub.status.idle":"2022-08-11T11:50:15.594744Z","shell.execute_reply.started":"2022-08-11T11:50:15.025255Z","shell.execute_reply":"2022-08-11T11:50:15.593565Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Drop age and expenses","metadata":{}},{"cell_type":"code","source":"X_2=X.drop(\"Exp\", axis=1)\nimport numpy as np\nfrom sklearn.impute import SimpleImputer\nimp = SimpleImputer(missing_values=np.nan, strategy='most_frequent')\nX_2=imp.fit_transform(X_2)","metadata":{"execution":{"iopub.status.busy":"2022-08-11T11:50:31.671810Z","iopub.execute_input":"2022-08-11T11:50:31.672191Z","iopub.status.idle":"2022-08-11T11:50:31.692635Z","shell.execute_reply.started":"2022-08-11T11:50:31.672161Z","shell.execute_reply":"2022-08-11T11:50:31.691490Z"},"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_2,Y, test_size=0.25, random_state = 0)","metadata":{"execution":{"iopub.status.busy":"2022-08-11T11:50:32.602329Z","iopub.execute_input":"2022-08-11T11:50:32.602746Z","iopub.status.idle":"2022-08-11T11:50:32.611624Z","shell.execute_reply.started":"2022-08-11T11:50:32.602711Z","shell.execute_reply":"2022-08-11T11:50:32.610128Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.naive_bayes import GaussianNB\nfrom sklearn.metrics import accuracy_score\n\nmodel2=GaussianNB()\nmodel2.fit(X_train, Y_train)\nY_pred=model2.predict(X_test)\nacc=accuracy_score(Y_test, Y_pred)\nprint(acc)","metadata":{"execution":{"iopub.status.busy":"2022-08-11T11:50:33.922270Z","iopub.execute_input":"2022-08-11T11:50:33.922662Z","iopub.status.idle":"2022-08-11T11:50:33.935034Z","shell.execute_reply.started":"2022-08-11T11:50:33.922632Z","shell.execute_reply":"2022-08-11T11:50:33.933607Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.neighbors import KNeighborsClassifier\nmodel2 = KNeighborsClassifier(n_neighbors=3)\nmodel2.fit(X_train, Y_train)\nY_pred=model2.predict(X_test)\nacc=accuracy_score(Y_test, Y_pred)\nprint(acc)","metadata":{"execution":{"iopub.status.busy":"2022-08-11T11:50:35.417877Z","iopub.execute_input":"2022-08-11T11:50:35.419181Z","iopub.status.idle":"2022-08-11T11:50:35.503044Z","shell.execute_reply.started":"2022-08-11T11:50:35.419138Z","shell.execute_reply":"2022-08-11T11:50:35.501862Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X.columns","metadata":{"execution":{"iopub.status.busy":"2022-08-11T11:50:37.710064Z","iopub.execute_input":"2022-08-11T11:50:37.711427Z","iopub.status.idle":"2022-08-11T11:50:37.718948Z","shell.execute_reply.started":"2022-08-11T11:50:37.711282Z","shell.execute_reply":"2022-08-11T11:50:37.717795Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_3=X.drop(\"VIP\", axis=1)\nX_3=X_3.drop(\"num\", axis=1)\nX_3=X_3.drop(\"Age\", axis=1)\n#X_3=X_3.drop(\"Exp\", axis=1)\n#X_3=X_3.drop(\"Destination_55 Cancri e\", axis=1)\n#Exp and Destination_55 Cancri e removal downgrade the accuracy\nX_3=X_3.drop(\"HomePlanet_Earth\", axis=1)\nX_3=X_3.drop(\"HomePlanet_Europa\", axis=1)\n#X_3=X_3.drop(\"HomePlanet_Mars\", axis=1)\n#X_3=X_3.drop(\"Cryo\", axis=1)\n#X_3=X_3.drop(\"deck\", axis=1)\nX_3=X_3.drop(\"Destination_TRAPPIST-1e\", axis=1)\n#X_3=X_3.drop(\"Destination_PSO J318.5-22\", axis=1)\n#X_3=X_3.drop(\"side\", axis=1)\n# Destination_TRAPPIST-1e removal gave a significant performance improvement\nimport numpy as np\nfrom sklearn.impute import SimpleImputer\nimp = SimpleImputer(missing_values=np.nan, strategy='most_frequent')\nX_3=imp.fit_transform(X_3)\nfrom sklearn.model_selection import train_test_split\nX_train, X_test, Y_train, Y_test = train_test_split(X_3,Y, test_size=0.25, random_state = 0)","metadata":{"execution":{"iopub.status.busy":"2022-08-11T11:50:41.029374Z","iopub.execute_input":"2022-08-11T11:50:41.029782Z","iopub.status.idle":"2022-08-11T11:50:41.057715Z","shell.execute_reply.started":"2022-08-11T11:50:41.029736Z","shell.execute_reply":"2022-08-11T11:50:41.056722Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.naive_bayes import GaussianNB\nfrom sklearn.metrics import accuracy_score\n\nmodel3=GaussianNB()\nmodel3.fit(X_train, Y_train)\nY_pred=model3.predict(X_test)\nacc=accuracy_score(Y_test, Y_pred)\nprint(acc)","metadata":{"execution":{"iopub.status.busy":"2022-08-11T11:50:42.515916Z","iopub.execute_input":"2022-08-11T11:50:42.517000Z","iopub.status.idle":"2022-08-11T11:50:42.529632Z","shell.execute_reply.started":"2022-08-11T11:50:42.516944Z","shell.execute_reply":"2022-08-11T11:50:42.527990Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.neighbors import KNeighborsClassifier\nmodel2 = KNeighborsClassifier(n_neighbors=10)\nmodel2.fit(X_train, Y_train)\nY_pred=model2.predict(X_test)\nacc=accuracy_score(Y_test, Y_pred)\nprint(acc)","metadata":{"execution":{"iopub.status.busy":"2022-08-11T11:50:43.697845Z","iopub.execute_input":"2022-08-11T11:50:43.698269Z","iopub.status.idle":"2022-08-11T11:50:43.788826Z","shell.execute_reply.started":"2022-08-11T11:50:43.698226Z","shell.execute_reply":"2022-08-11T11:50:43.787493Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import xgboost as xgb\nmodel=xgb.XGBClassifier()\nmodel.fit(X_train, Y_train)\nY_pred=model.predict(X_test)\nacc=accuracy_score(Y_test, Y_pred)\nprint(acc)","metadata":{"execution":{"iopub.status.busy":"2022-08-11T11:50:44.883431Z","iopub.execute_input":"2022-08-11T11:50:44.884427Z","iopub.status.idle":"2022-08-11T11:50:45.666211Z","shell.execute_reply.started":"2022-08-11T11:50:44.884385Z","shell.execute_reply":"2022-08-11T11:50:45.665154Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.ensemble import GradientBoostingClassifier\nmodel=GradientBoostingClassifier()\nmodel.fit(X_train, Y_train)\nY_pred=model.predict(X_test)\nacc=accuracy_score(Y_test.values, Y_pred)\nprint(acc)","metadata":{"execution":{"iopub.status.busy":"2022-08-11T11:57:08.754674Z","iopub.execute_input":"2022-08-11T11:57:08.755118Z","iopub.status.idle":"2022-08-11T11:57:09.193808Z","shell.execute_reply.started":"2022-08-11T11:57:08.755086Z","shell.execute_reply":"2022-08-11T11:57:09.190125Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission","metadata":{"execution":{"iopub.status.busy":"2022-08-11T11:50:48.746767Z","iopub.execute_input":"2022-08-11T11:50:48.747189Z","iopub.status.idle":"2022-08-11T11:50:48.762118Z","shell.execute_reply.started":"2022-08-11T11:50:48.747158Z","shell.execute_reply":"2022-08-11T11:50:48.761052Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test","metadata":{"execution":{"iopub.status.busy":"2022-08-11T11:50:50.108174Z","iopub.execute_input":"2022-08-11T11:50:50.108571Z","iopub.status.idle":"2022-08-11T11:50:50.140330Z","shell.execute_reply.started":"2022-08-11T11:50:50.108541Z","shell.execute_reply":"2022-08-11T11:50:50.138981Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_new=pd.get_dummies(test, columns = [\"HomePlanet\"])","metadata":{"execution":{"iopub.status.busy":"2022-08-11T11:50:54.022540Z","iopub.execute_input":"2022-08-11T11:50:54.022947Z","iopub.status.idle":"2022-08-11T11:50:54.034427Z","shell.execute_reply.started":"2022-08-11T11:50:54.022914Z","shell.execute_reply":"2022-08-11T11:50:54.033092Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_new[\"Cryo\"]=test_new.apply(lambda row: fill_cryo(row), axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-08-11T11:51:00.440397Z","iopub.execute_input":"2022-08-11T11:51:00.440855Z","iopub.status.idle":"2022-08-11T11:51:00.498967Z","shell.execute_reply.started":"2022-08-11T11:51:00.440819Z","shell.execute_reply":"2022-08-11T11:51:00.498055Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_new =test_new.drop(\"CryoSleep\", axis =1)","metadata":{"execution":{"iopub.status.busy":"2022-08-11T11:51:05.038258Z","iopub.execute_input":"2022-08-11T11:51:05.038671Z","iopub.status.idle":"2022-08-11T11:51:05.045413Z","shell.execute_reply.started":"2022-08-11T11:51:05.038638Z","shell.execute_reply":"2022-08-11T11:51:05.044278Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_cabin=pd.DataFrame().assign(Cabin=test['Cabin'])\ntest_cabin[['deck', 'num', 'side']] = test_cabin['Cabin'].str.split(\"/\",expand=True)\n#replace nan with top values from df.describe()\nimport numpy as np\ntest_cabin['deck'] = test_cabin['deck'].replace(np.nan, \"F\")\ntest_cabin['num'] = test_cabin['num'].replace(np.nan, \"82\")\ntest_cabin['side'] = test_cabin['side'].replace(np.nan, \"S\")\n\nfrom sklearn import preprocessing\n  \n# label_encoder object knows how to understand word labels.\nlabel_encoder = preprocessing.LabelEncoder()\n\n# conversion functions to numerical categories\ndef conv_num(row):\n    return int(row[\"num\"])\ndef conv_side(row):\n    if row[\"side\"]==\"S\":\n        return 0\n    else:\n        return 1\ntest_cabin['deck']= label_encoder.fit_transform(test_cabin['deck'])\ntest_cabin[\"num\"]=test_cabin.apply(lambda row: conv_num(row), axis=1)\ntest_cabin[\"side\"]=test_cabin.apply(lambda row: conv_side(row), axis=1)\n\ntest_new=test_new.drop(\"Cabin\", axis=1)\ntest_new[\"deck\"]=test_cabin[\"deck\"]\ntest_new[\"num\"]=test_cabin[\"num\"]\ntest_new[\"side\"]=test_cabin[\"side\"]\n\n# Replace nan with top value\ntest_new[\"Destination\"] = test_new[\"Destination\"].replace(np.nan, \"TRAPPIST-1e\")\n#Convert into numerical category\ntest_new=pd.get_dummies(test_new, columns = [\"Destination\"])\n\ntest_new[\"VIP\"]=test_new.apply(lambda row: fill_VIP(row), axis=1)\ntest_new[\"Exp\"]=test_new.apply(lambda row: fill_expense(row), axis=1)\ntest_new =test_new.drop(\"RoomService\", axis =1)\ntest_new =test_new.drop(\"FoodCourt\", axis =1)\ntest_new =test_new.drop(\"ShoppingMall\", axis =1)\ntest_new =test_new.drop(\"Spa\", axis =1)\ntest_new =test_new.drop(\"VRDeck\", axis =1)\nprint(test_new)\nX=test_new\n#X=X.drop(\"PassengerId\", axis =1)\nX=X.drop(\"Name\", axis =1)\n\nX_3=X.drop(\"VIP\", axis=1)\nX_3=X_3.drop(\"num\", axis=1)\nX_3=X_3.drop(\"Age\", axis=1)\n#X_3=X_3.drop(\"Exp\", axis=1)\n#X_3=X_3.drop(\"Destination_55 Cancri e\", axis=1)\n#Exp and Destination_55 Cancri e removal downgrade the accuracy\nX_3=X_3.drop(\"HomePlanet_Earth\", axis=1)\nX_3=X_3.drop(\"HomePlanet_Europa\", axis=1)\n#X_3=X_3.drop(\"HomePlanet_Mars\", axis=1)\n#X_3=X_3.drop(\"Cryo\", axis=1)\n#X_3=X_3.drop(\"deck\", axis=1)\nX_3=X_3.drop(\"Destination_TRAPPIST-1e\", axis=1)\n#X_3=X_3.drop(\"Destination_PSO J318.5-22\", axis=1)\n#X_3=X_3.drop(\"side\", axis=1)\n# Destination_TRAPPIST-1e removal gave a significant performance improvement\n\nimport numpy as np\nfrom sklearn.impute import SimpleImputer\nimp = SimpleImputer(missing_values=np.nan, strategy='most_frequent')\n\n\n","metadata":{"execution":{"iopub.status.busy":"2022-08-11T11:51:07.028943Z","iopub.execute_input":"2022-08-11T11:51:07.029355Z","iopub.status.idle":"2022-08-11T11:51:07.360847Z","shell.execute_reply.started":"2022-08-11T11:51:07.029320Z","shell.execute_reply":"2022-08-11T11:51:07.359906Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_3=X_3.drop(\"PassengerId\", axis=1)\nX_3=imp.fit_transform(X_3)\n","metadata":{"execution":{"iopub.status.busy":"2022-08-11T12:08:20.005044Z","iopub.execute_input":"2022-08-11T12:08:20.005553Z","iopub.status.idle":"2022-08-11T12:08:20.026269Z","shell.execute_reply.started":"2022-08-11T12:08:20.005515Z","shell.execute_reply":"2022-08-11T12:08:20.024626Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = model.predict(X_3)\n\nsub=pd.DataFrame({'Transported':y_pred.astype(bool)},index=test.index)\n\nsub.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-11T12:08:27.364011Z","iopub.execute_input":"2022-08-11T12:08:27.364406Z","iopub.status.idle":"2022-08-11T12:08:27.384569Z","shell.execute_reply.started":"2022-08-11T12:08:27.364368Z","shell.execute_reply":"2022-08-11T12:08:27.383495Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"submission","metadata":{"execution":{"iopub.status.busy":"2022-08-11T12:09:04.116742Z","iopub.execute_input":"2022-08-11T12:09:04.117195Z","iopub.status.idle":"2022-08-11T12:09:04.137284Z","shell.execute_reply.started":"2022-08-11T12:09:04.117162Z","shell.execute_reply":"2022-08-11T12:09:04.133681Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission[\"Transported\"]=sub[\"Transported\"]\nsubmission","metadata":{"execution":{"iopub.status.busy":"2022-08-11T12:12:18.005001Z","iopub.execute_input":"2022-08-11T12:12:18.005815Z","iopub.status.idle":"2022-08-11T12:12:18.020330Z","shell.execute_reply.started":"2022-08-11T12:12:18.005743Z","shell.execute_reply":"2022-08-11T12:12:18.019369Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-08-11T12:12:24.802910Z","iopub.execute_input":"2022-08-11T12:12:24.803659Z","iopub.status.idle":"2022-08-11T12:12:24.816993Z","shell.execute_reply.started":"2022-08-11T12:12:24.803618Z","shell.execute_reply":"2022-08-11T12:12:24.815690Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}