{"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":"<a id=\"0\"></a>\n# Introduction\n\nThe sinking of Titanic is one of the most notorious shipwreck in the history. In 1912, during her voyage, the Titanic sank after colliding with an iceberg, kiling 1502 out of 2224 passengers and crew.\n\n<font color = \"blue\">\nContent : \n\n1. [Load and Check Data](#1)\n1. [Variable Description](#2)\n    * [Univariate Variable Analysis](#3)\n        * [Categorical Variable](#4)\n        * [Numerical Variable](#5)\n1. [Basic Data Analysis](#6)\n1. [Outlier Detection](#7)\n1. [Missing Value](#8)\n    * [Find Missing Value](#9)\n    * [Fill Missing Value](#10)\n1. [Visualization](#11)\n    * [Correlation Between Sibsp -- Parch -- Age -- Fare -- Survived](#12)\n    * [SipSb -- Survived](#13)\n    * [Parch -- Survived](#14)\n    * [Pclass -- Survived](#15)\n    * [Age -- Survived](#16)\n    * [Pclass -- Survived -- Age](#17)\n    * [Embarked -- Sex -- Pclass -- Survived](#18)\n    * [Embarked -- Sex -- Fare -- Survived](#19)\n    * [Fill Missing : Age Feature](#20)","metadata":{}},{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\nimport matplotlib.pyplot as plt\nplt.style.use(\"seaborn-whitegrid\")\n\nimport seaborn as sns\n\nfrom collections import Counter\n\nimport warnings\nwarnings.filterwarnings(\"ignore\")\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-08-02T06:37:46.863216Z","iopub.execute_input":"2022-08-02T06:37:46.863954Z","iopub.status.idle":"2022-08-02T06:37:48.035492Z","shell.execute_reply.started":"2022-08-02T06:37:46.863832Z","shell.execute_reply":"2022-08-02T06:37:48.034301Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"[Yukarı](#0)<a id=\"1\"></a>\n# Load and Check Data","metadata":{}},{"cell_type":"code","source":"train_df = pd.read_csv(\"/kaggle/input/titanic/train.csv\")\ntest_df = pd.read_csv(\"/kaggle/input/titanic/test.csv\")\ntest_PassengerId = test_df[\"PassengerId\"]","metadata":{"execution":{"iopub.status.busy":"2022-08-02T06:37:48.037622Z","iopub.execute_input":"2022-08-02T06:37:48.037994Z","iopub.status.idle":"2022-08-02T06:37:48.072128Z","shell.execute_reply.started":"2022-08-02T06:37:48.037960Z","shell.execute_reply":"2022-08-02T06:37:48.071104Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.columns","metadata":{"execution":{"iopub.status.busy":"2022-08-02T06:37:48.073942Z","iopub.execute_input":"2022-08-02T06:37:48.074762Z","iopub.status.idle":"2022-08-02T06:37:48.085239Z","shell.execute_reply.started":"2022-08-02T06:37:48.074719Z","shell.execute_reply":"2022-08-02T06:37:48.083920Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-02T06:37:48.087262Z","iopub.execute_input":"2022-08-02T06:37:48.088093Z","iopub.status.idle":"2022-08-02T06:37:48.112067Z","shell.execute_reply.started":"2022-08-02T06:37:48.088047Z","shell.execute_reply":"2022-08-02T06:37:48.111209Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.describe()","metadata":{"execution":{"iopub.status.busy":"2022-08-02T06:37:48.114473Z","iopub.execute_input":"2022-08-02T06:37:48.115257Z","iopub.status.idle":"2022-08-02T06:37:48.156036Z","shell.execute_reply.started":"2022-08-02T06:37:48.115213Z","shell.execute_reply":"2022-08-02T06:37:48.154829Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"[Yukarı](#0)<a id=\"2\"></a>\n# Variable Description\n\n1. PassengerId : unique id number to each passenger\n1. Survived : passenger survive(1) or died(0)\n1. Pclass : passenger class\n1. Name : name\n1. Sex : gender of passenger\n1. Age : age of passenger\n1. SibSp : number of siblings/spouses\n1. Parch : number of parents/children\n1. Ticket : ticket number\n1. Fare : amount of money spend on ticket\n1. Cabin : cabin category\n1. Embarked : port where passenger embark (C = Cherbourg, Q = Queenstown, S = Southhampton)","metadata":{}},{"cell_type":"code","source":"train_df.info()","metadata":{"execution":{"iopub.status.busy":"2022-08-02T06:37:48.157726Z","iopub.execute_input":"2022-08-02T06:37:48.158180Z","iopub.status.idle":"2022-08-02T06:37:48.176015Z","shell.execute_reply.started":"2022-08-02T06:37:48.158138Z","shell.execute_reply":"2022-08-02T06:37:48.175004Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"* float64(2) : Fare and Age\n* int64(5) : PassengerId , Survived , Pclass, SibSp and Parch\n* object(5) : Name , Sex , Ticket , Cabin and Embarked","metadata":{}},{"cell_type":"markdown","source":"[Yukarı](#0)<a id=\"3\"></a>\n# Univariate Variable Analysis\n\n* Categorical Variable : Survived, Sex, Pclass, Embarked, Cabin, Name, Ticket, SibSp and Parch\n* Numerical Variable : Fare, Age and PassengerId","metadata":{}},{"cell_type":"markdown","source":"[Yukarı](#0)<a id=\"4\"></a>\n## Categorical Variable ","metadata":{}},{"cell_type":"code","source":"def bar_plot(variable):\n    ###    input : variable ex: \"Sex\"\n    ###    output: bar plot & value count\n    \n    # get feature\n    var = train_df[variable]\n    # count number of categorical variable(value)\n    varValue = var.value_counts()\n    \n    # visualize\n    plt.figure(figsize = (9,3))\n    plt.bar(varValue.index,varValue)\n    plt.xticks(varValue.index,varValue.index.values)\n    plt.ylabel(\"Frequency\")\n    plt.title(variable)\n    plt.show()\n    print(\"{} : \\n {}\".format(variable,varValue))","metadata":{"execution":{"iopub.status.busy":"2022-08-02T06:37:48.179679Z","iopub.execute_input":"2022-08-02T06:37:48.180086Z","iopub.status.idle":"2022-08-02T06:37:48.187397Z","shell.execute_reply.started":"2022-08-02T06:37:48.180048Z","shell.execute_reply":"2022-08-02T06:37:48.186140Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"category1 = [\"Survived\",\"Sex\",\"Pclass\",\"Embarked\",\"SibSp\",\"Parch\"]\nfor c in category1:\n    bar_plot(c)","metadata":{"execution":{"iopub.status.busy":"2022-08-02T06:37:48.189124Z","iopub.execute_input":"2022-08-02T06:37:48.189955Z","iopub.status.idle":"2022-08-02T06:37:49.191984Z","shell.execute_reply.started":"2022-08-02T06:37:48.189889Z","shell.execute_reply":"2022-08-02T06:37:49.190767Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"category2 = [\"Cabin\",\"Name\",\"Ticket\"]\nfor c in category2:\n    print(\"{} \\n\".format(train_df[c].value_counts()))","metadata":{"execution":{"iopub.status.busy":"2022-08-02T06:37:49.193462Z","iopub.execute_input":"2022-08-02T06:37:49.194073Z","iopub.status.idle":"2022-08-02T06:37:49.205941Z","shell.execute_reply.started":"2022-08-02T06:37:49.194026Z","shell.execute_reply":"2022-08-02T06:37:49.204748Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"[Yukarı](#0)<a id=\"5\"></a>\n## Numerical Variable ","metadata":{}},{"cell_type":"code","source":"def plot_hist(variable):\n    plt.figure(figsize = (9,3))\n    plt.hist(train_df[variable], bins=50)\n    plt.xlabel(variable)\n    plt.ylabel(\"Frequency\")\n    plt.title(\"{} distibution with hist\".format(variable))\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-02T06:37:49.207640Z","iopub.execute_input":"2022-08-02T06:37:49.208386Z","iopub.status.idle":"2022-08-02T06:37:49.215192Z","shell.execute_reply.started":"2022-08-02T06:37:49.208340Z","shell.execute_reply":"2022-08-02T06:37:49.214293Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"numericVar = [\"Fare\",\"Age\",\"PassengerId\"]\nfor n in numericVar:\n    plot_hist(n)","metadata":{"execution":{"iopub.status.busy":"2022-08-02T06:37:49.216669Z","iopub.execute_input":"2022-08-02T06:37:49.217313Z","iopub.status.idle":"2022-08-02T06:37:50.059390Z","shell.execute_reply.started":"2022-08-02T06:37:49.217272Z","shell.execute_reply":"2022-08-02T06:37:50.058514Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"[Yukarı](#0)<a id=\"6\"></a>\n# Basic Data Analysis\n\n* Pclass - Survived\n* Sex - Survived\n* SibSp - Survived\n* Parch - Survived","metadata":{}},{"cell_type":"code","source":"# Pclass vs Survived\ntrain_df[[\"Pclass\",\"Survived\"]].groupby([\"Pclass\"],as_index = False).mean().sort_values(by=\"Survived\", ascending = False)","metadata":{"execution":{"iopub.status.busy":"2022-08-02T06:37:50.060753Z","iopub.execute_input":"2022-08-02T06:37:50.061364Z","iopub.status.idle":"2022-08-02T06:37:50.077179Z","shell.execute_reply.started":"2022-08-02T06:37:50.061329Z","shell.execute_reply":"2022-08-02T06:37:50.075863Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Sex vs Survived\ntrain_df[[\"Sex\",\"Survived\"]].groupby([\"Sex\"],as_index = False).mean().sort_values(by=\"Survived\", ascending = False)","metadata":{"execution":{"iopub.status.busy":"2022-08-02T06:37:50.078860Z","iopub.execute_input":"2022-08-02T06:37:50.079949Z","iopub.status.idle":"2022-08-02T06:37:50.098165Z","shell.execute_reply.started":"2022-08-02T06:37:50.079883Z","shell.execute_reply":"2022-08-02T06:37:50.097207Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# SibSp vs Survived\ntrain_df[[\"SibSp\",\"Survived\"]].groupby([\"SibSp\"],as_index = False).mean().sort_values(by=\"Survived\", ascending = False)","metadata":{"execution":{"iopub.status.busy":"2022-08-02T06:37:50.103766Z","iopub.execute_input":"2022-08-02T06:37:50.104259Z","iopub.status.idle":"2022-08-02T06:37:50.121125Z","shell.execute_reply.started":"2022-08-02T06:37:50.104219Z","shell.execute_reply":"2022-08-02T06:37:50.119969Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Parch vs Survived\ntrain_df[[\"Parch\",\"Survived\"]].groupby([\"Parch\"],as_index = False).mean().sort_values(by=\"Survived\", ascending = False)","metadata":{"execution":{"iopub.status.busy":"2022-08-02T06:37:50.122373Z","iopub.execute_input":"2022-08-02T06:37:50.122707Z","iopub.status.idle":"2022-08-02T06:37:50.138785Z","shell.execute_reply.started":"2022-08-02T06:37:50.122676Z","shell.execute_reply":"2022-08-02T06:37:50.137784Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"[Yukarı](#0)<a id=\"7\"></a>\n# Outlier Detection","metadata":{}},{"cell_type":"code","source":"def detect_outliers(df,features):\n    outlier_indices= []\n    \n    for c in features:\n        #1st quartile\n        Q1 = np.percentile(df[c],25)\n        #3rd quartile\n        Q3 = np.percentile(df[c],75)\n        #IQR\n        IQR = Q3 - Q1\n        #Outlier step\n        outlier_step = IQR * 1.5\n        #detect outlier and their indeces\n        outlier_list_col = df[(df[c]<Q1-outlier_step)|(df[c]>Q3+outlier_step)].index\n        #store indeces\n        outlier_indices.extend(outlier_list_col)\n        \n    outlier_indices = Counter(outlier_indices)\n    multiple_outliers = list(i for i, v in outlier_indices.items() if v>2)\n    return multiple_outliers","metadata":{"execution":{"iopub.status.busy":"2022-08-02T06:37:50.140017Z","iopub.execute_input":"2022-08-02T06:37:50.140986Z","iopub.status.idle":"2022-08-02T06:37:50.148821Z","shell.execute_reply.started":"2022-08-02T06:37:50.140948Z","shell.execute_reply":"2022-08-02T06:37:50.147539Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.loc[detect_outliers(train_df,[\"Age\",\"SibSp\",\"Parch\",\"Fare\"])]","metadata":{"execution":{"iopub.status.busy":"2022-08-02T06:37:50.150800Z","iopub.execute_input":"2022-08-02T06:37:50.151511Z","iopub.status.idle":"2022-08-02T06:37:50.183200Z","shell.execute_reply.started":"2022-08-02T06:37:50.151471Z","shell.execute_reply":"2022-08-02T06:37:50.181996Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# drop outliers\ntrain_df= train_df.drop(detect_outliers(train_df,[\"Age\",\"SibSp\",\"Parch\",\"Fare\"])) ","metadata":{"execution":{"iopub.status.busy":"2022-08-02T06:37:50.184480Z","iopub.execute_input":"2022-08-02T06:37:50.184784Z","iopub.status.idle":"2022-08-02T06:37:50.196717Z","shell.execute_reply.started":"2022-08-02T06:37:50.184756Z","shell.execute_reply":"2022-08-02T06:37:50.195828Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"[Yukarı](#0)<a id=\"8\"></a>\n# Missing Value\n\n* Find Missing Value\n* Fill Missing Value","metadata":{}},{"cell_type":"code","source":"train_df_len = len(train_df)\ntrain_df = pd.concat([train_df,test_df],axis = 0).reset_index(drop = True)","metadata":{"execution":{"iopub.status.busy":"2022-08-02T06:37:50.198657Z","iopub.execute_input":"2022-08-02T06:37:50.199522Z","iopub.status.idle":"2022-08-02T06:37:50.209430Z","shell.execute_reply.started":"2022-08-02T06:37:50.199476Z","shell.execute_reply":"2022-08-02T06:37:50.208357Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-02T06:37:50.211069Z","iopub.execute_input":"2022-08-02T06:37:50.211725Z","iopub.status.idle":"2022-08-02T06:37:50.230389Z","shell.execute_reply.started":"2022-08-02T06:37:50.211682Z","shell.execute_reply":"2022-08-02T06:37:50.229225Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"[Yukarı](#0)<a id=\"9\"></a>\n## Find Missing Value","metadata":{}},{"cell_type":"code","source":"train_df.columns[train_df.isnull().any()]","metadata":{"execution":{"iopub.status.busy":"2022-08-02T06:37:50.231996Z","iopub.execute_input":"2022-08-02T06:37:50.232690Z","iopub.status.idle":"2022-08-02T06:37:50.242919Z","shell.execute_reply.started":"2022-08-02T06:37:50.232646Z","shell.execute_reply":"2022-08-02T06:37:50.241698Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-02T06:37:50.244579Z","iopub.execute_input":"2022-08-02T06:37:50.245674Z","iopub.status.idle":"2022-08-02T06:37:50.257505Z","shell.execute_reply.started":"2022-08-02T06:37:50.245630Z","shell.execute_reply":"2022-08-02T06:37:50.256361Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"[Yukarı](#0)<a id=\"10\"></a>\n## Fill Missing Value\n\n* Embarked has 2 missing value\n* Fare has only 1","metadata":{}},{"cell_type":"code","source":"train_df[train_df[\"Embarked\"].isnull()]","metadata":{"execution":{"iopub.status.busy":"2022-08-02T06:37:50.259064Z","iopub.execute_input":"2022-08-02T06:37:50.260250Z","iopub.status.idle":"2022-08-02T06:37:50.278595Z","shell.execute_reply.started":"2022-08-02T06:37:50.260206Z","shell.execute_reply":"2022-08-02T06:37:50.277347Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.boxplot(column=\"Fare\",by=\"Embarked\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-02T06:37:50.280381Z","iopub.execute_input":"2022-08-02T06:37:50.281322Z","iopub.status.idle":"2022-08-02T06:37:50.502776Z","shell.execute_reply.started":"2022-08-02T06:37:50.281276Z","shell.execute_reply":"2022-08-02T06:37:50.501371Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df[\"Embarked\"] = train_df[\"Embarked\"].fillna(\"C\")","metadata":{"execution":{"iopub.status.busy":"2022-08-02T06:37:50.504467Z","iopub.execute_input":"2022-08-02T06:37:50.504959Z","iopub.status.idle":"2022-08-02T06:37:50.511542Z","shell.execute_reply.started":"2022-08-02T06:37:50.504887Z","shell.execute_reply":"2022-08-02T06:37:50.510397Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df[train_df[\"Embarked\"].isnull()]","metadata":{"execution":{"iopub.status.busy":"2022-08-02T06:37:50.513246Z","iopub.execute_input":"2022-08-02T06:37:50.513962Z","iopub.status.idle":"2022-08-02T06:37:50.529970Z","shell.execute_reply.started":"2022-08-02T06:37:50.513891Z","shell.execute_reply":"2022-08-02T06:37:50.528784Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df[train_df[\"Fare\"].isnull()]","metadata":{"execution":{"iopub.status.busy":"2022-08-02T06:37:50.531578Z","iopub.execute_input":"2022-08-02T06:37:50.532270Z","iopub.status.idle":"2022-08-02T06:37:50.549991Z","shell.execute_reply.started":"2022-08-02T06:37:50.532228Z","shell.execute_reply":"2022-08-02T06:37:50.548766Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df[\"Fare\"] = train_df[\"Fare\"].fillna(np.mean(train_df[train_df[\"Pclass\"] == 3][\"Fare\"]))","metadata":{"execution":{"iopub.status.busy":"2022-08-02T06:37:50.551885Z","iopub.execute_input":"2022-08-02T06:37:50.552339Z","iopub.status.idle":"2022-08-02T06:37:50.560360Z","shell.execute_reply.started":"2022-08-02T06:37:50.552297Z","shell.execute_reply":"2022-08-02T06:37:50.559211Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df[train_df[\"Fare\"].isnull()]","metadata":{"execution":{"iopub.status.busy":"2022-08-02T06:37:50.561933Z","iopub.execute_input":"2022-08-02T06:37:50.562973Z","iopub.status.idle":"2022-08-02T06:37:50.576631Z","shell.execute_reply.started":"2022-08-02T06:37:50.562769Z","shell.execute_reply":"2022-08-02T06:37:50.575460Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"[Yukarı](#0)<a id=\"11\"></a>\n# Visualization","metadata":{}},{"cell_type":"markdown","source":"<a id=\"12\"></a>\n## Correlation Between Sibsp -- Parch -- Age -- Fare -- Survived","metadata":{}},{"cell_type":"code","source":"list1 = [\"SibSp\",\"Parch\",\"Age\",\"Fare\",\"Survived\"]\nsns.heatmap(train_df[list1].corr(), annot = True , fmt = \".2f\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-02T06:37:50.577774Z","iopub.execute_input":"2022-08-02T06:37:50.578389Z","iopub.status.idle":"2022-08-02T06:37:50.877299Z","shell.execute_reply.started":"2022-08-02T06:37:50.578358Z","shell.execute_reply":"2022-08-02T06:37:50.876208Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Fare feature seems to have correlation with Survived feature (0.26).","metadata":{}},{"cell_type":"markdown","source":"[Yukarı](#0)<a id=\"13\"></a>\n## SipSb -- Survived","metadata":{}},{"cell_type":"code","source":"g = sns.factorplot(x = \"SibSp\", y = \"Survived\", data = train_df, kind = \"bar\", size = 6)\ng.set_ylabels(\"Survived Probability\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-02T06:37:50.878777Z","iopub.execute_input":"2022-08-02T06:37:50.879120Z","iopub.status.idle":"2022-08-02T06:37:51.305073Z","shell.execute_reply.started":"2022-08-02T06:37:50.879090Z","shell.execute_reply":"2022-08-02T06:37:51.304033Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"* Having a lot of SibSp have less chance to survive.\n* If SibSp == 0 or 1 or 2, passenger has more chance to survive\n* We can consider a new feature describing these categories.","metadata":{}},{"cell_type":"markdown","source":"[Yukarı](#0)<a id=\"14\"></a>\n## Parch -- Survived","metadata":{}},{"cell_type":"code","source":"g = sns.factorplot(x=\"Parch\", y=\"Survived\", data= train_df, kind=\"bar\",size=6)\ng.set_ylabels(\"Survived Probabilty\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-02T06:37:51.307194Z","iopub.execute_input":"2022-08-02T06:37:51.307674Z","iopub.status.idle":"2022-08-02T06:37:51.732153Z","shell.execute_reply.started":"2022-08-02T06:37:51.307629Z","shell.execute_reply":"2022-08-02T06:37:51.731077Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"* SibSp and Parch can be used for new feature extraction with th = 3\n* Small familes have more chance to survive.\n* There is a std in survival of passenger with parch = 3\n","metadata":{}},{"cell_type":"markdown","source":"[Yukarı](#0)<a id=\"15\"></a>\n## Pclass -- Survived","metadata":{}},{"cell_type":"code","source":"g = sns.factorplot(x = \"Pclass\", y = \"Survived\", data = train_df, kind = \"bar\", size = 6)\ng.set_ylabels(\"Survived Probability\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-02T06:37:51.733703Z","iopub.execute_input":"2022-08-02T06:37:51.734038Z","iopub.status.idle":"2022-08-02T06:37:52.196069Z","shell.execute_reply.started":"2022-08-02T06:37:51.734009Z","shell.execute_reply":"2022-08-02T06:37:52.194654Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"[Yukarı](#0)<a id=\"16\"></a>\n## Age -- Survived\n","metadata":{}},{"cell_type":"code","source":"g = sns.FacetGrid(train_df, col = \"Survived\")\ng.map(sns.distplot, \"Age\", bins= 25)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-02T06:37:52.197554Z","iopub.execute_input":"2022-08-02T06:37:52.198079Z","iopub.status.idle":"2022-08-02T06:37:52.653209Z","shell.execute_reply.started":"2022-08-02T06:37:52.198040Z","shell.execute_reply":"2022-08-02T06:37:52.651996Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"* Age <= 10 has a high survival rate,\n* Oldest passengers (80) survived,\n* Large number of 20 years old did not survive,\n* Most passengers are in 15-35 age range,\n* Use age feature in training\n* Use age distribution for missing value of age","metadata":{}},{"cell_type":"markdown","source":"[Yukarı](#0)<a id=\"17\"></a>\n## Pclass -- Survived -- Age","metadata":{}},{"cell_type":"code","source":"g = sns.FacetGrid(train_df, col=\"Survived\", row=\"Pclass\", size=3)\ng.map(plt.hist, \"Age\", bins=25)\ng.add_legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-02T06:37:52.654560Z","iopub.execute_input":"2022-08-02T06:37:52.654871Z","iopub.status.idle":"2022-08-02T06:37:53.970790Z","shell.execute_reply.started":"2022-08-02T06:37:52.654842Z","shell.execute_reply":"2022-08-02T06:37:53.969737Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"[Yukarı](#0)<a id=\"18\"></a>\n## Embarked -- Sex -- Pclass -- Survived","metadata":{}},{"cell_type":"code","source":"g = sns.FacetGrid(train_df, row=\"Embarked\", size=3)\ng.map(sns.pointplot, \"Pclass\",\"Survived\",\"Sex\")\ng.add_legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-02T06:37:53.972380Z","iopub.execute_input":"2022-08-02T06:37:53.973032Z","iopub.status.idle":"2022-08-02T06:37:54.987050Z","shell.execute_reply.started":"2022-08-02T06:37:53.972996Z","shell.execute_reply":"2022-08-02T06:37:54.985883Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"[Yukarı](#0)<a id=\"19\"></a>\n## Embarked -- Sex -- Fare -- Survived","metadata":{}},{"cell_type":"code","source":"g = sns.FacetGrid(train_df, row=\"Embarked\", col=\"Survived\", size=2.5)\ng.map(sns.barplot,\"Sex\",\"Fare\")\ng.add_legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-02T06:37:54.988603Z","iopub.execute_input":"2022-08-02T06:37:54.989241Z","iopub.status.idle":"2022-08-02T06:37:56.132758Z","shell.execute_reply.started":"2022-08-02T06:37:54.989204Z","shell.execute_reply":"2022-08-02T06:37:56.131672Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"[Yukarı](#0)<a id=\"20\"></a>\n## Fill Missing : Age Feature","metadata":{}},{"cell_type":"code","source":"train_df[train_df[\"Age\"].isnull()]","metadata":{"execution":{"iopub.status.busy":"2022-08-02T06:37:56.134484Z","iopub.execute_input":"2022-08-02T06:37:56.134957Z","iopub.status.idle":"2022-08-02T06:37:56.162011Z","shell.execute_reply.started":"2022-08-02T06:37:56.134886Z","shell.execute_reply":"2022-08-02T06:37:56.160866Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.factorplot(x=\"Sex\", y=\"Age\",data=train_df, kind=\"box\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-02T06:37:56.163426Z","iopub.execute_input":"2022-08-02T06:37:56.163778Z","iopub.status.idle":"2022-08-02T06:37:56.354783Z","shell.execute_reply.started":"2022-08-02T06:37:56.163747Z","shell.execute_reply":"2022-08-02T06:37:56.353458Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Sex is not informative for age prediction, age distribution seems to be the same.","metadata":{}},{"cell_type":"code","source":"sns.factorplot(x=\"Sex\", y=\"Age\",hue=\"Pclass\", data=train_df, kind=\"box\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-02T06:37:56.356382Z","iopub.execute_input":"2022-08-02T06:37:56.356836Z","iopub.status.idle":"2022-08-02T06:37:56.901455Z","shell.execute_reply.started":"2022-08-02T06:37:56.356790Z","shell.execute_reply":"2022-08-02T06:37:56.899984Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"1st class passengers are older than 2nd and 2nd is older than 3rd class.","metadata":{}},{"cell_type":"code","source":"sns.factorplot(x=\"Parch\", y=\"Age\", data=train_df, kind=\"box\")\nsns.factorplot(x=\"SibSp\", y=\"Age\", data=train_df, kind=\"box\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-02T06:37:56.909185Z","iopub.execute_input":"2022-08-02T06:37:56.909564Z","iopub.status.idle":"2022-08-02T06:37:57.528144Z","shell.execute_reply.started":"2022-08-02T06:37:56.909530Z","shell.execute_reply":"2022-08-02T06:37:57.526861Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df[\"Sex\"] = [1 if i == \"male\" else 0 for i in train_df[\"Sex\"]]","metadata":{"execution":{"iopub.status.busy":"2022-08-02T06:37:57.529523Z","iopub.execute_input":"2022-08-02T06:37:57.529866Z","iopub.status.idle":"2022-08-02T06:37:57.536928Z","shell.execute_reply.started":"2022-08-02T06:37:57.529836Z","shell.execute_reply":"2022-08-02T06:37:57.535872Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.heatmap(train_df[[\"Age\",\"Sex\",\"SibSp\",\"Parch\",\"Pclass\"]].corr(),annot=True)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-02T06:37:57.539309Z","iopub.execute_input":"2022-08-02T06:37:57.540285Z","iopub.status.idle":"2022-08-02T06:37:57.816552Z","shell.execute_reply.started":"2022-08-02T06:37:57.540251Z","shell.execute_reply":"2022-08-02T06:37:57.815397Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"index_nan_age = list(train_df[\"Age\"][train_df[\"Age\"].isnull()].index)\nfor i in index_nan_age:\n    age_pred = train_df[\"Age\"][((train_df[\"SibSp\"] == train_df.iloc[i][\"SibSp\"]) & (train_df[\"Parch\"] == train_df.iloc[i][\"Parch\"]) & (train_df[\"Pclass\"] == train_df.iloc[i][\"Pclass\"]))].median()\n    age_med = train_df[\"Age\"].median()\n    if not np.isnan(age_pred):\n        train_df[\"Age\"].iloc[i] = age_pred\n    else:\n        train_df[\"Age\"].iloc[i] = age_med","metadata":{"execution":{"iopub.status.busy":"2022-08-02T06:37:57.817706Z","iopub.execute_input":"2022-08-02T06:37:57.818034Z","iopub.status.idle":"2022-08-02T06:37:58.361062Z","shell.execute_reply.started":"2022-08-02T06:37:57.818006Z","shell.execute_reply":"2022-08-02T06:37:58.360036Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df[train_df[\"Age\"].isnull()]","metadata":{"execution":{"iopub.status.busy":"2022-08-02T06:37:58.362459Z","iopub.execute_input":"2022-08-02T06:37:58.362809Z","iopub.status.idle":"2022-08-02T06:37:58.374975Z","shell.execute_reply.started":"2022-08-02T06:37:58.362777Z","shell.execute_reply":"2022-08-02T06:37:58.373650Z"},"trusted":true},"execution_count":null,"outputs":[]}]}