{"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-05T12:51:19.422661Z","iopub.execute_input":"2022-07-05T12:51:19.424098Z","iopub.status.idle":"2022-07-05T12:51:19.432920Z","shell.execute_reply.started":"2022-07-05T12:51:19.424030Z","shell.execute_reply":"2022-07-05T12:51:19.431687Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TOP = os.getcwd().replace('working', '')\ninput_dir = TOP + '/input/titanic/'","metadata":{"execution":{"iopub.status.busy":"2022-07-05T12:51:22.438010Z","iopub.execute_input":"2022-07-05T12:51:22.439634Z","iopub.status.idle":"2022-07-05T12:51:22.446181Z","shell.execute_reply.started":"2022-07-05T12:51:22.439567Z","shell.execute_reply":"2022-07-05T12:51:22.444725Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Reading in input file to see shape and size of the training set.","metadata":{}},{"cell_type":"code","source":"train_df = pd.read_csv(input_dir+'train.csv')","metadata":{"execution":{"iopub.status.busy":"2022-07-05T12:51:25.821210Z","iopub.execute_input":"2022-07-05T12:51:25.821610Z","iopub.status.idle":"2022-07-05T12:51:25.845621Z","shell.execute_reply.started":"2022-07-05T12:51:25.821569Z","shell.execute_reply":"2022-07-05T12:51:25.844544Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-05T12:51:28.489752Z","iopub.execute_input":"2022-07-05T12:51:28.491197Z","iopub.status.idle":"2022-07-05T12:51:28.505341Z","shell.execute_reply.started":"2022-07-05T12:51:28.491135Z","shell.execute_reply":"2022-07-05T12:51:28.503698Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T12:51:30.542330Z","iopub.execute_input":"2022-07-05T12:51:30.543739Z","iopub.status.idle":"2022-07-05T12:51:30.580859Z","shell.execute_reply.started":"2022-07-05T12:51:30.543688Z","shell.execute_reply":"2022-07-05T12:51:30.579474Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Looks like we have a number of missing values for Age, Cabin and Embarked. The latter might be easier to imput missing values than the others.","metadata":{}},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"markdown","source":"View the first few lines. ","metadata":{}},{"cell_type":"code","source":"train_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T12:51:34.903315Z","iopub.execute_input":"2022-07-05T12:51:34.904831Z","iopub.status.idle":"2022-07-05T12:51:34.942932Z","shell.execute_reply.started":"2022-07-05T12:51:34.904760Z","shell.execute_reply":"2022-07-05T12:51:34.941629Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport seaborn as sns\n%matplotlib inline","metadata":{"execution":{"iopub.status.busy":"2022-07-05T12:51:39.027483Z","iopub.execute_input":"2022-07-05T12:51:39.028064Z","iopub.status.idle":"2022-07-05T12:51:39.711181Z","shell.execute_reply.started":"2022-07-05T12:51:39.028016Z","shell.execute_reply":"2022-07-05T12:51:39.709644Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.Survived.value_counts().plot.bar()\nplt.xticks([0,1], ['Perished', 'Survived'], rotation = 45);\n","metadata":{"execution":{"iopub.status.busy":"2022-07-05T12:51:40.987942Z","iopub.execute_input":"2022-07-05T12:51:40.988360Z","iopub.status.idle":"2022-07-05T12:51:41.213120Z","shell.execute_reply.started":"2022-07-05T12:51:40.988329Z","shell.execute_reply":"2022-07-05T12:51:41.211912Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.Survived.value_counts(normalize = True, dropna = False)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T12:51:44.252883Z","iopub.execute_input":"2022-07-05T12:51:44.253396Z","iopub.status.idle":"2022-07-05T12:51:44.267463Z","shell.execute_reply.started":"2022-07-05T12:51:44.253358Z","shell.execute_reply":"2022-07-05T12:51:44.266088Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Somewhat imbalanced set with ~38% survived to 62% perished. Using balanced accuracy will be a better metric in lieu of accuracy.","metadata":{}},{"cell_type":"code","source":"train_df.PassengerId.nunique()\ntrain_df.shape[0]","metadata":{"execution":{"iopub.status.busy":"2022-07-05T12:51:47.041748Z","iopub.execute_input":"2022-07-05T12:51:47.042248Z","iopub.status.idle":"2022-07-05T12:51:47.051033Z","shell.execute_reply.started":"2022-07-05T12:51:47.042195Z","shell.execute_reply":"2022-07-05T12:51:47.049932Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"891 unique passengers - no duplicates.","metadata":{}},{"cell_type":"code","source":"sns.pairplot(train_df, hue = 'Survived')","metadata":{"execution":{"iopub.status.busy":"2022-07-05T12:51:51.382315Z","iopub.execute_input":"2022-07-05T12:51:51.382764Z","iopub.status.idle":"2022-07-05T12:52:01.808203Z","shell.execute_reply.started":"2022-07-05T12:51:51.382721Z","shell.execute_reply":"2022-07-05T12:52:01.806464Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"markdown","source":"More people from Pclass 1 tended to survive.","metadata":{}},{"cell_type":"code","source":"women = train_df.loc[train_df.Sex == 'female'][\"Survived\"]\nrate_women = sum(women)/len(women)\n\nprint(\"% of women who survived:\", rate_women)\n","metadata":{"execution":{"iopub.status.busy":"2022-07-05T12:52:01.810718Z","iopub.execute_input":"2022-07-05T12:52:01.812027Z","iopub.status.idle":"2022-07-05T12:52:01.823605Z","shell.execute_reply.started":"2022-07-05T12:52:01.811976Z","shell.execute_reply":"2022-07-05T12:52:01.822208Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"men = train_df.loc[train_df.Sex == 'male'][\"Survived\"]\nrate_men = sum(men)/len(men)\n\nprint(\"% of men who survived:\", rate_men)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T12:52:01.826104Z","iopub.execute_input":"2022-07-05T12:52:01.827130Z","iopub.status.idle":"2022-07-05T12:52:01.838732Z","shell.execute_reply.started":"2022-07-05T12:52:01.827058Z","shell.execute_reply":"2022-07-05T12:52:01.837518Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" \npclass = train_df.loc[train_df.Pclass == 1][\"Survived\"]\nrate_pclass1 = sum(pclass)/len(pclass)\n\nprint(\"% of 1st class passengers who survived:\", rate_pclass1)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T12:52:01.840872Z","iopub.execute_input":"2022-07-05T12:52:01.841514Z","iopub.status.idle":"2022-07-05T12:52:01.878389Z","shell.execute_reply.started":"2022-07-05T12:52:01.841466Z","shell.execute_reply":"2022-07-05T12:52:01.875406Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pclass_female = train_df.loc[(train_df.Pclass == 1) & (train_df.Sex == 'female')][\"Survived\"]\nrate_pclass1_female = sum(pclass_female)/len(pclass_female)\n\nprint(\"% of 1st class female passengers who survived:\", rate_pclass1_female)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T12:52:01.881053Z","iopub.execute_input":"2022-07-05T12:52:01.882433Z","iopub.status.idle":"2022-07-05T12:52:01.930564Z","shell.execute_reply.started":"2022-07-05T12:52:01.882372Z","shell.execute_reply":"2022-07-05T12:52:01.928939Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pclass_female = train_df.loc[(train_df.Pclass == 3) & (train_df.Sex == 'female')][\"Survived\"]\nrate_pclass3_female = sum(pclass_female)/len(pclass_female)\n\nprint(\"% of 3rd class female passengers who survived:\", rate_pclass3_female)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T12:52:04.412280Z","iopub.execute_input":"2022-07-05T12:52:04.412669Z","iopub.status.idle":"2022-07-05T12:52:04.422465Z","shell.execute_reply.started":"2022-07-05T12:52:04.412638Z","shell.execute_reply":"2022-07-05T12:52:04.421041Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pclass_male = train_df.loc[(train_df.Pclass == 3) & (train_df.Sex == 'male')][\"Survived\"]\nrate_pclass3_male = sum(pclass_male)/len(pclass_male)\n\nprint(\"% of 3rd class male passengers who survived:\", rate_pclass3_male)","metadata":{"execution":{"iopub.status.busy":"2022-07-04T19:05:19.905734Z","iopub.execute_input":"2022-07-04T19:05:19.906542Z","iopub.status.idle":"2022-07-04T19:05:19.915015Z","shell.execute_reply.started":"2022-07-04T19:05:19.906507Z","shell.execute_reply":"2022-07-04T19:05:19.913783Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pclass_male = train_df.loc[(train_df.Pclass == 1) & (train_df.Sex == 'male') ][\"Survived\"]\nrate_pclass1_male = sum(pclass_male)/len(pclass_male)\n\nprint(\"% of 1st class male passengers who survived:\", rate_pclass1_male)","metadata":{"execution":{"iopub.status.busy":"2022-07-04T19:05:30.661725Z","iopub.execute_input":"2022-07-04T19:05:30.662784Z","iopub.status.idle":"2022-07-04T19:05:30.671768Z","shell.execute_reply.started":"2022-07-04T19:05:30.662729Z","shell.execute_reply":"2022-07-04T19:05:30.670548Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"97% of all female 1st class passengers survived but only 50% of class 3 females. Male 1st class passengers were 3 times as likely to survive as their class 3 counterparts though this does not discriminate for age.","metadata":{}},{"cell_type":"markdown","source":"What's the age distribution amongst male 1st passengers that survived?","metadata":{}},{"cell_type":"code","source":"train_df.loc[(train_df.Pclass == 1) & (train_df.Sex == 'male') & (train_df.Survived == 1)][\"Age\"].plot(kind = 'hist')","metadata":{"execution":{"iopub.status.busy":"2022-07-05T12:52:11.250513Z","iopub.execute_input":"2022-07-05T12:52:11.250946Z","iopub.status.idle":"2022-07-05T12:52:11.469258Z","shell.execute_reply.started":"2022-07-05T12:52:11.250902Z","shell.execute_reply":"2022-07-05T12:52:11.468361Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.loc[(train_df.Pclass == 1) & (train_df.Sex == 'male') & (train_df.Survived == 1)][\"Age\"].describe()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T12:52:15.386814Z","iopub.execute_input":"2022-07-05T12:52:15.387289Z","iopub.status.idle":"2022-07-05T12:52:15.406155Z","shell.execute_reply.started":"2022-07-05T12:52:15.387254Z","shell.execute_reply":"2022-07-05T12:52:15.404536Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.loc[(train_df.Pclass == 1) & (train_df.Sex == 'female') & (train_df.Survived == 1)][\"Age\"].plot(kind = 'hist')","metadata":{"execution":{"iopub.status.busy":"2022-07-05T12:52:18.541542Z","iopub.execute_input":"2022-07-05T12:52:18.541954Z","iopub.status.idle":"2022-07-05T12:52:18.748387Z","shell.execute_reply.started":"2022-07-05T12:52:18.541922Z","shell.execute_reply":"2022-07-05T12:52:18.747047Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Women and girls spanned a different range of ages than the men. From 14 years to 63 whereas men spanned ages from less than a year to 80. Something funky about Age? - suggests few children were in 1st class relative to the other classes.","metadata":{}},{"cell_type":"code","source":"train_df.loc[(train_df.Pclass == 1) & (train_df.Sex == 'female') & (train_df.Survived == 1)][\"Age\"].describe()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T12:52:23.140428Z","iopub.execute_input":"2022-07-05T12:52:23.140854Z","iopub.status.idle":"2022-07-05T12:52:23.156696Z","shell.execute_reply.started":"2022-07-05T12:52:23.140823Z","shell.execute_reply":"2022-07-05T12:52:23.155420Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.histplot(data = train_df, x = 'Age', hue = 'Pclass', bins = 20)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T12:52:25.092245Z","iopub.execute_input":"2022-07-05T12:52:25.092729Z","iopub.status.idle":"2022-07-05T12:52:25.499081Z","shell.execute_reply.started":"2022-07-05T12:52:25.092689Z","shell.execute_reply":"2022-07-05T12:52:25.497560Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Most children are in Pclass 2 and 3 and it appears they tended to survive.","metadata":{}},{"cell_type":"code","source":"sns.histplot(data = train_df, x = 'Age', hue = 'Survived', bins = 20)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T12:52:28.637393Z","iopub.execute_input":"2022-07-05T12:52:28.637883Z","iopub.status.idle":"2022-07-05T12:52:28.986211Z","shell.execute_reply.started":"2022-07-05T12:52:28.637841Z","shell.execute_reply":"2022-07-05T12:52:28.983955Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"children = train_df.loc[train_df.Age < 10][\"Survived\"]\nrate_child = sum(children)/len(children)\n\nprint(\"% of children who survived:\", rate_child)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T12:52:32.359254Z","iopub.execute_input":"2022-07-05T12:52:32.360760Z","iopub.status.idle":"2022-07-05T12:52:32.369148Z","shell.execute_reply.started":"2022-07-05T12:52:32.360710Z","shell.execute_reply":"2022-07-05T12:52:32.367781Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"children = train_df.loc[(train_df.Age < 10) & (train_df.Pclass != 1)][\"Survived\"]\nrate_child = sum(children)/len(children)\n\nprint(\"% of children who survived in classes 2 and 3:\", rate_child)","metadata":{"execution":{"iopub.status.busy":"2022-07-04T19:07:14.811790Z","iopub.execute_input":"2022-07-04T19:07:14.812444Z","iopub.status.idle":"2022-07-04T19:07:14.820649Z","shell.execute_reply.started":"2022-07-04T19:07:14.812397Z","shell.execute_reply":"2022-07-04T19:07:14.819702Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Children regardless of class survived at the same proportion as overall - most children were in the lower class decks. ","metadata":{}},{"cell_type":"code","source":"train_df.loc[(train_df.Pclass == 3) & (train_df.Sex == 'male') & (train_df.Age > 8)][\"Survived\"].value_counts(normalize = True)\n\nprint('Most adult men in 3rd class perished if we account for the children. So the 3 to 1 estimate is generous.')","metadata":{"execution":{"iopub.status.busy":"2022-07-05T12:52:51.306061Z","iopub.execute_input":"2022-07-05T12:52:51.306530Z","iopub.status.idle":"2022-07-05T12:52:51.319254Z","shell.execute_reply.started":"2022-07-05T12:52:51.306494Z","shell.execute_reply":"2022-07-05T12:52:51.317616Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.loc[(train_df.Pclass != 1) & (train_df.Age < 10)][\"Survived\"].value_counts(normalize = True)\n\nprint('61% of children in the other classes survived .')","metadata":{"execution":{"iopub.status.busy":"2022-07-05T12:52:56.556393Z","iopub.execute_input":"2022-07-05T12:52:56.556831Z","iopub.status.idle":"2022-07-05T12:52:56.568092Z","shell.execute_reply.started":"2022-07-05T12:52:56.556797Z","shell.execute_reply":"2022-07-05T12:52:56.566984Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.loc[(train_df.Age < 10)][\"Survived\"].value_counts(normalize = True)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T12:53:00.926194Z","iopub.execute_input":"2022-07-05T12:53:00.926613Z","iopub.status.idle":"2022-07-05T12:53:00.938558Z","shell.execute_reply.started":"2022-07-05T12:53:00.926579Z","shell.execute_reply":"2022-07-05T12:53:00.937485Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Factors that really influence survivability - age - children or a combination of sex and class. ","metadata":{}},{"cell_type":"code","source":"train_df.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T12:53:03.642214Z","iopub.execute_input":"2022-07-05T12:53:03.642629Z","iopub.status.idle":"2022-07-05T12:53:03.659901Z","shell.execute_reply.started":"2022-07-05T12:53:03.642597Z","shell.execute_reply":"2022-07-05T12:53:03.658744Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T12:53:06.143041Z","iopub.execute_input":"2022-07-05T12:53:06.144054Z","iopub.status.idle":"2022-07-05T12:53:06.161251Z","shell.execute_reply.started":"2022-07-05T12:53:06.144013Z","shell.execute_reply":"2022-07-05T12:53:06.160031Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Fare and Class of cabin are correlated. Could potentially drop one of these.\n","metadata":{}},{"cell_type":"code","source":"sns.histplot(data = train_df, x = 'Fare', hue = 'Pclass', bins = 20)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T12:53:08.531470Z","iopub.execute_input":"2022-07-05T12:53:08.531875Z","iopub.status.idle":"2022-07-05T12:53:09.141807Z","shell.execute_reply.started":"2022-07-05T12:53:08.531843Z","shell.execute_reply":"2022-07-05T12:53:09.140310Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import cross_validate, train_test_split\nfrom sklearn.pipeline import make_pipeline\nfrom sklearn.ensemble import RandomForestClassifier\nfrom sklearn.pipeline import Pipeline\nfrom sklearn.preprocessing import StandardScaler, OrdinalEncoder\nfrom sklearn.dummy import DummyClassifier\nfrom sklearn.metrics import balanced_accuracy_score, f1_score, make_scorer, roc_auc_score\nfrom sklearn.metrics import auc\nfrom sklearn.compose import make_column_selector as selector","metadata":{"execution":{"iopub.status.busy":"2022-07-05T13:03:41.005742Z","iopub.execute_input":"2022-07-05T13:03:41.006311Z","iopub.status.idle":"2022-07-05T13:03:41.015699Z","shell.execute_reply.started":"2022-07-05T13:03:41.006270Z","shell.execute_reply":"2022-07-05T13:03:41.014548Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn import set_config\nset_config(display='diagram')","metadata":{"execution":{"iopub.status.busy":"2022-07-05T12:53:13.895456Z","iopub.execute_input":"2022-07-05T12:53:13.895983Z","iopub.status.idle":"2022-07-05T12:53:13.902089Z","shell.execute_reply.started":"2022-07-05T12:53:13.895944Z","shell.execute_reply":"2022-07-05T12:53:13.900716Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.columns","metadata":{"execution":{"iopub.status.busy":"2022-07-05T12:53:16.612636Z","iopub.execute_input":"2022-07-05T12:53:16.613035Z","iopub.status.idle":"2022-07-05T12:53:16.622021Z","shell.execute_reply.started":"2022-07-05T12:53:16.613004Z","shell.execute_reply":"2022-07-05T12:53:16.620817Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = train_df.set_index('PassengerId')","metadata":{"execution":{"iopub.status.busy":"2022-07-05T12:53:18.538037Z","iopub.execute_input":"2022-07-05T12:53:18.538452Z","iopub.status.idle":"2022-07-05T12:53:18.547480Z","shell.execute_reply.started":"2022-07-05T12:53:18.538419Z","shell.execute_reply":"2022-07-05T12:53:18.545680Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y = data['Survived']","metadata":{"execution":{"iopub.status.busy":"2022-07-05T12:53:20.315721Z","iopub.execute_input":"2022-07-05T12:53:20.316138Z","iopub.status.idle":"2022-07-05T12:53:20.322360Z","shell.execute_reply.started":"2022-07-05T12:53:20.316105Z","shell.execute_reply":"2022-07-05T12:53:20.321034Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = data.drop(['Survived'], axis = 1)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T12:53:23.916296Z","iopub.execute_input":"2022-07-05T12:53:23.916696Z","iopub.status.idle":"2022-07-05T12:53:23.924593Z","shell.execute_reply.started":"2022-07-05T12:53:23.916664Z","shell.execute_reply":"2022-07-05T12:53:23.923464Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X.columns","metadata":{"execution":{"iopub.status.busy":"2022-07-05T12:53:35.221722Z","iopub.execute_input":"2022-07-05T12:53:35.222183Z","iopub.status.idle":"2022-07-05T12:53:35.231678Z","shell.execute_reply.started":"2022-07-05T12:53:35.222148Z","shell.execute_reply":"2022-07-05T12:53:35.230337Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X['child']  = X['Age'].apply(lambda x: 1 if x < 10 else 0)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T12:54:05.828169Z","iopub.execute_input":"2022-07-05T12:54:05.828657Z","iopub.status.idle":"2022-07-05T12:54:05.837984Z","shell.execute_reply.started":"2022-07-05T12:54:05.828623Z","shell.execute_reply":"2022-07-05T12:54:05.836549Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X.columns","metadata":{"execution":{"iopub.status.busy":"2022-07-05T12:54:26.582180Z","iopub.execute_input":"2022-07-05T12:54:26.582598Z","iopub.status.idle":"2022-07-05T12:54:26.590882Z","shell.execute_reply.started":"2022-07-05T12:54:26.582548Z","shell.execute_reply":"2022-07-05T12:54:26.589314Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = X.drop(['Name', 'Ticket','Age', 'Cabin'], axis = 1)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T12:55:02.593181Z","iopub.execute_input":"2022-07-05T12:55:02.593696Z","iopub.status.idle":"2022-07-05T12:55:02.602145Z","shell.execute_reply.started":"2022-07-05T12:55:02.593644Z","shell.execute_reply":"2022-07-05T12:55:02.601106Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, X_test, y_train, y_test = train_test_split(X, y, stratify = y, test_size = 0.2, random_state = 42)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T12:55:04.735469Z","iopub.execute_input":"2022-07-05T12:55:04.735908Z","iopub.status.idle":"2022-07-05T12:55:04.747289Z","shell.execute_reply.started":"2022-07-05T12:55:04.735873Z","shell.execute_reply":"2022-07-05T12:55:04.746283Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(X_train.shape, X_test.shape, y_train.shape, y_test.shape)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T12:55:07.405647Z","iopub.execute_input":"2022-07-05T12:55:07.406129Z","iopub.status.idle":"2022-07-05T12:55:07.414560Z","shell.execute_reply.started":"2022-07-05T12:55:07.406094Z","shell.execute_reply":"2022-07-05T12:55:07.412682Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T12:55:13.512982Z","iopub.execute_input":"2022-07-05T12:55:13.513704Z","iopub.status.idle":"2022-07-05T12:55:13.537749Z","shell.execute_reply.started":"2022-07-05T12:55:13.513590Z","shell.execute_reply":"2022-07-05T12:55:13.536450Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for col in X_train.columns:\n    if col in ['Pclass', 'child', 'SibSp','Parch', 'Embarked']:\n        X_train[col] = X_train[col].astype(object)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T12:55:58.112128Z","iopub.execute_input":"2022-07-05T12:55:58.112644Z","iopub.status.idle":"2022-07-05T12:55:58.124116Z","shell.execute_reply.started":"2022-07-05T12:55:58.112604Z","shell.execute_reply":"2022-07-05T12:55:58.122421Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"categorical_columns_selector = selector(dtype_include = object)\ncategorical_columns = categorical_columns_selector(X_train)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T12:56:00.293321Z","iopub.execute_input":"2022-07-05T12:56:00.293781Z","iopub.status.idle":"2022-07-05T12:56:00.303553Z","shell.execute_reply.started":"2022-07-05T12:56:00.293746Z","shell.execute_reply":"2022-07-05T12:56:00.302478Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"categorical_columns","metadata":{"execution":{"iopub.status.busy":"2022-07-05T12:56:02.387272Z","iopub.execute_input":"2022-07-05T12:56:02.387668Z","iopub.status.idle":"2022-07-05T12:56:02.395037Z","shell.execute_reply.started":"2022-07-05T12:56:02.387636Z","shell.execute_reply":"2022-07-05T12:56:02.393848Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"numerical_columns_selector = selector(dtype_exclude=object)\nnumerical_columns = numerical_columns_selector(X_train)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T12:56:25.957988Z","iopub.execute_input":"2022-07-05T12:56:25.958420Z","iopub.status.idle":"2022-07-05T12:56:25.966138Z","shell.execute_reply.started":"2022-07-05T12:56:25.958377Z","shell.execute_reply":"2022-07-05T12:56:25.965024Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.pipeline import Pipeline\nfrom sklearn.impute import SimpleImputer\nfrom sklearn.preprocessing import OneHotEncoder, StandardScaler\n\ncategorical_preprocessor = Pipeline(steps = [(\"imputer\", SimpleImputer(strategy = \"most_frequent\")), (\"encoder\", OneHotEncoder(handle_unknown=\"ignore\"))])\nnumerical_preprocessor = StandardScaler()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T13:20:30.212668Z","iopub.execute_input":"2022-07-05T13:20:30.213196Z","iopub.status.idle":"2022-07-05T13:20:30.220900Z","shell.execute_reply.started":"2022-07-05T13:20:30.213154Z","shell.execute_reply":"2022-07-05T13:20:30.219600Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.compose import ColumnTransformer\npreprocessor = ColumnTransformer(\n    transformers=[\n        (\"num\", numerical_preprocessor, numerical_columns),\n        (\"cat\", categorical_preprocessor, categorical_columns),\n    ]\n)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T13:20:32.823938Z","iopub.execute_input":"2022-07-05T13:20:32.824435Z","iopub.status.idle":"2022-07-05T13:20:32.830425Z","shell.execute_reply.started":"2022-07-05T13:20:32.824397Z","shell.execute_reply":"2022-07-05T13:20:32.829451Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.ensemble import RandomForestClassifier\nclf = Pipeline(\n    steps=[(\"preprocessor\", preprocessor), (\"classifier\", RandomForestClassifier(n_estimators = 1000, max_samples = 0.66, random_state = 42))]\n)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T13:25:25.387374Z","iopub.execute_input":"2022-07-05T13:25:25.387852Z","iopub.status.idle":"2022-07-05T13:25:25.394836Z","shell.execute_reply.started":"2022-07-05T13:25:25.387814Z","shell.execute_reply":"2022-07-05T13:25:25.393366Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"clf","metadata":{"execution":{"iopub.status.busy":"2022-07-05T13:25:27.308454Z","iopub.execute_input":"2022-07-05T13:25:27.308908Z","iopub.status.idle":"2022-07-05T13:25:27.366866Z","shell.execute_reply.started":"2022-07-05T13:25:27.308875Z","shell.execute_reply":"2022-07-05T13:25:27.365912Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import balanced_accuracy_score, f1_score, make_scorer, roc_auc_score\nfrom sklearn.metrics import auc\nfrom sklearn.model_selection import StratifiedKFold, cross_validate\ncv = StratifiedKFold(n_splits = 5)\nscorer = make_scorer(balanced_accuracy_score)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T13:25:30.651902Z","iopub.execute_input":"2022-07-05T13:25:30.652336Z","iopub.status.idle":"2022-07-05T13:25:30.658213Z","shell.execute_reply.started":"2022-07-05T13:25:30.652301Z","shell.execute_reply":"2022-07-05T13:25:30.657098Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cv_results = cross_validate(clf, X_train, y_train, cv = 5, scoring = scorer, return_train_score = True)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T13:25:32.929673Z","iopub.execute_input":"2022-07-05T13:25:32.930765Z","iopub.status.idle":"2022-07-05T13:25:47.003119Z","shell.execute_reply.started":"2022-07-05T13:25:32.930706Z","shell.execute_reply":"2022-07-05T13:25:47.001913Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(cv_results['test_score'].mean(), cv_results['test_score'].std())","metadata":{"execution":{"iopub.status.busy":"2022-07-05T13:25:50.327402Z","iopub.execute_input":"2022-07-05T13:25:50.327824Z","iopub.status.idle":"2022-07-05T13:25:50.335038Z","shell.execute_reply.started":"2022-07-05T13:25:50.327791Z","shell.execute_reply":"2022-07-05T13:25:50.333512Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(cv_results['train_score'].mean(), cv_results['train_score'].std())\n","metadata":{"execution":{"iopub.status.busy":"2022-07-05T13:25:52.367385Z","iopub.execute_input":"2022-07-05T13:25:52.368392Z","iopub.status.idle":"2022-07-05T13:25:52.374559Z","shell.execute_reply.started":"2022-07-05T13:25:52.368353Z","shell.execute_reply":"2022-07-05T13:25:52.373114Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cv_results = pd.DataFrame(cv_results)\ncv_results[['test_score', 'train_score']].plot.hist(edgecolor = 'black')\nplt.xlabel('CV Balanced Accuracy')","metadata":{"execution":{"iopub.status.busy":"2022-07-05T13:23:32.115858Z","iopub.execute_input":"2022-07-05T13:23:32.116393Z","iopub.status.idle":"2022-07-05T13:23:32.387547Z","shell.execute_reply.started":"2022-07-05T13:23:32.116354Z","shell.execute_reply":"2022-07-05T13:23:32.386187Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"clf2 = Pipeline(\n    steps=[(\"preprocessor\", preprocessor), (\"classifier\", RandomForestClassifier(max_samples = 0.66, random_state = 42))]\n)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T14:02:23.281949Z","iopub.execute_input":"2022-07-05T14:02:23.282412Z","iopub.status.idle":"2022-07-05T14:02:23.289835Z","shell.execute_reply.started":"2022-07-05T14:02:23.282377Z","shell.execute_reply":"2022-07-05T14:02:23.288302Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"clf2.get_params().keys()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T14:03:25.186182Z","iopub.execute_input":"2022-07-05T14:03:25.187286Z","iopub.status.idle":"2022-07-05T14:03:25.196311Z","shell.execute_reply.started":"2022-07-05T14:03:25.187208Z","shell.execute_reply":"2022-07-05T14:03:25.195300Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import validation_curve\nn_estimators = [1,50, 75, 100, 150,250, 500, 750, 1000]\ntrain_scores, test_scores = validation_curve(\n    clf2, X_train, y_train, param_name=\"classifier__n_estimators\", param_range=n_estimators,\n    cv=cv, scoring=scorer, n_jobs=2)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T14:05:10.168535Z","iopub.execute_input":"2022-07-05T14:05:10.169055Z","iopub.status.idle":"2022-07-05T14:05:31.829194Z","shell.execute_reply.started":"2022-07-05T14:05:10.169019Z","shell.execute_reply":"2022-07-05T14:05:31.827858Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize = (10,6))\nplt.errorbar(n_estimators, train_scores.mean(axis=1),\n             yerr=train_scores.std(axis=1), label='Training score')\nplt.errorbar(n_estimators, test_scores.mean(axis=1),\n             yerr=test_scores.std(axis=1), label='Testing score')\nplt.legend(loc = 'center')\n\nplt.xlabel(\"Number of estimators\")\nplt.ylabel(\"Balanced Accuracy (mean)\")","metadata":{"execution":{"iopub.status.busy":"2022-07-05T14:05:34.992020Z","iopub.execute_input":"2022-07-05T14:05:34.992498Z","iopub.status.idle":"2022-07-05T14:05:35.232743Z","shell.execute_reply.started":"2022-07-05T14:05:34.992462Z","shell.execute_reply":"2022-07-05T14:05:35.231777Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Explored the no of estimators and the impact this had on training and testing balanced accuracies. No need for 1000 estimators as the default. Let's perform a grid search to refine the number of estimators to maximise balanced accuracy.","metadata":{}},{"cell_type":"code","source":"from sklearn.model_selection import GridSearchCV\n\nparam_grid = {\n    \"classifier__n_estimators\": [1, 2, 5, 10, 20, 50, 100, 200],\n    \"classifier__max_leaf_nodes\": [2, 5, 10, 20, 50],\n}","metadata":{"execution":{"iopub.status.busy":"2022-07-05T14:11:03.102881Z","iopub.execute_input":"2022-07-05T14:11:03.103377Z","iopub.status.idle":"2022-07-05T14:11:03.111614Z","shell.execute_reply.started":"2022-07-05T14:11:03.103336Z","shell.execute_reply":"2022-07-05T14:11:03.109748Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_to_tune = RandomForestClassifier(random_state = 42, max_samples = 0.66)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T14:11:04.642917Z","iopub.execute_input":"2022-07-05T14:11:04.643417Z","iopub.status.idle":"2022-07-05T14:11:04.649436Z","shell.execute_reply.started":"2022-07-05T14:11:04.643379Z","shell.execute_reply":"2022-07-05T14:11:04.648416Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"clf3 = Pipeline(\n    steps=[(\"preprocessor\", preprocessor), (\"classifier\", model_to_tune)]\n)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T14:11:05.847377Z","iopub.execute_input":"2022-07-05T14:11:05.847868Z","iopub.status.idle":"2022-07-05T14:11:05.855403Z","shell.execute_reply.started":"2022-07-05T14:11:05.847832Z","shell.execute_reply":"2022-07-05T14:11:05.853774Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"inner_cv = StratifiedKFold(n_splits = 10)\nouter_cv = StratifiedKFold(n_splits = 10)\n","metadata":{"execution":{"iopub.status.busy":"2022-07-05T14:11:07.086772Z","iopub.execute_input":"2022-07-05T14:11:07.088108Z","iopub.status.idle":"2022-07-05T14:11:07.093278Z","shell.execute_reply.started":"2022-07-05T14:11:07.088059Z","shell.execute_reply":"2022-07-05T14:11:07.091767Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = GridSearchCV(\n    estimator=clf3, param_grid=param_grid, cv=inner_cv, scoring = make_scorer(balanced_accuracy_score), n_jobs=-1)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T14:11:08.122302Z","iopub.execute_input":"2022-07-05T14:11:08.123621Z","iopub.status.idle":"2022-07-05T14:11:08.129723Z","shell.execute_reply.started":"2022-07-05T14:11:08.123560Z","shell.execute_reply":"2022-07-05T14:11:08.128610Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cv_results = cross_validate(model, X_train, y_train, cv=outer_cv, scoring = make_scorer(balanced_accuracy_score), n_jobs=-1, return_estimator = True)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T14:11:11.876413Z","iopub.execute_input":"2022-07-05T14:11:11.876871Z","iopub.status.idle":"2022-07-05T14:15:12.790466Z","shell.execute_reply.started":"2022-07-05T14:11:11.876839Z","shell.execute_reply":"2022-07-05T14:15:12.788996Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"After hyperparameter tuning - what does our CV test scores look like. Slightl improvement over the original RFC using default settings.","metadata":{}},{"cell_type":"code","source":"cv_results['test_score'].mean(), cv_results['test_score'].std()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T14:15:19.187955Z","iopub.execute_input":"2022-07-05T14:15:19.188379Z","iopub.status.idle":"2022-07-05T14:15:19.197943Z","shell.execute_reply.started":"2022-07-05T14:15:19.188346Z","shell.execute_reply":"2022-07-05T14:15:19.196823Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for estimator in cv_results['estimator']:\n    print(estimator.best_params_, estimator.best_score_)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T14:17:32.039594Z","iopub.execute_input":"2022-07-05T14:17:32.039995Z","iopub.status.idle":"2022-07-05T14:17:32.046602Z","shell.execute_reply.started":"2022-07-05T14:17:32.039963Z","shell.execute_reply":"2022-07-05T14:17:32.045395Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(X_train, y_train).best_params_","metadata":{"execution":{"iopub.status.busy":"2022-07-05T14:18:04.968070Z","iopub.execute_input":"2022-07-05T14:18:04.968606Z","iopub.status.idle":"2022-07-05T14:18:25.191933Z","shell.execute_reply.started":"2022-07-05T14:18:04.968562Z","shell.execute_reply":"2022-07-05T14:18:25.189979Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_model = Pipeline(\n    steps=[(\"preprocessor\", preprocessor), (\"classifier\", RandomForestClassifier(max_samples = 0.66, n_estimators = 20, max_leaf_nodes = 50, random_state = 42))]\n)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T14:19:55.530627Z","iopub.execute_input":"2022-07-05T14:19:55.531272Z","iopub.status.idle":"2022-07-05T14:19:55.538600Z","shell.execute_reply.started":"2022-07-05T14:19:55.531192Z","shell.execute_reply":"2022-07-05T14:19:55.537172Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_model.fit(X_train, y_train)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T14:20:22.388201Z","iopub.execute_input":"2022-07-05T14:20:22.388742Z","iopub.status.idle":"2022-07-05T14:20:22.547293Z","shell.execute_reply.started":"2022-07-05T14:20:22.388702Z","shell.execute_reply":"2022-07-05T14:20:22.546192Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_preds = final_model.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T14:20:58.837097Z","iopub.execute_input":"2022-07-05T14:20:58.838508Z","iopub.status.idle":"2022-07-05T14:20:58.859135Z","shell.execute_reply.started":"2022-07-05T14:20:58.838460Z","shell.execute_reply":"2022-07-05T14:20:58.858169Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n\nbalanced_accuracy_score(y_test, test_preds)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T14:21:36.643621Z","iopub.execute_input":"2022-07-05T14:21:36.644110Z","iopub.status.idle":"2022-07-05T14:21:36.653617Z","shell.execute_reply.started":"2022-07-05T14:21:36.644075Z","shell.execute_reply":"2022-07-05T14:21:36.652643Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import confusion_matrix, classification_report\nconfusion_matrix(y_test, test_preds)\n","metadata":{"execution":{"iopub.status.busy":"2022-07-05T14:22:13.744458Z","iopub.execute_input":"2022-07-05T14:22:13.744903Z","iopub.status.idle":"2022-07-05T14:22:13.754796Z","shell.execute_reply.started":"2022-07-05T14:22:13.744871Z","shell.execute_reply":"2022-07-05T14:22:13.753180Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(classification_report(y_test, test_preds))","metadata":{"execution":{"iopub.status.busy":"2022-07-05T14:22:36.528742Z","iopub.execute_input":"2022-07-05T14:22:36.529136Z","iopub.status.idle":"2022-07-05T14:22:36.541963Z","shell.execute_reply.started":"2022-07-05T14:22:36.529105Z","shell.execute_reply":"2022-07-05T14:22:36.540329Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"For the test set from this train/test split - balanced accuracy is consistent with that from 10 fold CV.\nLet's input the test set provided and make predictions.","metadata":{}},{"cell_type":"code","source":"test_data = pd.read_csv(input_dir+\"test.csv\")\ntest_data.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T14:26:15.485497Z","iopub.execute_input":"2022-07-05T14:26:15.488383Z","iopub.status.idle":"2022-07-05T14:26:15.532009Z","shell.execute_reply.started":"2022-07-05T14:26:15.488304Z","shell.execute_reply":"2022-07-05T14:26:15.530570Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T14:26:55.118957Z","iopub.execute_input":"2022-07-05T14:26:55.119484Z","iopub.status.idle":"2022-07-05T14:26:55.139056Z","shell.execute_reply.started":"2022-07-05T14:26:55.119443Z","shell.execute_reply":"2022-07-05T14:26:55.137691Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Format the columns to match what they were in the original training set.","metadata":{}},{"cell_type":"code","source":"test_data['child']  = test_data['Age'].apply(lambda x: 1 if x < 10 else 0)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T14:32:28.402525Z","iopub.execute_input":"2022-07-05T14:32:28.404398Z","iopub.status.idle":"2022-07-05T14:32:28.413059Z","shell.execute_reply.started":"2022-07-05T14:32:28.404333Z","shell.execute_reply":"2022-07-05T14:32:28.411925Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data = test_data.drop(['Name', 'Ticket','Age', 'Cabin'], axis = 1)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T14:33:20.474688Z","iopub.execute_input":"2022-07-05T14:33:20.475113Z","iopub.status.idle":"2022-07-05T14:33:20.485024Z","shell.execute_reply.started":"2022-07-05T14:33:20.475080Z","shell.execute_reply":"2022-07-05T14:33:20.483546Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data = test_data.set_index('PassengerId')","metadata":{"execution":{"iopub.status.busy":"2022-07-05T14:34:38.731141Z","iopub.execute_input":"2022-07-05T14:34:38.731647Z","iopub.status.idle":"2022-07-05T14:34:38.740121Z","shell.execute_reply.started":"2022-07-05T14:34:38.731612Z","shell.execute_reply":"2022-07-05T14:34:38.738688Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for col in test_data.columns:\n    if col in ['Pclass', 'child', 'SibSp','Parch', 'Embarked']:\n        test_data[col] = test_data[col].astype(object)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T14:35:22.287946Z","iopub.execute_input":"2022-07-05T14:35:22.288425Z","iopub.status.idle":"2022-07-05T14:35:22.297952Z","shell.execute_reply.started":"2022-07-05T14:35:22.288391Z","shell.execute_reply":"2022-07-05T14:35:22.296903Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T14:35:44.447587Z","iopub.execute_input":"2022-07-05T14:35:44.448191Z","iopub.status.idle":"2022-07-05T14:35:44.471324Z","shell.execute_reply.started":"2022-07-05T14:35:44.448141Z","shell.execute_reply":"2022-07-05T14:35:44.469844Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data[test_data['Fare'].isnull()]","metadata":{"execution":{"iopub.status.busy":"2022-07-05T14:41:22.484704Z","iopub.execute_input":"2022-07-05T14:41:22.485096Z","iopub.status.idle":"2022-07-05T14:41:22.510216Z","shell.execute_reply.started":"2022-07-05T14:41:22.485062Z","shell.execute_reply":"2022-07-05T14:41:22.506959Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"imp_mean = SimpleImputer(missing_values=np.nan, strategy='mean')","metadata":{"execution":{"iopub.status.busy":"2022-07-05T14:43:39.951180Z","iopub.execute_input":"2022-07-05T14:43:39.951800Z","iopub.status.idle":"2022-07-05T14:43:39.958088Z","shell.execute_reply.started":"2022-07-05T14:43:39.951757Z","shell.execute_reply":"2022-07-05T14:43:39.957086Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_fare = imp_mean.fit_transform(test_data['Fare'].values.reshape(-1, 1))","metadata":{"execution":{"iopub.status.busy":"2022-07-05T14:49:07.890980Z","iopub.execute_input":"2022-07-05T14:49:07.891543Z","iopub.status.idle":"2022-07-05T14:49:07.900199Z","shell.execute_reply.started":"2022-07-05T14:49:07.891498Z","shell.execute_reply":"2022-07-05T14:49:07.898800Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data['Fare2'] = pd.DataFrame(new_fare)[0].to_list()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T14:51:47.870574Z","iopub.execute_input":"2022-07-05T14:51:47.871022Z","iopub.status.idle":"2022-07-05T14:51:47.881714Z","shell.execute_reply.started":"2022-07-05T14:51:47.870989Z","shell.execute_reply":"2022-07-05T14:51:47.880592Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data.drop(['Fare'], axis = 1, inplace = True)\ntest_data.rename(columns = {'Fare2' : 'Fare'}, inplace = True)\ntest_data.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T14:53:15.373720Z","iopub.execute_input":"2022-07-05T14:53:15.374370Z","iopub.status.idle":"2022-07-05T14:53:15.399950Z","shell.execute_reply.started":"2022-07-05T14:53:15.374317Z","shell.execute_reply":"2022-07-05T14:53:15.398509Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_predictions = final_model.predict(test_data)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T14:53:20.094768Z","iopub.execute_input":"2022-07-05T14:53:20.095526Z","iopub.status.idle":"2022-07-05T14:53:20.127153Z","shell.execute_reply.started":"2022-07-05T14:53:20.095469Z","shell.execute_reply":"2022-07-05T14:53:20.125669Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data","metadata":{"execution":{"iopub.status.busy":"2022-07-05T14:53:54.839104Z","iopub.execute_input":"2022-07-05T14:53:54.840426Z","iopub.status.idle":"2022-07-05T14:53:54.860653Z","shell.execute_reply.started":"2022-07-05T14:53:54.840377Z","shell.execute_reply":"2022-07-05T14:53:54.859552Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output = pd.DataFrame(test_predictions, index = test_data.index, columns = ['Survived'])","metadata":{"execution":{"iopub.status.busy":"2022-07-05T14:55:49.429455Z","iopub.execute_input":"2022-07-05T14:55:49.430674Z","iopub.status.idle":"2022-07-05T14:55:49.436743Z","shell.execute_reply.started":"2022-07-05T14:55:49.430623Z","shell.execute_reply":"2022-07-05T14:55:49.435664Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output = output.reset_index()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T14:56:07.786443Z","iopub.execute_input":"2022-07-05T14:56:07.786959Z","iopub.status.idle":"2022-07-05T14:56:07.795412Z","shell.execute_reply.started":"2022-07-05T14:56:07.786921Z","shell.execute_reply":"2022-07-05T14:56:07.794348Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output.to_csv('submission.csv', index=False)\nprint(\"Your submission was successfully saved!\")","metadata":{"execution":{"iopub.status.busy":"2022-07-05T14:59:56.546080Z","iopub.execute_input":"2022-07-05T14:59:56.546592Z","iopub.status.idle":"2022-07-05T14:59:56.557721Z","shell.execute_reply.started":"2022-07-05T14:59:56.546547Z","shell.execute_reply":"2022-07-05T14:59:56.556334Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}