{"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":"\n# Kaggle - Titanic Dataset Analysis & Forecast\n### y = 'survived'\n","metadata":{"papermill":{"duration":0.013413,"end_time":"2022-08-08T18:28:22.803839","exception":false,"start_time":"2022-08-08T18:28:22.790426","status":"completed"},"tags":[]}},{"cell_type":"markdown","source":"created by Dominic Hernes, 06.08.2022","metadata":{"papermill":{"duration":0.0132,"end_time":"2022-08-08T18:28:22.830154","exception":false,"start_time":"2022-08-08T18:28:22.816954","status":"completed"},"tags":[]}},{"cell_type":"code","source":"from pathlib import Path\n\nimport numpy as np\nimport pandas as pd\n\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n\nfrom sklearn.pipeline import Pipeline\nfrom sklearn.preprocessing import OneHotEncoder, StandardScaler\nfrom sklearn.compose import make_column_transformer\nfrom sklearn.model_selection import train_test_split, GridSearchCV, KFold\nfrom sklearn.metrics import accuracy_score\nfrom sklearn.ensemble import RandomForestClassifier\nfrom sklearn.inspection import permutation_importance\n\nimport warnings\nwarnings.simplefilter(action='ignore', category=FutureWarning)\nwarnings.simplefilter(action='ignore', category=UserWarning)\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":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","papermill":{"duration":1.777092,"end_time":"2022-08-08T18:28:24.621051","exception":false,"start_time":"2022-08-08T18:28:22.843959","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-12T22:30:25.907131Z","iopub.execute_input":"2022-08-12T22:30:25.907620Z","iopub.status.idle":"2022-08-12T22:30:25.921135Z","shell.execute_reply.started":"2022-08-12T22:30:25.907584Z","shell.execute_reply":"2022-08-12T22:30:25.919957Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# flags, constants and globals\n# global variable\nbase_folder = Path.cwd()\ndata_folder = base_folder / \"data\"\n\n# flags\nVERBOSITY = True\n\n# constants\nRAND_SEED = 1337\nVALID_SIZE = 0.2\n","metadata":{"papermill":{"duration":0.023712,"end_time":"2022-08-08T18:28:24.658411","exception":false,"start_time":"2022-08-08T18:28:24.634699","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-12T22:30:26.053989Z","iopub.execute_input":"2022-08-12T22:30:26.054824Z","iopub.status.idle":"2022-08-12T22:30:26.060901Z","shell.execute_reply.started":"2022-08-12T22:30:26.054780Z","shell.execute_reply":"2022-08-12T22:30:26.059621Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# functions\ndef percentile_check(df: pd.DataFrame, key: str, percentile_high: float = 0.95, percentile_low: float = 0.05) -> None:\n    q_high = df[key].quantile(percentile_high)\n    q_low = df[key].quantile(percentile_low)\n    print(f\"Percentile check for the metric column '{key}':\")\n    print(\n        f\"{'Number above':<18} {q_high:>3n} ({percentile_high * 100:>6.2f}%-Quantil):{df[df[key] > q_high][key].describe()['count']:>10,}\")\n    print(\n        f\"{'Number below':<18} {q_low:>3n} ({percentile_low * 100:>6.2f}%-Quantil):{df[df[key] < q_low][key].describe()['count']:>10,}\")","metadata":{"papermill":{"duration":0.025005,"end_time":"2022-08-08T18:28:24.697337","exception":false,"start_time":"2022-08-08T18:28:24.672332","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-12T22:30:26.194132Z","iopub.execute_input":"2022-08-12T22:30:26.194957Z","iopub.status.idle":"2022-08-12T22:30:26.203240Z","shell.execute_reply.started":"2022-08-12T22:30:26.194911Z","shell.execute_reply":"2022-08-12T22:30:26.201781Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# load data\ntitan_df = pd.read_csv('../input/titanic/train.csv')\n","metadata":{"papermill":{"duration":0.035839,"end_time":"2022-08-08T18:28:24.747958","exception":false,"start_time":"2022-08-08T18:28:24.712119","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-12T22:30:26.332516Z","iopub.execute_input":"2022-08-12T22:30:26.332975Z","iopub.status.idle":"2022-08-12T22:30:26.345132Z","shell.execute_reply.started":"2022-08-12T22:30:26.332929Z","shell.execute_reply":"2022-08-12T22:30:26.344159Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"\n## Part 1: Data cleaning / preparation / exploration\n### Quick Overview","metadata":{"papermill":{"duration":0.012137,"end_time":"2022-08-08T18:28:24.773193","exception":false,"start_time":"2022-08-08T18:28:24.761056","status":"completed"},"tags":[]}},{"cell_type":"code","source":"# Train df head\ntitan_df.head()","metadata":{"papermill":{"duration":0.040426,"end_time":"2022-08-08T18:28:24.825864","exception":false,"start_time":"2022-08-08T18:28:24.785438","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-12T22:30:26.479637Z","iopub.execute_input":"2022-08-12T22:30:26.480554Z","iopub.status.idle":"2022-08-12T22:30:26.500740Z","shell.execute_reply.started":"2022-08-12T22:30:26.480514Z","shell.execute_reply":"2022-08-12T22:30:26.499501Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Information about the data**<br><br>\nPassengerID - Passenger's identification<br>\nSurvived - Status survived/missing,dead<br> \nPclass - Ticket/Passenger class<br>\nName - Name of passenger<br>\nSex - Gender of passenger<br>\nAge - Age of passenger<br>\nSibSp - Number of siblings or spouse<br>\nParch - Number of parents or child<br>\nTicket - Ticket number<br>\nFare - Ticket price<br> \nCabin - Cabin number<br> \nEmbarked - homeport (C=Cherbourg, Q=Queenstown, S=Southampton)","metadata":{"papermill":{"duration":0.012917,"end_time":"2022-08-08T18:28:24.852251","exception":false,"start_time":"2022-08-08T18:28:24.839334","status":"completed"},"tags":[]}},{"cell_type":"code","source":"# df info\ntitan_df.info()","metadata":{"papermill":{"duration":0.045055,"end_time":"2022-08-08T18:28:24.911159","exception":false,"start_time":"2022-08-08T18:28:24.866104","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-12T22:30:26.620031Z","iopub.execute_input":"2022-08-12T22:30:26.620507Z","iopub.status.idle":"2022-08-12T22:30:26.637233Z","shell.execute_reply.started":"2022-08-12T22:30:26.620468Z","shell.execute_reply":"2022-08-12T22:30:26.635610Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# df describe\ntitan_df.describe()","metadata":{"papermill":{"duration":0.058495,"end_time":"2022-08-08T18:28:24.984500","exception":false,"start_time":"2022-08-08T18:28:24.926005","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-12T22:30:26.800010Z","iopub.execute_input":"2022-08-12T22:30:26.800593Z","iopub.status.idle":"2022-08-12T22:30:26.839876Z","shell.execute_reply.started":"2022-08-12T22:30:26.800551Z","shell.execute_reply":"2022-08-12T22:30:26.838621Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# df missing value heatmap\nsns.set(style=\"darkgrid\")\nplt.figure(figsize=(12, 8))\nsns.heatmap(titan_df.isna().transpose(),\n            cmap=sns.color_palette('muted'),\n            cbar_kws={'label': 'Overview Missing Data'})","metadata":{"papermill":{"duration":0.809429,"end_time":"2022-08-08T18:28:25.808960","exception":false,"start_time":"2022-08-08T18:28:24.999531","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-12T22:30:26.932977Z","iopub.execute_input":"2022-08-12T22:30:26.933763Z","iopub.status.idle":"2022-08-12T22:30:28.119287Z","shell.execute_reply.started":"2022-08-12T22:30:26.933717Z","shell.execute_reply":"2022-08-12T22:30:28.117965Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Data Cleaning and Preparation","metadata":{"papermill":{"duration":0.013639,"end_time":"2022-08-08T18:28:25.837885","exception":false,"start_time":"2022-08-08T18:28:25.824246","status":"completed"},"tags":[]}},{"cell_type":"markdown","source":"#### Data Cleaning - Numerical features\nPassengerId/Survived/Pclass are ok!\nAge/SibSp/Parch/Fare","metadata":{"papermill":{"duration":0.013516,"end_time":"2022-08-08T18:28:25.865237","exception":false,"start_time":"2022-08-08T18:28:25.851721","status":"completed"},"tags":[]}},{"cell_type":"code","source":"# 'Fare' Feature\n# Distplot\nsns.distplot(titan_df['Fare'], kde=False, color='blue', bins=20)\nplt.title('Fare Dist Plot')\nplt.xlabel('Price')\nplt.ylabel('Total')\nplt.show()","metadata":{"papermill":{"duration":0.213287,"end_time":"2022-08-08T18:28:26.093136","exception":false,"start_time":"2022-08-08T18:28:25.879849","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-12T22:30:28.121512Z","iopub.execute_input":"2022-08-12T22:30:28.122410Z","iopub.status.idle":"2022-08-12T22:30:28.401716Z","shell.execute_reply.started":"2022-08-12T22:30:28.122368Z","shell.execute_reply":"2022-08-12T22:30:28.400469Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Box Plot\nplt.figure(figsize=(4, 12))\nsns.boxplot(data=titan_df['Fare'], palette=\"muted\")\nplt.title('Fare Box Plot')\nplt.xlabel('Fare feature')\nplt.ylabel('Prize')\nplt.show()","metadata":{"papermill":{"duration":0.152282,"end_time":"2022-08-08T18:28:26.261822","exception":false,"start_time":"2022-08-08T18:28:26.109540","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-12T22:30:28.402950Z","iopub.execute_input":"2022-08-12T22:30:28.403305Z","iopub.status.idle":"2022-08-12T22:30:28.602674Z","shell.execute_reply.started":"2022-08-12T22:30:28.403273Z","shell.execute_reply":"2022-08-12T22:30:28.601396Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# percentile check\npercentile_check(titan_df, 'Fare', 0.99, 0.01)","metadata":{"papermill":{"duration":0.035138,"end_time":"2022-08-08T18:28:26.319644","exception":false,"start_time":"2022-08-08T18:28:26.284506","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-12T22:30:28.606154Z","iopub.execute_input":"2022-08-12T22:30:28.606567Z","iopub.status.idle":"2022-08-12T22:30:28.623457Z","shell.execute_reply.started":"2022-08-12T22:30:28.606531Z","shell.execute_reply":"2022-08-12T22:30:28.622196Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 'Fare' feature outlier\ntitan_df[titan_df['Fare'] < 4]","metadata":{"papermill":{"duration":0.038838,"end_time":"2022-08-08T18:28:26.375618","exception":false,"start_time":"2022-08-08T18:28:26.336780","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-12T22:30:28.625063Z","iopub.execute_input":"2022-08-12T22:30:28.625443Z","iopub.status.idle":"2022-08-12T22:30:28.648105Z","shell.execute_reply.started":"2022-08-12T22:30:28.625411Z","shell.execute_reply":"2022-08-12T22:30:28.647153Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# group by for imputation\nagg_fare = titan_df.groupby('Pclass')['Fare'].agg([np.size, np.mean, np.std, np.median, np.min, np.max])","metadata":{"papermill":{"duration":0.035412,"end_time":"2022-08-08T18:28:26.427466","exception":false,"start_time":"2022-08-08T18:28:26.392054","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-12T22:30:28.649252Z","iopub.execute_input":"2022-08-12T22:30:28.649608Z","iopub.status.idle":"2022-08-12T22:30:28.664683Z","shell.execute_reply.started":"2022-08-12T22:30:28.649575Z","shell.execute_reply":"2022-08-12T22:30:28.663445Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# impute nulls\npcl_idx = agg_fare.index.to_list()\npcl_median = round(agg_fare['median'], 2).to_list()\n\nfor i in pcl_idx:\n    aggmask = (titan_df['Fare'] == 0) & (titan_df['Pclass'] == i)\n    titan_df['Fare'] = titan_df['Fare'].mask(aggmask, pcl_median[i - 1])","metadata":{"papermill":{"duration":0.027787,"end_time":"2022-08-08T18:28:26.470535","exception":false,"start_time":"2022-08-08T18:28:26.442748","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-12T22:30:28.666495Z","iopub.execute_input":"2022-08-12T22:30:28.667250Z","iopub.status.idle":"2022-08-12T22:30:28.680637Z","shell.execute_reply.started":"2022-08-12T22:30:28.667201Z","shell.execute_reply":"2022-08-12T22:30:28.679645Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# the upper outlier is true and should not be deleted","metadata":{"papermill":{"duration":0.024377,"end_time":"2022-08-08T18:28:26.513428","exception":false,"start_time":"2022-08-08T18:28:26.489051","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-12T22:30:28.682617Z","iopub.execute_input":"2022-08-12T22:30:28.683433Z","iopub.status.idle":"2022-08-12T22:30:28.688719Z","shell.execute_reply.started":"2022-08-12T22:30:28.683384Z","shell.execute_reply":"2022-08-12T22:30:28.687362Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 'Age' Feature\ntitan_df['Age'].isna().value_counts()","metadata":{"papermill":{"duration":0.028941,"end_time":"2022-08-08T18:28:26.558187","exception":false,"start_time":"2022-08-08T18:28:26.529246","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-12T22:30:28.690502Z","iopub.execute_input":"2022-08-12T22:30:28.691314Z","iopub.status.idle":"2022-08-12T22:30:28.706612Z","shell.execute_reply.started":"2022-08-12T22:30:28.691261Z","shell.execute_reply":"2022-08-12T22:30:28.705538Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 'Age' group by for imputation\nbins = [0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 70, 90, 100, 150, 200, 250, 300]\nagg_age = agg_fare = titan_df.groupby(['Pclass', pd.cut(titan_df['Fare'], bins)])['Age'].agg(\n                                      [np.size, np.mean, np.std, np.median, np.min, np.max])","metadata":{"papermill":{"duration":0.060488,"end_time":"2022-08-08T18:28:26.634339","exception":false,"start_time":"2022-08-08T18:28:26.573851","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-12T22:30:28.710511Z","iopub.execute_input":"2022-08-12T22:30:28.711423Z","iopub.status.idle":"2022-08-12T22:30:28.752827Z","shell.execute_reply.started":"2022-08-12T22:30:28.711384Z","shell.execute_reply":"2022-08-12T22:30:28.751831Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# impute table\nagg_age","metadata":{"papermill":{"duration":0.050057,"end_time":"2022-08-08T18:28:26.700193","exception":false,"start_time":"2022-08-08T18:28:26.650136","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-12T22:30:28.754037Z","iopub.execute_input":"2022-08-12T22:30:28.754596Z","iopub.status.idle":"2022-08-12T22:30:28.792294Z","shell.execute_reply.started":"2022-08-12T22:30:28.754558Z","shell.execute_reply":"2022-08-12T22:30:28.790906Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 'Age' imputation\ntitan_df['Age'] = titan_df['Age'].fillna(0)","metadata":{"papermill":{"duration":0.035446,"end_time":"2022-08-08T18:28:26.752170","exception":false,"start_time":"2022-08-08T18:28:26.716724","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-12T22:30:28.793661Z","iopub.execute_input":"2022-08-12T22:30:28.793997Z","iopub.status.idle":"2022-08-12T22:30:28.799902Z","shell.execute_reply.started":"2022-08-12T22:30:28.793967Z","shell.execute_reply":"2022-08-12T22:30:28.798856Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(len(titan_df['Age'])):\n    if titan_df.loc[i, 'Age'] == 0:\n        titan_df.loc[i, 'Age'] = agg_age.loc[titan_df.loc[i, 'Pclass'], titan_df.loc[i, 'Fare']]['median']","metadata":{"papermill":{"duration":0.162047,"end_time":"2022-08-08T18:28:26.929244","exception":false,"start_time":"2022-08-08T18:28:26.767197","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-12T22:30:28.801583Z","iopub.execute_input":"2022-08-12T22:30:28.801943Z","iopub.status.idle":"2022-08-12T22:30:28.979424Z","shell.execute_reply.started":"2022-08-12T22:30:28.801910Z","shell.execute_reply":"2022-08-12T22:30:28.978055Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# null value check\ntitan_df['Age'].isna().value_counts()","metadata":{"papermill":{"duration":0.028204,"end_time":"2022-08-08T18:28:26.973112","exception":false,"start_time":"2022-08-08T18:28:26.944908","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-12T22:30:28.981016Z","iopub.execute_input":"2022-08-12T22:30:28.982197Z","iopub.status.idle":"2022-08-12T22:30:28.990329Z","shell.execute_reply.started":"2022-08-12T22:30:28.982155Z","shell.execute_reply":"2022-08-12T22:30:28.989190Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 'Age' dist plot\nsns.distplot(titan_df['Age'], kde=False, color='blue', bins=40)\nplt.title('Age Dist Plot')\nplt.xlabel('Age')\nplt.ylabel('Total')\nplt.show()","metadata":{"papermill":{"duration":0.270902,"end_time":"2022-08-08T18:28:27.260029","exception":false,"start_time":"2022-08-08T18:28:26.989127","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-12T22:30:28.993096Z","iopub.execute_input":"2022-08-12T22:30:28.994172Z","iopub.status.idle":"2022-08-12T22:30:29.339432Z","shell.execute_reply.started":"2022-08-12T22:30:28.994103Z","shell.execute_reply":"2022-08-12T22:30:29.338013Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 'SibSp' & 'Parch' Features\n#\n# 'SibSp*\nsns.distplot(titan_df['SibSp'], kde=False, color='blue', bins=10)\nplt.title('SibSp')\nplt.xlabel('Number of siblings or spouse')\nplt.ylabel('Total')\nplt.show()","metadata":{"papermill":{"duration":0.220393,"end_time":"2022-08-08T18:28:27.496222","exception":false,"start_time":"2022-08-08T18:28:27.275829","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-12T22:30:29.341720Z","iopub.execute_input":"2022-08-12T22:30:29.342086Z","iopub.status.idle":"2022-08-12T22:30:29.632333Z","shell.execute_reply.started":"2022-08-12T22:30:29.342051Z","shell.execute_reply":"2022-08-12T22:30:29.630924Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# percentile check\npercentile_check(titan_df, 'SibSp', 0.99, 0.01)","metadata":{"papermill":{"duration":0.03103,"end_time":"2022-08-08T18:28:27.543844","exception":false,"start_time":"2022-08-08T18:28:27.512814","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-12T22:30:29.634644Z","iopub.execute_input":"2022-08-12T22:30:29.635146Z","iopub.status.idle":"2022-08-12T22:30:29.651661Z","shell.execute_reply.started":"2022-08-12T22:30:29.635080Z","shell.execute_reply":"2022-08-12T22:30:29.650741Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 'Parch'\nsns.distplot(titan_df['Parch'], kde=False, color='blue', bins=10)\nplt.title('Parch')\nplt.xlabel('Number of parents or child')\nplt.ylabel('Total')\nplt.show()","metadata":{"papermill":{"duration":0.212024,"end_time":"2022-08-08T18:28:27.772320","exception":false,"start_time":"2022-08-08T18:28:27.560296","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-12T22:30:29.654638Z","iopub.execute_input":"2022-08-12T22:30:29.655149Z","iopub.status.idle":"2022-08-12T22:30:29.934557Z","shell.execute_reply.started":"2022-08-12T22:30:29.655077Z","shell.execute_reply":"2022-08-12T22:30:29.933337Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# percentile check\npercentile_check(titan_df, 'Parch', 0.99, 0.01)","metadata":{"papermill":{"duration":0.031835,"end_time":"2022-08-08T18:28:27.821228","exception":false,"start_time":"2022-08-08T18:28:27.789393","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-12T22:30:29.936040Z","iopub.execute_input":"2022-08-12T22:30:29.936439Z","iopub.status.idle":"2022-08-12T22:30:29.951886Z","shell.execute_reply.started":"2022-08-12T22:30:29.936405Z","shell.execute_reply":"2022-08-12T22:30:29.950282Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# creating a 'total_family' feature\ntitan_df['total_family'] = titan_df['SibSp'] + titan_df['Parch']","metadata":{"papermill":{"duration":0.025117,"end_time":"2022-08-08T18:28:27.862466","exception":false,"start_time":"2022-08-08T18:28:27.837349","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-12T22:30:29.953291Z","iopub.execute_input":"2022-08-12T22:30:29.953639Z","iopub.status.idle":"2022-08-12T22:30:29.960367Z","shell.execute_reply.started":"2022-08-12T22:30:29.953607Z","shell.execute_reply":"2022-08-12T22:30:29.958865Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Data Cleaning - Categorial/Object features","metadata":{"papermill":{"duration":0.017081,"end_time":"2022-08-08T18:28:27.896184","exception":false,"start_time":"2022-08-08T18:28:27.879103","status":"completed"},"tags":[]}},{"cell_type":"code","source":"# 'Embarked' Feature\ntitan_df[titan_df['Embarked'].isna()]","metadata":{"papermill":{"duration":0.033763,"end_time":"2022-08-08T18:28:27.947731","exception":false,"start_time":"2022-08-08T18:28:27.913968","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-12T22:30:29.963501Z","iopub.execute_input":"2022-08-12T22:30:29.964231Z","iopub.status.idle":"2022-08-12T22:30:29.987049Z","shell.execute_reply.started":"2022-08-12T22:30:29.964179Z","shell.execute_reply":"2022-08-12T22:30:29.985670Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# both passengers have the same ticket id=113572 and are the only ones with this id\ntitan_df[titan_df['Ticket'] == '113572']","metadata":{"papermill":{"duration":0.034086,"end_time":"2022-08-08T18:28:27.998391","exception":false,"start_time":"2022-08-08T18:28:27.964305","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-12T22:30:29.988666Z","iopub.execute_input":"2022-08-12T22:30:29.989759Z","iopub.status.idle":"2022-08-12T22:30:30.010513Z","shell.execute_reply.started":"2022-08-12T22:30:29.989711Z","shell.execute_reply":"2022-08-12T22:30:30.008961Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# searching a match in 'name'\n# Icard, Miss., Amelie\n# Stone, Mrs. George Nelson (Martha Evelyn)\n# https://www.encyclopedia-titanica.org/titanic-survivor/amelia-icard.html\n# boared = Southampton\ntitan_df['Embarked'] = titan_df['Embarked'].mask(titan_df['Ticket'] == '113572', 'S')","metadata":{"papermill":{"duration":0.026707,"end_time":"2022-08-08T18:28:28.041237","exception":false,"start_time":"2022-08-08T18:28:28.014530","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-12T22:30:30.063585Z","iopub.execute_input":"2022-08-12T22:30:30.064891Z","iopub.status.idle":"2022-08-12T22:30:30.072909Z","shell.execute_reply.started":"2022-08-12T22:30:30.064835Z","shell.execute_reply":"2022-08-12T22:30:30.071983Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Bar Plot 'Embarked'\nsns.set(style=\"darkgrid\")\ntitan_df['Embarked'].value_counts().plot(kind='barh')","metadata":{"papermill":{"duration":0.198968,"end_time":"2022-08-08T18:28:28.258041","exception":false,"start_time":"2022-08-08T18:28:28.059073","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-12T22:30:30.199616Z","iopub.execute_input":"2022-08-12T22:30:30.200259Z","iopub.status.idle":"2022-08-12T22:30:30.430833Z","shell.execute_reply.started":"2022-08-12T22:30:30.200222Z","shell.execute_reply":"2022-08-12T22:30:30.429427Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 'Sex' Feature\ntitan_df['Sex'] = titan_df['Sex'].map({'male': 1, 'female': 0})","metadata":{"papermill":{"duration":0.026872,"end_time":"2022-08-08T18:28:28.302365","exception":false,"start_time":"2022-08-08T18:28:28.275493","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-12T22:30:30.432956Z","iopub.execute_input":"2022-08-12T22:30:30.433321Z","iopub.status.idle":"2022-08-12T22:30:30.439565Z","shell.execute_reply.started":"2022-08-12T22:30:30.433288Z","shell.execute_reply":"2022-08-12T22:30:30.438581Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 'Embarked' Feature\ntitan_df['Embarked'] = titan_df['Embarked'].map({'C': 1, 'S': 2, 'Q':3})","metadata":{"execution":{"iopub.status.busy":"2022-08-12T22:30:30.587079Z","iopub.execute_input":"2022-08-12T22:30:30.588176Z","iopub.status.idle":"2022-08-12T22:30:30.595265Z","shell.execute_reply.started":"2022-08-12T22:30:30.588101Z","shell.execute_reply":"2022-08-12T22:30:30.593878Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Bar Plot 'Sex' - Male=1, Female=0\nsns.set(style=\"darkgrid\")\ntitan_df['Sex'].value_counts().plot(kind='barh')","metadata":{"papermill":{"duration":0.180071,"end_time":"2022-08-08T18:28:28.500233","exception":false,"start_time":"2022-08-08T18:28:28.320162","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-12T22:30:30.920916Z","iopub.execute_input":"2022-08-12T22:30:30.921354Z","iopub.status.idle":"2022-08-12T22:30:31.156191Z","shell.execute_reply.started":"2022-08-12T22:30:30.921312Z","shell.execute_reply":"2022-08-12T22:30:31.154727Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 'Ticket' Feature\n# https://www.encyclopedia-titanica.org/community/threads/ticket-numbering-system.20348/\n# I don't think we pull out much value in this feature\n# So a simple cleanup on the ticket number is sufficient so the agents are removed from this feature\nticket_split = titan_df[\"Ticket\"].str.rsplit(\" \", n=1, expand=True)\nticket_split.columns = 'T0 T1'.split()\nticket_split = ticket_split.fillna(np.nan)\nticket_split['T1'] = ticket_split['T1'].fillna(ticket_split['T0'])\nticket_split['T1'] = ticket_split['T1'].str.replace('LINE', '0').str.strip()\nticket_split['T1'] = ticket_split['T1'].astype(int)\ndel ticket_split['T0']\nticket_split.columns = ['Ticketnumber']","metadata":{"papermill":{"duration":0.033624,"end_time":"2022-08-08T18:28:28.550828","exception":false,"start_time":"2022-08-08T18:28:28.517204","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-12T22:30:31.160083Z","iopub.execute_input":"2022-08-12T22:30:31.160960Z","iopub.status.idle":"2022-08-12T22:30:31.178861Z","shell.execute_reply.started":"2022-08-12T22:30:31.160915Z","shell.execute_reply":"2022-08-12T22:30:31.177578Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# concat ticketnummer\ntitan_df = pd.concat([titan_df, ticket_split], axis=1)\ndel titan_df['Ticket']","metadata":{"papermill":{"duration":0.028108,"end_time":"2022-08-08T18:28:28.596886","exception":false,"start_time":"2022-08-08T18:28:28.568778","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-12T22:30:31.203184Z","iopub.execute_input":"2022-08-12T22:30:31.203979Z","iopub.status.idle":"2022-08-12T22:30:31.213454Z","shell.execute_reply.started":"2022-08-12T22:30:31.203934Z","shell.execute_reply":"2022-08-12T22:30:31.212193Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 'Name' Feature\n# will be deleted, as it is no longer useful for the models\n# in the Date are better features to use, like sex and passengerID\ndel titan_df['Name']","metadata":{"papermill":{"duration":0.024961,"end_time":"2022-08-08T18:28:28.639461","exception":false,"start_time":"2022-08-08T18:28:28.614500","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-12T22:30:31.343227Z","iopub.execute_input":"2022-08-12T22:30:31.343726Z","iopub.status.idle":"2022-08-12T22:30:31.350336Z","shell.execute_reply.started":"2022-08-12T22:30:31.343686Z","shell.execute_reply":"2022-08-12T22:30:31.348964Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 'Cabin' Feature\n# https://www.encyclopedia-titanica.org/titanic-deckplans/\n# The cabin values consist of the deck and the room number. Hypothesis: This characteristic \n# should have a high influence on the forecast, like the ticket class and also correlate.\n# We should split this feature in number/deck and check nan\ntitan_df['Cabin'].isna().value_counts()","metadata":{"papermill":{"duration":0.028591,"end_time":"2022-08-08T18:28:28.685044","exception":false,"start_time":"2022-08-08T18:28:28.656453","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-12T22:30:31.481428Z","iopub.execute_input":"2022-08-12T22:30:31.481916Z","iopub.status.idle":"2022-08-12T22:30:31.493139Z","shell.execute_reply.started":"2022-08-12T22:30:31.481878Z","shell.execute_reply":"2022-08-12T22:30:31.492193Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"We need to we must impute the missing<br>\nhttps://titanic.fandom.com/wiki/First_Class_Staterooms<br>\nFirst Class - Decks A/B/C<br>\nMost of them on B(101)/C(134) - highest on Boatdeck(6) - A(36) lowest on D(49) and F(4)<br>\nSum = 330 First Class rooms<br>\nprice from £400 to £870<br>\n<br>\nhttps://titanic.fandom.com/wiki/Second_Class_Cabins<br>\nSecond Class - Decks D(39)/E(65)/(F64)<br>\nSum = 168 Rooms<br>\n<br><br> \nhttps://titanic.fandom.com/wiki/Third_Class_cabins<br>\nThird Class - Decks D/E/F/G<br>\nIt is true that the lower the deck the higher the probability of drownin\nImputation is vague because the rate of missing values is high. We use this \nimputation for testing purposes to see if we can increase the accuracy.\nAfter Testing various imputations. I decided to drop this feature ","metadata":{"papermill":{"duration":0.016203,"end_time":"2022-08-08T18:28:28.718111","exception":false,"start_time":"2022-08-08T18:28:28.701908","status":"completed"},"tags":[]}},{"cell_type":"code","source":"# save df without 'Cabin' Feature\n# dropping the feature for a direct comparison\ntitan_df_wo_cabin = titan_df.copy()\ndel titan_df_wo_cabin['Cabin']\ndel titan_df_wo_cabin['PassengerId']","metadata":{"papermill":{"duration":0.027591,"end_time":"2022-08-08T18:28:28.762796","exception":false,"start_time":"2022-08-08T18:28:28.735205","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-12T22:30:31.639139Z","iopub.execute_input":"2022-08-12T22:30:31.639663Z","iopub.status.idle":"2022-08-12T22:30:31.648343Z","shell.execute_reply.started":"2022-08-12T22:30:31.639616Z","shell.execute_reply":"2022-08-12T22:30:31.647401Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Data Visualization & Preparation","metadata":{"papermill":{"duration":0.017101,"end_time":"2022-08-08T18:28:28.797145","exception":false,"start_time":"2022-08-08T18:28:28.780044","status":"completed"},"tags":[]}},{"cell_type":"code","source":"# OneHot Encoding to transform the categorial features\noh_enc = OneHotEncoder(handle_unknown='ignore')","metadata":{"papermill":{"duration":0.023421,"end_time":"2022-08-08T18:28:28.837845","exception":false,"start_time":"2022-08-08T18:28:28.814424","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-12T22:30:31.816220Z","iopub.execute_input":"2022-08-12T22:30:31.817699Z","iopub.status.idle":"2022-08-12T22:30:31.822951Z","shell.execute_reply.started":"2022-08-12T22:30:31.817646Z","shell.execute_reply":"2022-08-12T22:30:31.821727Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Correlation Plots\ncorr = titan_df_wo_cabin.corr()\nmask = np.triu(np.ones_like(corr, dtype=bool))\nsns.set(style=\"darkgrid\")\nf, ax = plt.subplots(figsize=(11, 9))\ncmap = sns.diverging_palette(230, 20, as_cmap=True)\nsns.heatmap(corr, mask=mask, cmap=cmap, vmax=.3, center=0,\n            square=True, linewidths=.5, cbar_kws={\"shrink\": .5})\nplt.title('titan_df_wo_cabin corr plot')","metadata":{"papermill":{"duration":0.346696,"end_time":"2022-08-08T18:28:29.249110","exception":false,"start_time":"2022-08-08T18:28:28.902414","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-12T22:30:31.974811Z","iopub.execute_input":"2022-08-12T22:30:31.975293Z","iopub.status.idle":"2022-08-12T22:30:32.401729Z","shell.execute_reply.started":"2022-08-12T22:30:31.975255Z","shell.execute_reply":"2022-08-12T22:30:32.400504Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# z-score / standard scaler\nscaled_columns = titan_df_wo_cabin.columns\nscaled_columns_clean = list(scaled_columns)\ndel scaled_columns_clean[0]\n\ntitan_wo_survived = titan_df_wo_cabin.copy()\ndel titan_wo_survived['Survived']\n\nscaled_features = StandardScaler().fit_transform(titan_wo_survived.values)\nscaled_features_df = pd.DataFrame(scaled_features, index=titan_df_wo_cabin.index, columns=scaled_columns_clean)\nfeatures_df_full = pd.concat([scaled_features_df, titan_df_wo_cabin['Survived'].astype(int)], axis=1)","metadata":{"papermill":{"duration":0.030328,"end_time":"2022-08-08T18:28:29.298295","exception":false,"start_time":"2022-08-08T18:28:29.267967","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-12T22:30:32.404256Z","iopub.execute_input":"2022-08-12T22:30:32.405027Z","iopub.status.idle":"2022-08-12T22:30:32.417872Z","shell.execute_reply.started":"2022-08-12T22:30:32.404976Z","shell.execute_reply":"2022-08-12T22:30:32.416399Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scaled_columns","metadata":{"execution":{"iopub.status.busy":"2022-08-12T22:30:32.419941Z","iopub.execute_input":"2022-08-12T22:30:32.420400Z","iopub.status.idle":"2022-08-12T22:30:32.431544Z","shell.execute_reply.started":"2022-08-12T22:30:32.420362Z","shell.execute_reply":"2022-08-12T22:30:32.430501Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"features_df_full.describe()","metadata":{"papermill":{"duration":0.058249,"end_time":"2022-08-08T18:28:29.375162","exception":false,"start_time":"2022-08-08T18:28:29.316913","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-12T22:30:32.433452Z","iopub.execute_input":"2022-08-12T22:30:32.434032Z","iopub.status.idle":"2022-08-12T22:30:32.483927Z","shell.execute_reply.started":"2022-08-12T22:30:32.433996Z","shell.execute_reply":"2022-08-12T22:30:32.483064Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Part 2: Train and evaluate different Models (Random Forest)\n","metadata":{"papermill":{"duration":0.017928,"end_time":"2022-08-08T18:28:29.411347","exception":false,"start_time":"2022-08-08T18:28:29.393419","status":"completed"},"tags":[]}},{"cell_type":"markdown","source":"#### RF","metadata":{"papermill":{"duration":0.01859,"end_time":"2022-08-08T18:28:29.449034","exception":false,"start_time":"2022-08-08T18:28:29.430444","status":"completed"},"tags":[]}},{"cell_type":"code","source":"# df train/test split\ntitan_df_wo_cabin_xgb = features_df_full.copy()\nX = titan_df_wo_cabin_xgb\ny = X.pop('Survived').values\n\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=RAND_SEED)","metadata":{"papermill":{"duration":0.028676,"end_time":"2022-08-08T18:28:29.496028","exception":false,"start_time":"2022-08-08T18:28:29.467352","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-12T22:30:32.570449Z","iopub.execute_input":"2022-08-12T22:30:32.571211Z","iopub.status.idle":"2022-08-12T22:30:32.580520Z","shell.execute_reply.started":"2022-08-12T22:30:32.571155Z","shell.execute_reply":"2022-08-12T22:30:32.579407Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# %% Modell aufbauen\nmodel = RandomForestClassifier()\npipeline = Pipeline([\n    ('model', model)\n])\n\nparam_grid = {\n    'model__max_features': ['sqrt', 'log2'],\n    'model__max_depth': [7, 10, 12, 14],\n    'model__n_estimators': [10, 50, 100, 500],\n    'model__random_state': [RAND_SEED],\n    'model__criterion': ['gini']\n}\n\n# Hyperparameter Optimierung via GridSearch\ngrid_rf = GridSearchCV(pipeline, param_grid, cv=5, n_jobs=-1, scoring='accuracy', verbose=2, return_train_score=True)\n\nif __name__ == '__main__':\n    grid_rf.fit(X_train, y_train)\n\n# %%\n# # Best Result RF\n# {'model__criterion': 'gini',\n#  'model__max_depth': 14,\n#  'model__max_features': 'sqrt',\n#  'model__n_estimators': 100,\n#  'model__random_state': 1337}","metadata":{"execution":{"iopub.status.busy":"2022-08-12T22:30:32.732417Z","iopub.execute_input":"2022-08-12T22:30:32.733108Z","iopub.status.idle":"2022-08-12T22:30:56.636433Z","shell.execute_reply.started":"2022-08-12T22:30:32.733070Z","shell.execute_reply":"2022-08-12T22:30:56.635191Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"best_params = grid_rf.best_params_\nbest_params","metadata":{"execution":{"iopub.status.busy":"2022-08-12T22:30:56.638738Z","iopub.execute_input":"2022-08-12T22:30:56.639278Z","iopub.status.idle":"2022-08-12T22:30:56.648519Z","shell.execute_reply.started":"2022-08-12T22:30:56.639232Z","shell.execute_reply":"2022-08-12T22:30:56.647040Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = RandomForestClassifier(criterion='gini', max_depth=10, max_features='sqrt', n_estimators=100, \n                               random_state=RAND_SEED)\nmodel.fit(X_train, y_train)","metadata":{"papermill":{"duration":0.182511,"end_time":"2022-08-08T18:28:29.697127","exception":false,"start_time":"2022-08-08T18:28:29.514616","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-12T22:30:56.650450Z","iopub.execute_input":"2022-08-12T22:30:56.651145Z","iopub.status.idle":"2022-08-12T22:30:56.862511Z","shell.execute_reply.started":"2022-08-12T22:30:56.651092Z","shell.execute_reply":"2022-08-12T22:30:56.861215Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# feature Importance\nperm_importance = permutation_importance(model, X_test, y_test, n_jobs=-1, random_state=RAND_SEED)\nsorted_idx = perm_importance.importances_mean.argsort()\nplt.barh(X_test.columns[sorted_idx], perm_importance.importances_mean[sorted_idx])\nplt.xlabel(\"Permutation Importance\")\nplt.show()","metadata":{"papermill":{"duration":2.379551,"end_time":"2022-08-08T18:28:32.094625","exception":false,"start_time":"2022-08-08T18:28:29.715074","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-12T22:30:56.865192Z","iopub.execute_input":"2022-08-12T22:30:56.865581Z","iopub.status.idle":"2022-08-12T22:30:57.911070Z","shell.execute_reply.started":"2022-08-12T22:30:56.865546Z","shell.execute_reply":"2022-08-12T22:30:57.909882Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_predicts_train = model.predict(X_train)\nacc_train = accuracy_score(y_train, train_predicts_train)\nprint(round(acc_train*100, 2))\n\ntrain_predicts_test = model.predict(X_test)\nacc_test = accuracy_score(y_test, train_predicts_test)\nprint(round(acc_test*100, 2))","metadata":{"papermill":{"duration":0.066757,"end_time":"2022-08-08T18:28:32.180054","exception":false,"start_time":"2022-08-08T18:28:32.113297","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-12T22:30:57.912480Z","iopub.execute_input":"2022-08-12T22:30:57.912787Z","iopub.status.idle":"2022-08-12T22:30:57.964111Z","shell.execute_reply.started":"2022-08-12T22:30:57.912758Z","shell.execute_reply":"2022-08-12T22:30:57.963221Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Part 3: Forecasting the Testfile","metadata":{"papermill":{"duration":0.019373,"end_time":"2022-08-08T18:28:32.219673","exception":false,"start_time":"2022-08-08T18:28:32.200300","status":"completed"},"tags":[]}},{"cell_type":"code","source":"# load the testfile\ntitan_df_valid = pd.read_csv('../input/titanic/test.csv')\npassengerid = titan_df_valid.copy()['PassengerId']","metadata":{"papermill":{"duration":0.037157,"end_time":"2022-08-08T18:28:32.275715","exception":false,"start_time":"2022-08-08T18:28:32.238558","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-12T22:30:57.965282Z","iopub.execute_input":"2022-08-12T22:30:57.965956Z","iopub.status.idle":"2022-08-12T22:30:57.976718Z","shell.execute_reply.started":"2022-08-12T22:30:57.965915Z","shell.execute_reply":"2022-08-12T22:30:57.975451Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### prepare the text.csv for the forcasting","metadata":{"papermill":{"duration":0.018985,"end_time":"2022-08-08T18:28:32.314331","exception":false,"start_time":"2022-08-08T18:28:32.295346","status":"completed"},"tags":[]}},{"cell_type":"code","source":"# creating a 'total_family' feature\ntitan_df_valid['total_family'] = titan_df_valid['SibSp'] + titan_df_valid['Parch']","metadata":{"papermill":{"duration":0.029419,"end_time":"2022-08-08T18:28:32.363550","exception":false,"start_time":"2022-08-08T18:28:32.334131","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-12T22:30:57.978625Z","iopub.execute_input":"2022-08-12T22:30:57.979854Z","iopub.status.idle":"2022-08-12T22:30:57.986766Z","shell.execute_reply.started":"2022-08-12T22:30:57.979805Z","shell.execute_reply":"2022-08-12T22:30:57.985478Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# transform 'Sex' feature\ntitan_df_valid['Sex'] = titan_df_valid['Sex'].map({'male': 1, 'female': 0})","metadata":{"papermill":{"duration":0.029692,"end_time":"2022-08-08T18:28:32.412781","exception":false,"start_time":"2022-08-08T18:28:32.383089","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-12T22:30:57.988432Z","iopub.execute_input":"2022-08-12T22:30:57.989190Z","iopub.status.idle":"2022-08-12T22:30:57.998322Z","shell.execute_reply.started":"2022-08-12T22:30:57.989144Z","shell.execute_reply":"2022-08-12T22:30:57.997176Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 'Embarked' Feature\ntitan_df_valid['Embarked'] = titan_df['Embarked'].map({'C': 1, 'S': 2, 'Q':3})","metadata":{"execution":{"iopub.status.busy":"2022-08-12T22:30:57.999806Z","iopub.execute_input":"2022-08-12T22:30:58.000888Z","iopub.status.idle":"2022-08-12T22:30:58.011640Z","shell.execute_reply.started":"2022-08-12T22:30:58.000851Z","shell.execute_reply":"2022-08-12T22:30:58.010227Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# clean up 'ticket' - feature\n# 'Ticket' Feature\nticket_split_valid = titan_df_valid[\"Ticket\"].str.rsplit(\" \", n=1, expand=True)\nticket_split_valid.columns = 'T0 T1'.split()\nticket_split_valid = ticket_split_valid.fillna(np.nan)\nticket_split_valid['T1'] = ticket_split_valid['T1'].fillna(ticket_split_valid['T0'])\nticket_split_valid['T1'] = ticket_split_valid['T1'].astype(int)\ndel ticket_split_valid['T0']\nticket_split_valid.columns = ['Ticketnumber']","metadata":{"papermill":{"duration":0.031301,"end_time":"2022-08-08T18:28:32.463422","exception":false,"start_time":"2022-08-08T18:28:32.432121","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-12T22:30:58.016028Z","iopub.execute_input":"2022-08-12T22:30:58.016573Z","iopub.status.idle":"2022-08-12T22:30:58.030372Z","shell.execute_reply.started":"2022-08-12T22:30:58.016523Z","shell.execute_reply":"2022-08-12T22:30:58.029184Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# concat ticketnummer\ntitan_df_valid = pd.concat([titan_df_valid, ticket_split_valid], axis=1)\ndel titan_df_valid['Ticket']","metadata":{"papermill":{"duration":0.028602,"end_time":"2022-08-08T18:28:32.510953","exception":false,"start_time":"2022-08-08T18:28:32.482351","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-12T22:30:58.031711Z","iopub.execute_input":"2022-08-12T22:30:58.032452Z","iopub.status.idle":"2022-08-12T22:30:58.045997Z","shell.execute_reply.started":"2022-08-12T22:30:58.032418Z","shell.execute_reply":"2022-08-12T22:30:58.044658Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# del columns\ndel titan_df_valid['Cabin']\ndel titan_df_valid['Name']\ndel titan_df_valid['PassengerId']","metadata":{"papermill":{"duration":0.028065,"end_time":"2022-08-08T18:28:32.559281","exception":false,"start_time":"2022-08-08T18:28:32.531216","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-12T22:30:58.047637Z","iopub.execute_input":"2022-08-12T22:30:58.048004Z","iopub.status.idle":"2022-08-12T22:30:58.059055Z","shell.execute_reply.started":"2022-08-12T22:30:58.047971Z","shell.execute_reply":"2022-08-12T22:30:58.057722Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"titan_df_valid['Age'] = titan_df_valid['Age'].fillna(0)\ntitan_df_valid['Fare'] = titan_df_valid['Fare'].fillna(0)\ntitan_df_valid['Embarked'] = titan_df_valid['Embarked'].fillna(0)","metadata":{"papermill":{"duration":0.026716,"end_time":"2022-08-08T18:28:32.604533","exception":false,"start_time":"2022-08-08T18:28:32.577817","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-12T22:30:58.060805Z","iopub.execute_input":"2022-08-12T22:30:58.061198Z","iopub.status.idle":"2022-08-12T22:30:58.069106Z","shell.execute_reply.started":"2022-08-12T22:30:58.061161Z","shell.execute_reply":"2022-08-12T22:30:58.068177Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# scaling\nscaled_features = StandardScaler().fit_transform(titan_df_valid.values)\nscaled_features_df = pd.DataFrame(scaled_features, index=titan_df_valid.index, columns=titan_df_valid.columns)","metadata":{"papermill":{"duration":0.027598,"end_time":"2022-08-08T18:28:32.702277","exception":false,"start_time":"2022-08-08T18:28:32.674679","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-12T22:30:58.070631Z","iopub.execute_input":"2022-08-12T22:30:58.070981Z","iopub.status.idle":"2022-08-12T22:30:58.081719Z","shell.execute_reply.started":"2022-08-12T22:30:58.070949Z","shell.execute_reply":"2022-08-12T22:30:58.080218Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# since RF provided the best results, the forecasts are made on this model\npredictions = model.predict(scaled_features_df)\n\npredictions_titanic = pd.DataFrame({'PassengerId': passengerid, 'Survived': predictions})\npredictions_titanic = predictions_titanic.astype(int)\npredictions_titanic.to_csv('./submission.csv', index=False)\nprint(predictions_titanic)","metadata":{"papermill":{"duration":0.053503,"end_time":"2022-08-08T18:28:32.773909","exception":false,"start_time":"2022-08-08T18:28:32.720406","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-12T22:30:58.083539Z","iopub.execute_input":"2022-08-12T22:30:58.084016Z","iopub.status.idle":"2022-08-12T22:30:58.129548Z","shell.execute_reply.started":"2022-08-12T22:30:58.083978Z","shell.execute_reply":"2022-08-12T22:30:58.128552Z"},"trusted":true},"execution_count":null,"outputs":[]}]}