{"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":"markdown","source":"Current best result: 0.80476 (with XGBoost). Support Vector Classifier achieves 0.80102 accuracy.","metadata":{}},{"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-06T17:40:45.308912Z","iopub.execute_input":"2022-08-06T17:40:45.309597Z","iopub.status.idle":"2022-08-06T17:40:45.328210Z","shell.execute_reply.started":"2022-08-06T17:40:45.309433Z","shell.execute_reply":"2022-08-06T17:40:45.326667Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Let's read the data and look at it","metadata":{}},{"cell_type":"code","source":"train_data = pd.read_csv(\"/kaggle/input/spaceship-titanic/train.csv\", index_col = 'PassengerId')\ntrain_data.sample(20)","metadata":{"execution":{"iopub.status.busy":"2022-08-06T17:40:45.329900Z","iopub.execute_input":"2022-08-06T17:40:45.330756Z","iopub.status.idle":"2022-08-06T17:40:45.409867Z","shell.execute_reply.started":"2022-08-06T17:40:45.330695Z","shell.execute_reply":"2022-08-06T17:40:45.408464Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data = pd.read_csv(\"/kaggle/input/spaceship-titanic/test.csv\", index_col = 'PassengerId')\ntest_data.sample(20)","metadata":{"execution":{"iopub.status.busy":"2022-08-06T17:40:45.411810Z","iopub.execute_input":"2022-08-06T17:40:45.412479Z","iopub.status.idle":"2022-08-06T17:40:45.462623Z","shell.execute_reply.started":"2022-08-06T17:40:45.412440Z","shell.execute_reply":"2022-08-06T17:40:45.461599Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# EDA","metadata":{}},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n%matplotlib inline\nimport seaborn as sns","metadata":{"execution":{"iopub.status.busy":"2022-08-06T17:40:45.465285Z","iopub.execute_input":"2022-08-06T17:40:45.466229Z","iopub.status.idle":"2022-08-06T17:40:46.107621Z","shell.execute_reply.started":"2022-08-06T17:40:45.466185Z","shell.execute_reply":"2022-08-06T17:40:46.106568Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.columns","metadata":{"execution":{"iopub.status.busy":"2022-08-06T17:40:46.109075Z","iopub.execute_input":"2022-08-06T17:40:46.109632Z","iopub.status.idle":"2022-08-06T17:40:46.118356Z","shell.execute_reply.started":"2022-08-06T17:40:46.109597Z","shell.execute_reply":"2022-08-06T17:40:46.117048Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.info()","metadata":{"execution":{"iopub.status.busy":"2022-08-06T17:40:46.120416Z","iopub.execute_input":"2022-08-06T17:40:46.120817Z","iopub.status.idle":"2022-08-06T17:40:46.149351Z","shell.execute_reply.started":"2022-08-06T17:40:46.120782Z","shell.execute_reply":"2022-08-06T17:40:46.147969Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.isna().sum()/train_data.shape[0]","metadata":{"execution":{"iopub.status.busy":"2022-08-06T17:40:46.151098Z","iopub.execute_input":"2022-08-06T17:40:46.151473Z","iopub.status.idle":"2022-08-06T17:40:46.166525Z","shell.execute_reply.started":"2022-08-06T17:40:46.151430Z","shell.execute_reply":"2022-08-06T17:40:46.165496Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.heatmap(train_data.isnull(),yticklabels=False,cbar=False)","metadata":{"execution":{"iopub.status.busy":"2022-08-06T17:40:46.168456Z","iopub.execute_input":"2022-08-06T17:40:46.168910Z","iopub.status.idle":"2022-08-06T17:40:46.471296Z","shell.execute_reply.started":"2022-08-06T17:40:46.168855Z","shell.execute_reply":"2022-08-06T17:40:46.469908Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"All columns, except for the target one (Transported), have missing values","metadata":{}},{"cell_type":"code","source":"train_data.describe()","metadata":{"execution":{"iopub.status.busy":"2022-08-06T17:40:46.474935Z","iopub.execute_input":"2022-08-06T17:40:46.475567Z","iopub.status.idle":"2022-08-06T17:40:46.514443Z","shell.execute_reply.started":"2022-08-06T17:40:46.475517Z","shell.execute_reply":"2022-08-06T17:40:46.513059Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.HomePlanet.value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-08-06T17:40:46.516390Z","iopub.execute_input":"2022-08-06T17:40:46.517440Z","iopub.status.idle":"2022-08-06T17:40:46.528453Z","shell.execute_reply.started":"2022-08-06T17:40:46.517375Z","shell.execute_reply":"2022-08-06T17:40:46.526996Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.countplot(x='HomePlanet', data=train_data)","metadata":{"execution":{"iopub.status.busy":"2022-08-06T17:40:46.531004Z","iopub.execute_input":"2022-08-06T17:40:46.532020Z","iopub.status.idle":"2022-08-06T17:40:46.724417Z","shell.execute_reply.started":"2022-08-06T17:40:46.531960Z","shell.execute_reply":"2022-08-06T17:40:46.723168Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.Destination.value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-08-06T17:40:46.725866Z","iopub.execute_input":"2022-08-06T17:40:46.726365Z","iopub.status.idle":"2022-08-06T17:40:46.736620Z","shell.execute_reply.started":"2022-08-06T17:40:46.726325Z","shell.execute_reply":"2022-08-06T17:40:46.735753Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.countplot(x='Destination', data=train_data)","metadata":{"execution":{"iopub.status.busy":"2022-08-06T17:40:46.741603Z","iopub.execute_input":"2022-08-06T17:40:46.742996Z","iopub.status.idle":"2022-08-06T17:40:46.949183Z","shell.execute_reply.started":"2022-08-06T17:40:46.742945Z","shell.execute_reply":"2022-08-06T17:40:46.947767Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.CryoSleep.value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-08-06T17:40:46.951040Z","iopub.execute_input":"2022-08-06T17:40:46.951502Z","iopub.status.idle":"2022-08-06T17:40:46.961402Z","shell.execute_reply.started":"2022-08-06T17:40:46.951460Z","shell.execute_reply":"2022-08-06T17:40:46.960071Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.countplot(x='CryoSleep', data=train_data)","metadata":{"execution":{"iopub.status.busy":"2022-08-06T17:40:46.963215Z","iopub.execute_input":"2022-08-06T17:40:46.963758Z","iopub.status.idle":"2022-08-06T17:40:47.169427Z","shell.execute_reply.started":"2022-08-06T17:40:46.963679Z","shell.execute_reply":"2022-08-06T17:40:47.168174Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.Transported.value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-08-06T17:40:47.170645Z","iopub.execute_input":"2022-08-06T17:40:47.171000Z","iopub.status.idle":"2022-08-06T17:40:47.181412Z","shell.execute_reply.started":"2022-08-06T17:40:47.170969Z","shell.execute_reply":"2022-08-06T17:40:47.180143Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.countplot(x='Transported', data=train_data)","metadata":{"execution":{"iopub.status.busy":"2022-08-06T17:40:47.182522Z","iopub.execute_input":"2022-08-06T17:40:47.182855Z","iopub.status.idle":"2022-08-06T17:40:47.350752Z","shell.execute_reply.started":"2022-08-06T17:40:47.182825Z","shell.execute_reply":"2022-08-06T17:40:47.349598Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.heatmap(train_data.corr(method ='pearson'), annot=True)","metadata":{"execution":{"iopub.status.busy":"2022-08-06T17:40:47.353764Z","iopub.execute_input":"2022-08-06T17:40:47.354780Z","iopub.status.idle":"2022-08-06T17:40:47.836559Z","shell.execute_reply.started":"2022-08-06T17:40:47.354744Z","shell.execute_reply":"2022-08-06T17:40:47.835433Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.groupby(['Transported', 'HomePlanet']).agg({'Transported': 'count'})","metadata":{"execution":{"iopub.status.busy":"2022-08-06T17:40:47.838311Z","iopub.execute_input":"2022-08-06T17:40:47.838733Z","iopub.status.idle":"2022-08-06T17:40:47.856269Z","shell.execute_reply.started":"2022-08-06T17:40:47.838663Z","shell.execute_reply":"2022-08-06T17:40:47.855351Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.catplot(x='Transported',col='HomePlanet',kind='count',data=train_data)","metadata":{"execution":{"iopub.status.busy":"2022-08-06T17:40:47.857848Z","iopub.execute_input":"2022-08-06T17:40:47.858677Z","iopub.status.idle":"2022-08-06T17:40:48.354786Z","shell.execute_reply.started":"2022-08-06T17:40:47.858644Z","shell.execute_reply":"2022-08-06T17:40:48.353646Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.groupby(['Transported', 'Destination']).agg({'Transported': 'count'})","metadata":{"execution":{"iopub.status.busy":"2022-08-06T17:40:48.356311Z","iopub.execute_input":"2022-08-06T17:40:48.356711Z","iopub.status.idle":"2022-08-06T17:40:48.372415Z","shell.execute_reply.started":"2022-08-06T17:40:48.356658Z","shell.execute_reply":"2022-08-06T17:40:48.371283Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.catplot(x='Transported',col='Destination',kind='count',data=train_data)","metadata":{"execution":{"iopub.status.busy":"2022-08-06T17:40:48.373773Z","iopub.execute_input":"2022-08-06T17:40:48.374099Z","iopub.status.idle":"2022-08-06T17:40:48.860431Z","shell.execute_reply.started":"2022-08-06T17:40:48.374068Z","shell.execute_reply":"2022-08-06T17:40:48.859560Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.groupby(['Transported', 'CryoSleep']).agg({'Transported': 'count'})","metadata":{"execution":{"iopub.status.busy":"2022-08-06T17:40:48.861495Z","iopub.execute_input":"2022-08-06T17:40:48.862256Z","iopub.status.idle":"2022-08-06T17:40:48.876724Z","shell.execute_reply.started":"2022-08-06T17:40:48.862222Z","shell.execute_reply":"2022-08-06T17:40:48.875789Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.catplot(x='Transported',col='CryoSleep',kind='count',data=train_data)","metadata":{"execution":{"iopub.status.busy":"2022-08-06T17:40:48.878227Z","iopub.execute_input":"2022-08-06T17:40:48.879615Z","iopub.status.idle":"2022-08-06T17:40:49.266065Z","shell.execute_reply.started":"2022-08-06T17:40:48.879569Z","shell.execute_reply":"2022-08-06T17:40:49.264892Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"It seems that your chances of being transported are somewhat increased by European origin and heavily increased by being in criosleep...","metadata":{}},{"cell_type":"markdown","source":"# Preprocessing","metadata":{}},{"cell_type":"code","source":"y_train = train_data.Transported\nX_train_full = train_data.drop(['Transported'], axis=1, inplace=False)\nX_test_full = test_data","metadata":{"execution":{"iopub.status.busy":"2022-08-06T17:40:49.267530Z","iopub.execute_input":"2022-08-06T17:40:49.268533Z","iopub.status.idle":"2022-08-06T17:40:49.276035Z","shell.execute_reply.started":"2022-08-06T17:40:49.268487Z","shell.execute_reply":"2022-08-06T17:40:49.275096Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_full","metadata":{"execution":{"iopub.status.busy":"2022-08-06T17:40:49.277862Z","iopub.execute_input":"2022-08-06T17:40:49.278284Z","iopub.status.idle":"2022-08-06T17:40:49.311314Z","shell.execute_reply.started":"2022-08-06T17:40:49.278244Z","shell.execute_reply":"2022-08-06T17:40:49.310156Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_full.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-06T17:40:49.313132Z","iopub.execute_input":"2022-08-06T17:40:49.313946Z","iopub.status.idle":"2022-08-06T17:40:49.326053Z","shell.execute_reply.started":"2022-08-06T17:40:49.313903Z","shell.execute_reply":"2022-08-06T17:40:49.324978Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test_full.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-06T17:40:49.327407Z","iopub.execute_input":"2022-08-06T17:40:49.327773Z","iopub.status.idle":"2022-08-06T17:40:49.339638Z","shell.execute_reply.started":"2022-08-06T17:40:49.327727Z","shell.execute_reply":"2022-08-06T17:40:49.338446Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"list(X_train_full['Name'][:200])","metadata":{"execution":{"iopub.status.busy":"2022-08-06T17:40:49.340923Z","iopub.execute_input":"2022-08-06T17:40:49.341466Z","iopub.status.idle":"2022-08-06T17:40:49.353569Z","shell.execute_reply.started":"2022-08-06T17:40:49.341435Z","shell.execute_reply":"2022-08-06T17:40:49.352500Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Idea: Does any name consists of (one-word) first name and (one-word) last name? Perhaps we can split them and count family size (supposing that members of the same family share last name)?","metadata":{}},{"cell_type":"code","source":"X_train_full[['FirstName', 'LastName']] = X_train_full['Name'].str.split(' ', expand=True, n=1)\nX_train_full.drop(columns=['Name'], inplace=True)\nX_train_full","metadata":{"execution":{"iopub.status.busy":"2022-08-06T17:40:49.355158Z","iopub.execute_input":"2022-08-06T17:40:49.355888Z","iopub.status.idle":"2022-08-06T17:40:49.409404Z","shell.execute_reply.started":"2022-08-06T17:40:49.355846Z","shell.execute_reply":"2022-08-06T17:40:49.408279Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_full['LastName'].nunique()","metadata":{"execution":{"iopub.status.busy":"2022-08-06T17:40:49.411387Z","iopub.execute_input":"2022-08-06T17:40:49.412177Z","iopub.status.idle":"2022-08-06T17:40:49.421363Z","shell.execute_reply.started":"2022-08-06T17:40:49.412119Z","shell.execute_reply":"2022-08-06T17:40:49.420546Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_full['LastName'].isna().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-06T17:40:49.423212Z","iopub.execute_input":"2022-08-06T17:40:49.423984Z","iopub.status.idle":"2022-08-06T17:40:49.433251Z","shell.execute_reply.started":"2022-08-06T17:40:49.423943Z","shell.execute_reply":"2022-08-06T17:40:49.432151Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_full['FirstName'].fillna(value='Unknown',inplace=True)\nX_train_full['LastName'].fillna(value='Unknown',inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-08-06T17:40:49.435104Z","iopub.execute_input":"2022-08-06T17:40:49.435915Z","iopub.status.idle":"2022-08-06T17:40:49.444351Z","shell.execute_reply.started":"2022-08-06T17:40:49.435872Z","shell.execute_reply":"2022-08-06T17:40:49.443437Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test_full[['FirstName', 'LastName']] = X_test_full['Name'].str.split(' ', expand=True, n=1)\nX_test_full.drop(columns=['Name'], inplace=True)\nX_test_full","metadata":{"execution":{"iopub.status.busy":"2022-08-06T17:40:49.445877Z","iopub.execute_input":"2022-08-06T17:40:49.446306Z","iopub.status.idle":"2022-08-06T17:40:49.491046Z","shell.execute_reply.started":"2022-08-06T17:40:49.446263Z","shell.execute_reply":"2022-08-06T17:40:49.490125Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test_full['FirstName'].fillna(value='Unknown',inplace=True)\nX_test_full['LastName'].fillna(value='Unknown',inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-08-06T17:40:49.492091Z","iopub.execute_input":"2022-08-06T17:40:49.492880Z","iopub.status.idle":"2022-08-06T17:40:49.498799Z","shell.execute_reply.started":"2022-08-06T17:40:49.492841Z","shell.execute_reply":"2022-08-06T17:40:49.497773Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test_full['LastName'].nunique()","metadata":{"execution":{"iopub.status.busy":"2022-08-06T17:40:49.499832Z","iopub.execute_input":"2022-08-06T17:40:49.500507Z","iopub.status.idle":"2022-08-06T17:40:49.514309Z","shell.execute_reply.started":"2022-08-06T17:40:49.500477Z","shell.execute_reply":"2022-08-06T17:40:49.513448Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(set(X_train_full['LastName']).intersection(set(X_test_full['LastName'])))","metadata":{"execution":{"iopub.status.busy":"2022-08-06T17:40:49.515909Z","iopub.execute_input":"2022-08-06T17:40:49.517258Z","iopub.status.idle":"2022-08-06T17:40:49.527659Z","shell.execute_reply.started":"2022-08-06T17:40:49.517214Z","shell.execute_reply":"2022-08-06T17:40:49.526750Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"It seems that members of the same family are VERY often present in both train and test set, so let's count family size using all the data","metadata":{}},{"cell_type":"code","source":"X_all = pd.concat([X_train_full, X_test_full])\nX_all","metadata":{"execution":{"iopub.status.busy":"2022-08-06T17:40:49.529082Z","iopub.execute_input":"2022-08-06T17:40:49.529639Z","iopub.status.idle":"2022-08-06T17:40:49.567039Z","shell.execute_reply.started":"2022-08-06T17:40:49.529608Z","shell.execute_reply":"2022-08-06T17:40:49.565946Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"family_size = X_all['LastName'].value_counts()\nfamily_size","metadata":{"execution":{"iopub.status.busy":"2022-08-06T17:40:49.568505Z","iopub.execute_input":"2022-08-06T17:40:49.569021Z","iopub.status.idle":"2022-08-06T17:40:49.581841Z","shell.execute_reply.started":"2022-08-06T17:40:49.568965Z","shell.execute_reply":"2022-08-06T17:40:49.580901Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import statistics\nfamily_size['Unknown'] = statistics.median(family_size.values)\nfamily_size['Unknown']","metadata":{"execution":{"iopub.status.busy":"2022-08-06T17:40:49.592444Z","iopub.execute_input":"2022-08-06T17:40:49.593486Z","iopub.status.idle":"2022-08-06T17:40:49.603662Z","shell.execute_reply.started":"2022-08-06T17:40:49.593442Z","shell.execute_reply":"2022-08-06T17:40:49.602833Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_full['FamilySize'] = X_train_full['LastName'].apply(lambda s: family_size[s])\nX_train_full","metadata":{"execution":{"iopub.status.busy":"2022-08-06T17:40:49.604994Z","iopub.execute_input":"2022-08-06T17:40:49.606019Z","iopub.status.idle":"2022-08-06T17:40:49.678701Z","shell.execute_reply.started":"2022-08-06T17:40:49.605985Z","shell.execute_reply":"2022-08-06T17:40:49.677571Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test_full['FamilySize'] = X_test_full['LastName'].apply(lambda s: family_size[s])\nX_test_full","metadata":{"execution":{"iopub.status.busy":"2022-08-06T17:40:49.681980Z","iopub.execute_input":"2022-08-06T17:40:49.682331Z","iopub.status.idle":"2022-08-06T17:40:49.735592Z","shell.execute_reply.started":"2022-08-06T17:40:49.682299Z","shell.execute_reply":"2022-08-06T17:40:49.734822Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_full[['Deck', 'Num', 'Side']] = X_train_full['Cabin'].str.split('/', expand=True)\nX_test_full[['Deck', 'Num', 'Side']] = X_test_full['Cabin'].str.split('/', expand=True)\nX_train_full","metadata":{"execution":{"iopub.status.busy":"2022-08-06T17:40:49.737206Z","iopub.execute_input":"2022-08-06T17:40:49.737526Z","iopub.status.idle":"2022-08-06T17:40:49.948880Z","shell.execute_reply.started":"2022-08-06T17:40:49.737495Z","shell.execute_reply":"2022-08-06T17:40:49.947724Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = X_train_full.drop(columns=['FirstName', 'LastName', 'Cabin', 'Num'])\nX_test = test_data.drop(columns=['FirstName', 'LastName', 'Cabin', 'Num'])","metadata":{"execution":{"iopub.status.busy":"2022-08-06T17:40:49.950278Z","iopub.execute_input":"2022-08-06T17:40:49.950614Z","iopub.status.idle":"2022-08-06T17:40:49.964532Z","shell.execute_reply.started":"2022-08-06T17:40:49.950584Z","shell.execute_reply":"2022-08-06T17:40:49.963506Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cat_cols = [col for col in X_train.columns if X_train[col].dtype=='object']\ncat_cols","metadata":{"execution":{"iopub.status.busy":"2022-08-06T17:40:49.965852Z","iopub.execute_input":"2022-08-06T17:40:49.966534Z","iopub.status.idle":"2022-08-06T17:40:49.976372Z","shell.execute_reply.started":"2022-08-06T17:40:49.966501Z","shell.execute_reply":"2022-08-06T17:40:49.975482Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_cols = [col for col in X_train.columns if X_train[col].dtype=='float64']\nnum_cols","metadata":{"execution":{"iopub.status.busy":"2022-08-06T17:40:49.977677Z","iopub.execute_input":"2022-08-06T17:40:49.978477Z","iopub.status.idle":"2022-08-06T17:40:49.986392Z","shell.execute_reply.started":"2022-08-06T17:40:49.978429Z","shell.execute_reply":"2022-08-06T17:40:49.985545Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"luxury_cols = [col for col in num_cols if col != 'Age']\nluxury_cols","metadata":{"execution":{"iopub.status.busy":"2022-08-06T17:40:49.987800Z","iopub.execute_input":"2022-08-06T17:40:49.988446Z","iopub.status.idle":"2022-08-06T17:40:49.996896Z","shell.execute_reply.started":"2022-08-06T17:40:49.988413Z","shell.execute_reply":"2022-08-06T17:40:49.996168Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for col in cat_cols:\n    X_train[col].fillna(value=X_train[col].mode()[0],inplace=True)\n    X_test[col].fillna(value=X_train[col].mode()[0],inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-08-06T17:40:49.998050Z","iopub.execute_input":"2022-08-06T17:40:49.998707Z","iopub.status.idle":"2022-08-06T17:40:50.026047Z","shell.execute_reply.started":"2022-08-06T17:40:49.998662Z","shell.execute_reply":"2022-08-06T17:40:50.025263Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-06T17:40:50.027348Z","iopub.execute_input":"2022-08-06T17:40:50.028011Z","iopub.status.idle":"2022-08-06T17:40:50.037974Z","shell.execute_reply.started":"2022-08-06T17:40:50.027978Z","shell.execute_reply":"2022-08-06T17:40:50.036956Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for col in luxury_cols:\n    print(X_train.loc[X_train.CryoSleep==True, col].max())","metadata":{"execution":{"iopub.status.busy":"2022-08-06T17:40:50.039415Z","iopub.execute_input":"2022-08-06T17:40:50.039954Z","iopub.status.idle":"2022-08-06T17:40:50.051909Z","shell.execute_reply.started":"2022-08-06T17:40:50.039913Z","shell.execute_reply":"2022-08-06T17:40:50.050847Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for col in luxury_cols:\n    print(X_train.loc[X_train.CryoSleep==False, col].min(), X_train.loc[X_train.CryoSleep==False, col].median())","metadata":{"execution":{"iopub.status.busy":"2022-08-06T17:40:50.053292Z","iopub.execute_input":"2022-08-06T17:40:50.053601Z","iopub.status.idle":"2022-08-06T17:40:50.071347Z","shell.execute_reply.started":"2022-08-06T17:40:50.053572Z","shell.execute_reply":"2022-08-06T17:40:50.070382Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"median_costs = X_train.loc[X_train.CryoSleep==False, luxury_cols].median()\nmedian_costs","metadata":{"execution":{"iopub.status.busy":"2022-08-06T17:40:50.072582Z","iopub.execute_input":"2022-08-06T17:40:50.073301Z","iopub.status.idle":"2022-08-06T17:40:50.086203Z","shell.execute_reply.started":"2022-08-06T17:40:50.073266Z","shell.execute_reply":"2022-08-06T17:40:50.084929Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for col in luxury_cols:\n    X_train[col].fillna(X_train.groupby('CryoSleep')[col].transform('median'), inplace=True)\n    X_test[col].fillna(X_test.groupby('CryoSleep')[col].transform('median'), inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-08-06T17:40:50.087502Z","iopub.execute_input":"2022-08-06T17:40:50.087864Z","iopub.status.idle":"2022-08-06T17:40:50.109484Z","shell.execute_reply.started":"2022-08-06T17:40:50.087830Z","shell.execute_reply":"2022-08-06T17:40:50.108261Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-06T17:40:50.111028Z","iopub.execute_input":"2022-08-06T17:40:50.111378Z","iopub.status.idle":"2022-08-06T17:40:50.122851Z","shell.execute_reply.started":"2022-08-06T17:40:50.111347Z","shell.execute_reply":"2022-08-06T17:40:50.121699Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.Age.fillna(value=X_train.Age.median(),inplace=True)\nX_test.Age.fillna(value=X_train.Age.median(),inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-08-06T17:40:50.124156Z","iopub.execute_input":"2022-08-06T17:40:50.124566Z","iopub.status.idle":"2022-08-06T17:40:50.131842Z","shell.execute_reply.started":"2022-08-06T17:40:50.124459Z","shell.execute_reply":"2022-08-06T17:40:50.131053Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-06T17:40:50.133027Z","iopub.execute_input":"2022-08-06T17:40:50.134098Z","iopub.status.idle":"2022-08-06T17:40:50.151178Z","shell.execute_reply.started":"2022-08-06T17:40:50.133928Z","shell.execute_reply":"2022-08-06T17:40:50.149747Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-06T17:40:50.152594Z","iopub.execute_input":"2022-08-06T17:40:50.153096Z","iopub.status.idle":"2022-08-06T17:40:50.163631Z","shell.execute_reply.started":"2022-08-06T17:40:50.153062Z","shell.execute_reply":"2022-08-06T17:40:50.162548Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# X_train['LuxurySpends'] = sum([X_train[col] for col in luxury_cols])\n# X_test['LuxurySpends'] = sum([X_test[col] for col in luxury_cols])\n# X_train.drop(columns=luxury_cols, inplace=True)\n# X_test.drop(columns=luxury_cols, inplace=True)\n# X_train","metadata":{"execution":{"iopub.status.busy":"2022-08-06T17:40:50.165357Z","iopub.execute_input":"2022-08-06T17:40:50.165745Z","iopub.status.idle":"2022-08-06T17:40:50.174132Z","shell.execute_reply.started":"2022-08-06T17:40:50.165704Z","shell.execute_reply":"2022-08-06T17:40:50.172769Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.feature_selection import mutual_info_classif\n\n# functions from the course on Feature Engineering, with modification (categorical target)\ndef make_mi_scores(X, y):\n    X = X.copy()\n    for colname in X.select_dtypes([\"object\", \"category\"]):\n        X[colname], _ = X[colname].factorize()\n    # All discrete features should now have integer dtypes\n    discrete_features = [pd.api.types.is_integer_dtype(t) for t in X.dtypes]\n    mi_scores = mutual_info_classif(X, y, discrete_features=discrete_features, random_state=0)\n    mi_scores = pd.Series(mi_scores, name=\"MI Scores\", index=X.columns)\n    mi_scores = mi_scores.sort_values(ascending=False)\n    return mi_scores\n\n\ndef plot_mi_scores(scores):\n    scores = scores.sort_values(ascending=True)\n    width = np.arange(len(scores))\n    ticks = list(scores.index)\n    plt.barh(width, scores)\n    plt.yticks(width, ticks)\n    plt.title(\"Mutual Information Scores\")\n\n","metadata":{"execution":{"iopub.status.busy":"2022-08-06T17:40:50.175591Z","iopub.execute_input":"2022-08-06T17:40:50.176793Z","iopub.status.idle":"2022-08-06T17:40:50.271659Z","shell.execute_reply.started":"2022-08-06T17:40:50.176748Z","shell.execute_reply":"2022-08-06T17:40:50.270184Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"make_mi_scores(X_train, y_train)","metadata":{"execution":{"iopub.status.busy":"2022-08-06T17:40:50.273628Z","iopub.execute_input":"2022-08-06T17:40:50.274113Z","iopub.status.idle":"2022-08-06T17:40:50.609314Z","shell.execute_reply.started":"2022-08-06T17:40:50.274067Z","shell.execute_reply":"2022-08-06T17:40:50.608239Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = pd.get_dummies(X_train)\nX_train","metadata":{"execution":{"iopub.status.busy":"2022-08-06T17:40:50.611101Z","iopub.execute_input":"2022-08-06T17:40:50.611439Z","iopub.status.idle":"2022-08-06T17:40:50.658500Z","shell.execute_reply.started":"2022-08-06T17:40:50.611407Z","shell.execute_reply":"2022-08-06T17:40:50.657399Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test = pd.get_dummies(X_test)\nX_test","metadata":{"execution":{"iopub.status.busy":"2022-08-06T17:40:50.660065Z","iopub.execute_input":"2022-08-06T17:40:50.660945Z","iopub.status.idle":"2022-08-06T17:40:50.703852Z","shell.execute_reply.started":"2022-08-06T17:40:50.660904Z","shell.execute_reply":"2022-08-06T17:40:50.702636Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Random Forest","metadata":{}},{"cell_type":"code","source":"from sklearn.model_selection import KFold, cross_val_score\n\nfrom sklearn.ensemble import RandomForestClassifier\n\nbest_parameters = 0, 0\nbest_score = 0\n\nkf = KFold(n_splits=5, shuffle=True, random_state=42)\nfor k in range(100, 500, 100):\n    for d in range(5, 20, 5):\n        print(f'Number of trees = {k}, max depth = {d}')    \n        clf = RandomForestClassifier(n_estimators=k, max_depth=d, random_state=42)\n        score = round(cross_val_score(clf, X_train, y_train, cv = kf, scoring='accuracy').mean(), 3)\n        print(f'Accuracy = {score}')\n        if score > best_score:\n            best_score = score\n            best_parameters = k, d  \n        ","metadata":{"execution":{"iopub.status.busy":"2022-08-06T17:40:50.705388Z","iopub.execute_input":"2022-08-06T17:40:50.706234Z","iopub.status.idle":"2022-08-06T17:42:40.254971Z","shell.execute_reply.started":"2022-08-06T17:40:50.706198Z","shell.execute_reply":"2022-08-06T17:42:40.253480Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"best_parameters","metadata":{"execution":{"iopub.status.busy":"2022-08-06T17:42:40.257478Z","iopub.execute_input":"2022-08-06T17:42:40.258281Z","iopub.status.idle":"2022-08-06T17:42:40.266321Z","shell.execute_reply.started":"2022-08-06T17:42:40.258228Z","shell.execute_reply":"2022-08-06T17:42:40.264909Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = RandomForestClassifier(n_estimators=best_parameters[0], max_depth=best_parameters[1], random_state=42)\nmodel.fit(X_train, y_train)\npredictions = model.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2022-08-06T17:42:40.267701Z","iopub.execute_input":"2022-08-06T17:42:40.269051Z","iopub.status.idle":"2022-08-06T17:42:41.282206Z","shell.execute_reply.started":"2022-08-06T17:42:40.268989Z","shell.execute_reply":"2022-08-06T17:42:41.280747Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output = pd.DataFrame({'PassengerId': X_test.index, 'Transported': predictions})\noutput.to_csv('submission_rf.csv', index=False)\nprint(\"Your submission was successfully saved!\")","metadata":{"execution":{"iopub.status.busy":"2022-08-06T17:42:41.284229Z","iopub.execute_input":"2022-08-06T17:42:41.284855Z","iopub.status.idle":"2022-08-06T17:42:41.302971Z","shell.execute_reply.started":"2022-08-06T17:42:41.284791Z","shell.execute_reply":"2022-08-06T17:42:41.301661Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output","metadata":{"execution":{"iopub.status.busy":"2022-08-06T17:42:41.305091Z","iopub.execute_input":"2022-08-06T17:42:41.305910Z","iopub.status.idle":"2022-08-06T17:42:41.323845Z","shell.execute_reply.started":"2022-08-06T17:42:41.305858Z","shell.execute_reply":"2022-08-06T17:42:41.322754Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# XGB \n","metadata":{}},{"cell_type":"code","source":"from xgboost import XGBClassifier\n\nbest_score = 0\nbest_k = 0\n\nfor k in range(100, 1000, 100):\n    print(f'{k} models')\n    clf = XGBClassifier(n_estimators=k, learning_rate=0.05, n_jobs=4)\n    clf.fit(X_train, y_train)\n    score = cross_val_score(clf, X_train, y_train, cv=kf, scoring='accuracy').mean()\n    print(f'{k}: Accuracy = {score}')\n    if score > best_score:\n        best_score = score\n        best_k = k","metadata":{"execution":{"iopub.status.busy":"2022-08-06T17:42:41.325577Z","iopub.execute_input":"2022-08-06T17:42:41.326565Z","iopub.status.idle":"2022-08-06T17:46:09.159158Z","shell.execute_reply.started":"2022-08-06T17:42:41.326509Z","shell.execute_reply":"2022-08-06T17:46:09.158097Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"best_score, best_k","metadata":{"execution":{"iopub.status.busy":"2022-08-06T17:46:09.163087Z","iopub.execute_input":"2022-08-06T17:46:09.163894Z","iopub.status.idle":"2022-08-06T17:46:09.173419Z","shell.execute_reply.started":"2022-08-06T17:46:09.163853Z","shell.execute_reply":"2022-08-06T17:46:09.172513Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"clf = XGBClassifier(n_estimators=best_k, learning_rate=0.05, n_jobs=4)\nclf.fit(X_train, y_train)\npredictions = clf.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2022-08-06T17:46:09.174612Z","iopub.execute_input":"2022-08-06T17:46:09.175017Z","iopub.status.idle":"2022-08-06T17:46:10.116966Z","shell.execute_reply.started":"2022-08-06T17:46:09.174983Z","shell.execute_reply":"2022-08-06T17:46:10.115926Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output = pd.DataFrame({'PassengerId': X_test.index, 'Transported': predictions.astype(bool)})\noutput.to_csv('submission_xgb.csv', index=False)\nprint(\"Your submission was successfully saved!\")","metadata":{"execution":{"iopub.status.busy":"2022-08-06T17:46:10.118785Z","iopub.execute_input":"2022-08-06T17:46:10.119128Z","iopub.status.idle":"2022-08-06T17:46:10.135935Z","shell.execute_reply.started":"2022-08-06T17:46:10.119097Z","shell.execute_reply":"2022-08-06T17:46:10.134895Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output","metadata":{"execution":{"iopub.status.busy":"2022-08-06T17:46:10.137822Z","iopub.execute_input":"2022-08-06T17:46:10.138625Z","iopub.status.idle":"2022-08-06T17:46:10.152334Z","shell.execute_reply.started":"2022-08-06T17:46:10.138576Z","shell.execute_reply":"2022-08-06T17:46:10.151376Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Logistic Regression","metadata":{}},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler\nscaler = StandardScaler()\nX_train_scaled = scaler.fit_transform(X_train)\nX_train_scaled","metadata":{"execution":{"iopub.status.busy":"2022-08-06T17:46:10.153946Z","iopub.execute_input":"2022-08-06T17:46:10.154560Z","iopub.status.idle":"2022-08-06T17:46:10.200677Z","shell.execute_reply.started":"2022-08-06T17:46:10.154529Z","shell.execute_reply":"2022-08-06T17:46:10.199269Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test_scaled = scaler.transform(X_test)\nX_test_scaled","metadata":{"execution":{"iopub.status.busy":"2022-08-06T17:46:10.204672Z","iopub.execute_input":"2022-08-06T17:46:10.205038Z","iopub.status.idle":"2022-08-06T17:46:10.226356Z","shell.execute_reply.started":"2022-08-06T17:46:10.205005Z","shell.execute_reply":"2022-08-06T17:46:10.225193Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.linear_model import LogisticRegression\nbest_coeff = 0\nbest_score = 0\nfor reg_coeff in [0.1, 1, 5, 10, 20, 50, 100, 500, 1000]:\n    print(f'Regularization coefficient = {reg_coeff}')    \n    clf = LogisticRegression(penalty='l2', C=reg_coeff, random_state=42)\n    score = round(cross_val_score(clf, X_train_scaled, y_train, cv = kf, scoring='accuracy').mean(), 3)\n    print(f'Accuracy = {score}')\n    if score > best_score:\n        best_score, best_coeff = score, reg_coeff","metadata":{"execution":{"iopub.status.busy":"2022-08-06T17:46:10.227662Z","iopub.execute_input":"2022-08-06T17:46:10.228008Z","iopub.status.idle":"2022-08-06T17:46:12.812666Z","shell.execute_reply.started":"2022-08-06T17:46:10.227976Z","shell.execute_reply":"2022-08-06T17:46:12.811134Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"best_score, best_coeff","metadata":{"execution":{"iopub.status.busy":"2022-08-06T17:46:12.820433Z","iopub.execute_input":"2022-08-06T17:46:12.824587Z","iopub.status.idle":"2022-08-06T17:46:12.841047Z","shell.execute_reply.started":"2022-08-06T17:46:12.824517Z","shell.execute_reply":"2022-08-06T17:46:12.838678Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"clf = LogisticRegression(penalty='l2', C=best_coeff, random_state=42)\nclf.fit(X_train_scaled, y_train)\npredictions = clf.predict(X_test_scaled)\n","metadata":{"execution":{"iopub.status.busy":"2022-08-06T17:46:12.846759Z","iopub.execute_input":"2022-08-06T17:46:12.851944Z","iopub.status.idle":"2022-08-06T17:46:12.918044Z","shell.execute_reply.started":"2022-08-06T17:46:12.851878Z","shell.execute_reply":"2022-08-06T17:46:12.916341Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output = pd.DataFrame({'PassengerId': X_test.index, 'Transported': predictions})\noutput.to_csv('submission_log.csv', index=False)\nprint(\"Your submission was successfully saved!\")","metadata":{"execution":{"iopub.status.busy":"2022-08-06T17:46:12.926300Z","iopub.execute_input":"2022-08-06T17:46:12.930394Z","iopub.status.idle":"2022-08-06T17:46:12.967355Z","shell.execute_reply.started":"2022-08-06T17:46:12.930324Z","shell.execute_reply":"2022-08-06T17:46:12.965787Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output","metadata":{"execution":{"iopub.status.busy":"2022-08-06T17:46:12.974036Z","iopub.execute_input":"2022-08-06T17:46:12.975002Z","iopub.status.idle":"2022-08-06T17:46:13.007456Z","shell.execute_reply.started":"2022-08-06T17:46:12.974927Z","shell.execute_reply":"2022-08-06T17:46:13.005872Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# KNN","metadata":{}},{"cell_type":"code","source":"from sklearn.neighbors import KNeighborsClassifier\n\nbest_score = 0\nbest_k = 0\nM = 50\nfor k in range(1, M + 1):\n    clf = KNeighborsClassifier(n_neighbors=k)\n    score = cross_val_score(clf, X_train_scaled, y_train, cv=kf, scoring='accuracy').mean()\n    print(f'{k}: Accuracy = {score}') \n    if score > best_score:\n        best_score = score\n        best_k = k","metadata":{"execution":{"iopub.status.busy":"2022-08-06T17:46:13.015072Z","iopub.execute_input":"2022-08-06T17:46:13.018971Z","iopub.status.idle":"2022-08-06T17:47:42.352750Z","shell.execute_reply.started":"2022-08-06T17:46:13.018901Z","shell.execute_reply":"2022-08-06T17:47:42.351421Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"best_score, best_k","metadata":{"execution":{"iopub.status.busy":"2022-08-06T17:47:42.354302Z","iopub.execute_input":"2022-08-06T17:47:42.354769Z","iopub.status.idle":"2022-08-06T17:47:42.365201Z","shell.execute_reply.started":"2022-08-06T17:47:42.354721Z","shell.execute_reply":"2022-08-06T17:47:42.364349Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"clf = KNeighborsClassifier(n_neighbors=best_k)\nclf.fit(X_train_scaled, y_train)\npredictions = clf.predict(X_test_scaled)","metadata":{"execution":{"iopub.status.busy":"2022-08-06T17:47:42.366653Z","iopub.execute_input":"2022-08-06T17:47:42.367481Z","iopub.status.idle":"2022-08-06T17:47:43.330441Z","shell.execute_reply.started":"2022-08-06T17:47:42.367437Z","shell.execute_reply":"2022-08-06T17:47:43.329281Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output = pd.DataFrame({'PassengerId': X_test.index, 'Transported': predictions})\noutput.to_csv('submission_knn.csv', index=False)\nprint(\"Your submission was successfully saved!\")","metadata":{"execution":{"iopub.status.busy":"2022-08-06T17:47:43.331733Z","iopub.execute_input":"2022-08-06T17:47:43.332088Z","iopub.status.idle":"2022-08-06T17:47:43.345654Z","shell.execute_reply.started":"2022-08-06T17:47:43.332055Z","shell.execute_reply":"2022-08-06T17:47:43.344454Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output","metadata":{"execution":{"iopub.status.busy":"2022-08-06T17:47:43.347421Z","iopub.execute_input":"2022-08-06T17:47:43.347902Z","iopub.status.idle":"2022-08-06T17:47:43.361835Z","shell.execute_reply.started":"2022-08-06T17:47:43.347869Z","shell.execute_reply":"2022-08-06T17:47:43.360530Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# SVC","metadata":{}},{"cell_type":"code","source":"from sklearn.svm import SVC\n\nfor g in ['auto', 'scale']:\n    clf = SVC(gamma=g)\n    clf.fit(X_train, y_train)\n    score = cross_val_score(clf, X_train_scaled, y_train, cv=kf, scoring='accuracy').mean()\n    print(f'{g}: Accuracy = {score}')","metadata":{"execution":{"iopub.status.busy":"2022-08-06T17:47:43.363776Z","iopub.execute_input":"2022-08-06T17:47:43.364119Z","iopub.status.idle":"2022-08-06T17:48:19.891814Z","shell.execute_reply.started":"2022-08-06T17:47:43.364088Z","shell.execute_reply":"2022-08-06T17:48:19.890120Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"clf = SVC()\nclf.fit(X_train_scaled, y_train)\npredictions = clf.predict(X_test_scaled)","metadata":{"execution":{"iopub.status.busy":"2022-08-06T17:48:19.894036Z","iopub.execute_input":"2022-08-06T17:48:19.894541Z","iopub.status.idle":"2022-08-06T17:48:24.834071Z","shell.execute_reply.started":"2022-08-06T17:48:19.894493Z","shell.execute_reply":"2022-08-06T17:48:24.833041Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output = pd.DataFrame({'PassengerId': X_test.index, 'Transported': predictions})\noutput.to_csv('submission_svc.csv', index=False)\nprint(\"Your submission was successfully saved!\")","metadata":{"execution":{"iopub.status.busy":"2022-08-06T17:48:24.835156Z","iopub.execute_input":"2022-08-06T17:48:24.835462Z","iopub.status.idle":"2022-08-06T17:48:24.850143Z","shell.execute_reply.started":"2022-08-06T17:48:24.835434Z","shell.execute_reply":"2022-08-06T17:48:24.848781Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output","metadata":{"execution":{"iopub.status.busy":"2022-08-06T17:48:24.851584Z","iopub.execute_input":"2022-08-06T17:48:24.852454Z","iopub.status.idle":"2022-08-06T17:48:24.867579Z","shell.execute_reply.started":"2022-08-06T17:48:24.852416Z","shell.execute_reply":"2022-08-06T17:48:24.866436Z"},"trusted":true},"execution_count":null,"outputs":[]}]}