{"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":"import pandas as pd\nimport numpy as np","metadata":{"execution":{"iopub.status.busy":"2022-08-03T06:54:29.059569Z","iopub.execute_input":"2022-08-03T06:54:29.060413Z","iopub.status.idle":"2022-08-03T06:54:29.087016Z","shell.execute_reply.started":"2022-08-03T06:54:29.060285Z","shell.execute_reply":"2022-08-03T06:54:29.086069Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_path = '../input/titanic/test.csv'\ntrain_path = '../input/titanic/train.csv'\n\ntest = pd.read_csv(test_path)\ntrain = pd.read_csv(train_path)\nprint(train.shape, '\\t', test.shape)","metadata":{"execution":{"iopub.status.busy":"2022-08-03T06:54:29.089260Z","iopub.execute_input":"2022-08-03T06:54:29.090049Z","iopub.status.idle":"2022-08-03T06:54:29.121006Z","shell.execute_reply.started":"2022-08-03T06:54:29.090006Z","shell.execute_reply":"2022-08-03T06:54:29.119950Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nulls = train.isnull().sum().sort_values(ascending=False)\nnulls.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-03T06:54:29.123212Z","iopub.execute_input":"2022-08-03T06:54:29.124090Z","iopub.status.idle":"2022-08-03T06:54:29.138751Z","shell.execute_reply.started":"2022-08-03T06:54:29.124054Z","shell.execute_reply":"2022-08-03T06:54:29.137430Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nulls = nulls.head(1).index.tolist()\nnulls = nulls + ['Name', 'Ticket']\nnulls","metadata":{"execution":{"iopub.status.busy":"2022-08-03T06:54:29.142264Z","iopub.execute_input":"2022-08-03T06:54:29.143068Z","iopub.status.idle":"2022-08-03T06:54:29.151158Z","shell.execute_reply.started":"2022-08-03T06:54:29.143013Z","shell.execute_reply":"2022-08-03T06:54:29.149986Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.drop(nulls, axis=1, inplace=True)\ntest.drop(nulls, axis=1, inplace=True)\nprint(train.shape,'\\t',test.shape)","metadata":{"execution":{"iopub.status.busy":"2022-08-03T06:54:29.152424Z","iopub.execute_input":"2022-08-03T06:54:29.152758Z","iopub.status.idle":"2022-08-03T06:54:29.170651Z","shell.execute_reply.started":"2022-08-03T06:54:29.152729Z","shell.execute_reply":"2022-08-03T06:54:29.169569Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.info()","metadata":{"execution":{"iopub.status.busy":"2022-08-03T06:54:29.172276Z","iopub.execute_input":"2022-08-03T06:54:29.173424Z","iopub.status.idle":"2022-08-03T06:54:29.193892Z","shell.execute_reply.started":"2022-08-03T06:54:29.173393Z","shell.execute_reply":"2022-08-03T06:54:29.192724Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.describe()","metadata":{"execution":{"iopub.status.busy":"2022-08-03T06:54:29.196873Z","iopub.execute_input":"2022-08-03T06:54:29.197573Z","iopub.status.idle":"2022-08-03T06:54:29.236990Z","shell.execute_reply.started":"2022-08-03T06:54:29.197530Z","shell.execute_reply":"2022-08-03T06:54:29.235840Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"####################\n### VISUALIZATION ###\n####################\nimport seaborn as sns\nimport plotly.express as px\nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2022-08-03T06:54:29.238328Z","iopub.execute_input":"2022-08-03T06:54:29.238689Z","iopub.status.idle":"2022-08-03T06:54:31.633608Z","shell.execute_reply.started":"2022-08-03T06:54:29.238657Z","shell.execute_reply":"2022-08-03T06:54:31.631861Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(16,9))\nsns.boxplot(x=\"Survived\", y=\"Fare\", hue=\"Sex\", data=train, width=0.5)","metadata":{"execution":{"iopub.status.busy":"2022-08-03T06:54:31.636803Z","iopub.execute_input":"2022-08-03T06:54:31.637318Z","iopub.status.idle":"2022-08-03T06:54:32.038537Z","shell.execute_reply.started":"2022-08-03T06:54:31.637286Z","shell.execute_reply":"2022-08-03T06:54:32.036810Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10, 7))\nsns.barplot(x=\"Pclass\", y=\"Survived\", data=train, estimator=sum, ci=None)\nplt.title('Survived Passenger Class')","metadata":{"execution":{"iopub.status.busy":"2022-08-03T06:54:32.040105Z","iopub.execute_input":"2022-08-03T06:54:32.041047Z","iopub.status.idle":"2022-08-03T06:54:32.247711Z","shell.execute_reply.started":"2022-08-03T06:54:32.041012Z","shell.execute_reply":"2022-08-03T06:54:32.246796Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"px.pie(train, names='Pclass', hole=.6, title=\"Titanic Passenger Class Ratio\")","metadata":{"execution":{"iopub.status.busy":"2022-08-03T06:54:32.249034Z","iopub.execute_input":"2022-08-03T06:54:32.250054Z","iopub.status.idle":"2022-08-03T06:54:33.410533Z","shell.execute_reply.started":"2022-08-03T06:54:32.250017Z","shell.execute_reply":"2022-08-03T06:54:33.409570Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(16, 9))\nsns.barplot(x=\"Pclass\", y=\"Fare\", data=train, estimator=sum, hue='Survived', ci=None)","metadata":{"execution":{"iopub.status.busy":"2022-08-03T06:54:33.411912Z","iopub.execute_input":"2022-08-03T06:54:33.412852Z","iopub.status.idle":"2022-08-03T06:54:33.662046Z","shell.execute_reply.started":"2022-08-03T06:54:33.412806Z","shell.execute_reply":"2022-08-03T06:54:33.661005Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(16,9))\nsns.barplot(data=train, x='Pclass', y='Survived', hue='Embarked', ci=None, estimator=sum)","metadata":{"execution":{"iopub.status.busy":"2022-08-03T06:54:33.663651Z","iopub.execute_input":"2022-08-03T06:54:33.664127Z","iopub.status.idle":"2022-08-03T06:54:33.928094Z","shell.execute_reply.started":"2022-08-03T06:54:33.664083Z","shell.execute_reply":"2022-08-03T06:54:33.926645Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(16,9))\nsns.kdeplot(train.Age[train.Sex=='male'], shade=True, color=\"b\")\nsns.kdeplot(train.Age[train.Sex=='female'], shade=True, color=\"r\")","metadata":{"execution":{"iopub.status.busy":"2022-08-03T06:54:33.929756Z","iopub.execute_input":"2022-08-03T06:54:33.930124Z","iopub.status.idle":"2022-08-03T06:54:34.199598Z","shell.execute_reply.started":"2022-08-03T06:54:33.930091Z","shell.execute_reply":"2022-08-03T06:54:34.198339Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"px.density_heatmap(train, x='Age', y='Sex', z='Survived')","metadata":{"execution":{"iopub.status.busy":"2022-08-03T06:54:34.201133Z","iopub.execute_input":"2022-08-03T06:54:34.202012Z","iopub.status.idle":"2022-08-03T06:54:34.295782Z","shell.execute_reply.started":"2022-08-03T06:54:34.201970Z","shell.execute_reply":"2022-08-03T06:54:34.294740Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.isnull().sum().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-03T06:54:34.297164Z","iopub.execute_input":"2022-08-03T06:54:34.297596Z","iopub.status.idle":"2022-08-03T06:54:34.306171Z","shell.execute_reply.started":"2022-08-03T06:54:34.297556Z","shell.execute_reply":"2022-08-03T06:54:34.304991Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.info()","metadata":{"execution":{"iopub.status.busy":"2022-08-03T06:54:34.307507Z","iopub.execute_input":"2022-08-03T06:54:34.307923Z","iopub.status.idle":"2022-08-03T06:54:34.325320Z","shell.execute_reply.started":"2022-08-03T06:54:34.307892Z","shell.execute_reply":"2022-08-03T06:54:34.324348Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.set_index('PassengerId', inplace=True)\ntrain = train.astype({'Pclass': object,\n                'SibSp': object,\n                'Parch': object}, errors='raise')\ntrain.info()","metadata":{"execution":{"iopub.status.busy":"2022-08-03T06:54:34.326395Z","iopub.execute_input":"2022-08-03T06:54:34.326760Z","iopub.status.idle":"2022-08-03T06:54:34.350095Z","shell.execute_reply.started":"2022-08-03T06:54:34.326720Z","shell.execute_reply":"2022-08-03T06:54:34.349070Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = train[['Pclass', 'Sex', 'Age', 'SibSp', 'Parch', 'Fare', 'Embarked']]\ny = train.Survived\n\nfrom sklearn.model_selection import train_test_split\nX_train, X_test, y_train, y_test = train_test_split(X, y, train_size=0.8, random_state=0)\n\ncategorical_cols = ['Pclass', 'Sex', 'Embarked']\n\nnumerical_cols = ['Age', 'SibSp', 'Parch', 'Fare']\n\nmy_cols = categorical_cols+ numerical_cols\nX_train = X_train[my_cols]\nX_test = X_test[my_cols]\n\nprint(numerical_cols, categorical_cols)","metadata":{"execution":{"iopub.status.busy":"2022-08-03T06:54:34.351604Z","iopub.execute_input":"2022-08-03T06:54:34.352215Z","iopub.status.idle":"2022-08-03T06:54:34.481002Z","shell.execute_reply.started":"2022-08-03T06:54:34.352167Z","shell.execute_reply":"2022-08-03T06:54:34.479844Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"######################\n### MODEL BUILDING ###\n######################\nfrom sklearn.ensemble import RandomForestClassifier\nrf = RandomForestClassifier(n_estimators=100, random_state=0)","metadata":{"execution":{"iopub.status.busy":"2022-08-03T06:54:34.482642Z","iopub.execute_input":"2022-08-03T06:54:34.483082Z","iopub.status.idle":"2022-08-03T06:54:34.701580Z","shell.execute_reply.started":"2022-08-03T06:54:34.483040Z","shell.execute_reply":"2022-08-03T06:54:34.700567Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"################\n### PIPELINE ###\n################\nfrom sklearn.pipeline import Pipeline\nfrom sklearn.impute import SimpleImputer\nfrom sklearn.preprocessing import StandardScaler\nfrom sklearn.preprocessing import LabelEncoder\nfrom sklearn.preprocessing import OneHotEncoder\n\nnumerical_pipeline = Pipeline(steps=[\n    ('imputer', SimpleImputer(strategy='mean')),\n    ('Scale', StandardScaler())\n])\n\ncategorical_pipeline = Pipeline(steps=[\n    ('impute', SimpleImputer(strategy='most_frequent')),\n    ('OHE', OneHotEncoder())\n])\n\nfrom sklearn.compose import ColumnTransformer\npreprocessor = ColumnTransformer(transformers=[\n    ('num', numerical_pipeline, numerical_cols),\n    ('cat', categorical_pipeline, categorical_cols)\n])\n\nmy_pipeline = Pipeline(steps=[\n    ('preprocessor', preprocessor),\n    ('model', rf)\n])\nmy_pipeline","metadata":{"execution":{"iopub.status.busy":"2022-08-03T06:54:34.702889Z","iopub.execute_input":"2022-08-03T06:54:34.703212Z","iopub.status.idle":"2022-08-03T06:54:34.752855Z","shell.execute_reply.started":"2022-08-03T06:54:34.703178Z","shell.execute_reply":"2022-08-03T06:54:34.751481Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"my_pipeline.fit(X_train, y_train)\npreds = my_pipeline.predict(X_test)\npreds","metadata":{"execution":{"iopub.status.busy":"2022-08-03T06:54:34.754754Z","iopub.execute_input":"2022-08-03T06:54:34.755111Z","iopub.status.idle":"2022-08-03T06:54:34.987244Z","shell.execute_reply.started":"2022-08-03T06:54:34.755078Z","shell.execute_reply":"2022-08-03T06:54:34.986182Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import confusion_matrix\ncm = confusion_matrix(y_test, preds)\nprint(f'Succes rate: {((cm[0][0]+cm[1][1])/(cm.sum()))*100:.2f}%')","metadata":{"execution":{"iopub.status.busy":"2022-08-03T06:54:34.988663Z","iopub.execute_input":"2022-08-03T06:54:34.988999Z","iopub.status.idle":"2022-08-03T06:54:34.997223Z","shell.execute_reply.started":"2022-08-03T06:54:34.988968Z","shell.execute_reply":"2022-08-03T06:54:34.996437Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.info()","metadata":{"execution":{"iopub.status.busy":"2022-08-03T06:54:35.002738Z","iopub.execute_input":"2022-08-03T06:54:35.003320Z","iopub.status.idle":"2022-08-03T06:54:35.019175Z","shell.execute_reply.started":"2022-08-03T06:54:35.003277Z","shell.execute_reply":"2022-08-03T06:54:35.018206Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = test.astype({'Pclass':object,\n            'SibSp':object,\n            'Parch':object})\ntest.info()","metadata":{"execution":{"iopub.status.busy":"2022-08-03T06:54:35.020830Z","iopub.execute_input":"2022-08-03T06:54:35.021630Z","iopub.status.idle":"2022-08-03T06:54:35.042552Z","shell.execute_reply.started":"2022-08-03T06:54:35.021552Z","shell.execute_reply":"2022-08-03T06:54:35.041451Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test.head(3)","metadata":{"execution":{"iopub.status.busy":"2022-08-03T06:54:35.043672Z","iopub.execute_input":"2022-08-03T06:54:35.044510Z","iopub.status.idle":"2022-08-03T06:54:35.058943Z","shell.execute_reply.started":"2022-08-03T06:54:35.044464Z","shell.execute_reply":"2022-08-03T06:54:35.057574Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.set_index('PassengerId', inplace=True)\ntest.head(3)","metadata":{"execution":{"iopub.status.busy":"2022-08-03T06:54:35.060261Z","iopub.execute_input":"2022-08-03T06:54:35.061181Z","iopub.status.idle":"2022-08-03T06:54:35.077167Z","shell.execute_reply.started":"2022-08-03T06:54:35.061141Z","shell.execute_reply":"2022-08-03T06:54:35.075932Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_preds = my_pipeline.predict(test)\noutput = pd.DataFrame({'PassengerId': test.index,\n                       'Survived': test_preds})\noutput.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-08-03T06:54:35.078465Z","iopub.execute_input":"2022-08-03T06:54:35.079285Z","iopub.status.idle":"2022-08-03T06:54:35.122067Z","shell.execute_reply.started":"2022-08-03T06:54:35.079252Z","shell.execute_reply":"2022-08-03T06:54:35.121193Z"},"trusted":true},"execution_count":null,"outputs":[]}]}