{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "3621a5a0-c84e-7858-8df2-fc84e27ba635"
      },
      "source": [
        "**Experimenting with Titanic dataset**\n",
        "\n",
        "Based off of \"A Journey through Titanic\", by Omar El Gabry"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "aaf04d8b-4dde-26ab-55dd-d25edaaa3d98"
      },
      "outputs": [],
      "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 in \n",
        "\n",
        "import numpy as np # linear algebra\n",
        "import scipy.stats as stats\n",
        "import pylab as pl\n",
        "import matplotlib.pyplot as plt\n",
        "import seaborn as sns\n",
        "sns.set_style('whitegrid')\n",
        "%matplotlib inline\n",
        "\n",
        "import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n",
        "from pandas import Series, DataFrame\n",
        "# Input data files are available in the \"../input/\" directory.\n",
        "# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory\n",
        "\n",
        "# machine learning\n",
        "from sklearn.linear_model import LogisticRegression\n",
        "from sklearn.svm import SVC, LinearSVC\n",
        "from sklearn.ensemble import RandomForestClassifier\n",
        "from sklearn.neighbors import KNeighborsClassifier\n",
        "from sklearn.naive_bayes import GaussianNB\n",
        "\n",
        "from subprocess import check_output\n",
        "print(check_output([\"ls\", \"../input\"]).decode(\"utf8\"))\n",
        "\n",
        "# Any results you write to the current directory are saved as output."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "99071f47-d9da-4049-0c38-9900d7441dab"
      },
      "outputs": [],
      "source": [
        "titanic_df = pd.read_csv(\"../input/train.csv\")\n",
        "test_df = pd.read_csv(\"../input/test.csv\")\n",
        "\n",
        "titanic_df.head()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "ae64d31f-5406-c030-d734-44714b868333"
      },
      "outputs": [],
      "source": [
        "titanic_df.info()\n",
        "print(\"------------------------------\")\n",
        "test_df.info()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "5a44f357-ab52-adf2-428a-16eafde6c8bd"
      },
      "outputs": [],
      "source": [
        "titanic_df.drop(['PassengerId', 'Ticket', 'Cabin', 'Embarked', 'Name'], axis = 1, inplace = True)\n",
        "test_df.drop(['Ticket', 'Cabin', 'Embarked', 'Name'], axis = 1, inplace=True)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "7a4de15f-b7a3-aa2e-213f-71dc132c74bb"
      },
      "outputs": [],
      "source": [
        "#sns.factorplot('Embarked', 'Survived', data=titanic_df, size=4, aspect=3)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "35622286-e66e-eec8-5b1a-4820f2aa134b"
      },
      "outputs": [],
      "source": [
        ""
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "488f6e47-a08f-e7e0-deb9-3de429978ecc"
      },
      "outputs": [],
      "source": [
        "# Fare\n",
        "\n",
        "# Fill in missing data for \"Fare\" in test_df\n",
        "test_df[\"Fare\"].fillna(test_df[\"Fare\"].median(), inplace=True)\n",
        "\n",
        "# get fare for survived & didn't survive passengers\n",
        "fare_not_survived = titanic_df[\"Fare\"][titanic_df[\"Survived\"]==0]\n",
        "fare_survived = titanic_df[\"Fare\"][titanic_df[\"Survived\"]==1]\n",
        "\n",
        "average_fare = DataFrame([fare_not_survived.mean(), fare_survived.mean()])\n",
        "std_fare = DataFrame([fare_not_survived.std(), fare_survived.std()])\n",
        "\n",
        "# plot\n",
        "titanic_df['Fare'].plot(kind='hist', figsize=(15, 3), bins=100, xlim=(0, 50))\n",
        "\n",
        "average_fare.index.names = std_fare.index.names = [\"Survived\"]\n",
        "average_fare.plot(yerr=std_fare, kind='bar', legend=False)\n",
        "\n",
        "#x = np.linspace(-3, 3, 100)\n",
        "#plt.plot(x, mlab.normpdf(x, average_fare, sigma_fare))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "58dc3148-2790-61e8-c487-c220af3bf905"
      },
      "outputs": [],
      "source": [
        "# Age\n",
        "\n",
        "fig, (axis1,axis2) = plt.subplots(1,2,figsize=(15,4))\n",
        "axis1.set_title('Original Age values - Titanic')\n",
        "axis2.set_title('New Age values - Titanic')\n",
        "\n",
        "# axis3.set_title('Original Age values - Test')\n",
        "# axis4.set_title('New Age values - Test')\n",
        "\n",
        "# get average, std, and number of NaN values in titanic_df\n",
        "average_age_titanic   = titanic_df[\"Age\"].mean()\n",
        "std_age_titanic       = titanic_df[\"Age\"].std()\n",
        "count_nan_age_titanic = titanic_df[\"Age\"].isnull().sum()\n",
        "\n",
        "# get average, std, and number of NaN values in test_df\n",
        "average_age_test   = test_df[\"Age\"].mean()\n",
        "std_age_test       = test_df[\"Age\"].std()\n",
        "count_nan_age_test = test_df[\"Age\"].isnull().sum()\n",
        "\n",
        "# generate random numbers between (mean - std) & (mean + std)\n",
        "rand_1 = np.random.randint(average_age_titanic - std_age_titanic, average_age_titanic + std_age_titanic, size = count_nan_age_titanic)\n",
        "rand_2 = np.random.randint(average_age_test - std_age_test, average_age_test + std_age_test, size = count_nan_age_test)\n",
        "\n",
        "# plot original Age values\n",
        "# NOTE: drop all null values, and convert to int\n",
        "titanic_df['Age'].dropna().astype(int).hist(bins=70, ax=axis1)\n",
        "# test_df['Age'].dropna().astype(int).hist(bins=70, ax=axis1)\n",
        "\n",
        "# fill NaN values in Age column with random values generated\n",
        "titanic_df[\"Age\"][np.isnan(titanic_df[\"Age\"])] = rand_1\n",
        "test_df[\"Age\"][np.isnan(test_df[\"Age\"])] = rand_2\n",
        "\n",
        "# convert from float to int\n",
        "titanic_df['Age'] = titanic_df['Age'].astype(int)\n",
        "test_df['Age']    = test_df['Age'].astype(int)\n",
        "        \n",
        "# plot new Age Values\n",
        "titanic_df['Age'].hist(bins=70, ax=axis2)\n",
        "# test_df['Age'].hist(bins=70, ax=axis4)\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "3022809b-957b-f405-49c2-da60e48b075b"
      },
      "outputs": [],
      "source": [
        "# .... continue with plot Age column\n",
        "\n",
        "# peaks for survived/not survived passengers by their age\n",
        "facet = sns.FacetGrid(titanic_df, hue=\"Survived\",aspect=4)\n",
        "facet.map(sns.kdeplot,'Age',shade= True)\n",
        "facet.set(xlim=(0, titanic_df['Age'].max()))\n",
        "facet.add_legend()\n",
        "\n",
        "# average survived passengers by age\n",
        "fig, axis1 = plt.subplots(1,1,figsize=(18,4))\n",
        "average_age = titanic_df[[\"Age\", \"Survived\"]].groupby(['Age'],as_index=False).mean()\n",
        "sns.barplot(x='Age', y='Survived', data=average_age)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "b258da4e-d6b4-f88c-491c-688b3f6db34c"
      },
      "outputs": [],
      "source": [
        "# Sex\n",
        "\n",
        "# As we see, children(age < ~16) on aboard seem to have a high chances for Survival.\n",
        "# So, we can classify passengers as males, females, and child\n",
        "def get_person(passenger):\n",
        "    age,sex = passenger\n",
        "    return 'child' if age < 16 else sex\n",
        "    \n",
        "titanic_df['Person'] = titanic_df[['Age','Sex']].apply(get_person,axis=1)\n",
        "test_df['Person']    = test_df[['Age','Sex']].apply(get_person,axis=1)\n",
        "\n",
        "# No need to use Sex column since we created Person column\n",
        "titanic_df.drop(['Sex'],axis=1,inplace=True)\n",
        "test_df.drop(['Sex'],axis=1,inplace=True)\n",
        "\n",
        "# create dummy variables for Person column, & drop Male as it has the lowest average of survived passengers\n",
        "person_dummies_titanic  = pd.get_dummies(titanic_df['Person'])\n",
        "person_dummies_titanic.columns = ['Child','Female','Male']\n",
        "person_dummies_titanic.drop(['Male'], axis=1, inplace=True)\n",
        "\n",
        "person_dummies_test  = pd.get_dummies(test_df['Person'])\n",
        "person_dummies_test.columns = ['Child','Female','Male']\n",
        "person_dummies_test.drop(['Male'], axis=1, inplace=True)\n",
        "\n",
        "titanic_df = titanic_df.join(person_dummies_titanic)\n",
        "test_df    = test_df.join(person_dummies_test)\n",
        "\n",
        "fig, (axis1,axis2) = plt.subplots(1,2,figsize=(10,5))\n",
        "\n",
        "# sns.factorplot('Person',data=titanic_df,kind='count',ax=axis1)\n",
        "sns.countplot(x='Person', data=titanic_df, ax=axis1)\n",
        "\n",
        "# average of survived for each Person(male, female, or child)\n",
        "person_perc = titanic_df[[\"Person\", \"Survived\"]].groupby(['Person'],as_index=False).mean()\n",
        "sns.barplot(x='Person', y='Survived', data=person_perc, ax=axis2, order=['male','female','child'])\n",
        "\n",
        "titanic_df.drop(['Person'],axis=1,inplace=True)\n",
        "test_df.drop(['Person'],axis=1,inplace=True)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "ab48d505-2469-7bdc-6e5a-15eb71b290ae"
      },
      "outputs": [],
      "source": [
        "# Pclass\n",
        "\n",
        "# sns.factorplot('Pclass',data=titanic_df,kind='count',order=[1,2,3])\n",
        "sns.factorplot('Pclass','Survived',order=[1,2,3], data=titanic_df,size=5)\n",
        "\n",
        "# create dummy variables for Pclass column, & drop 3rd class as it has the lowest average of survived passengers\n",
        "pclass_dummies_titanic  = pd.get_dummies(titanic_df['Pclass'])\n",
        "pclass_dummies_titanic.columns = ['Class_1','Class_2','Class_3']\n",
        "pclass_dummies_titanic.drop(['Class_3'], axis=1, inplace=True)\n",
        "\n",
        "pclass_dummies_test  = pd.get_dummies(test_df['Pclass'])\n",
        "pclass_dummies_test.columns = ['Class_1','Class_2','Class_3']\n",
        "pclass_dummies_test.drop(['Class_3'], axis=1, inplace=True)\n",
        "\n",
        "titanic_df.drop(['Pclass'],axis=1,inplace=True)\n",
        "test_df.drop(['Pclass'],axis=1,inplace=True)\n",
        "\n",
        "titanic_df = titanic_df.join(pclass_dummies_titanic)\n",
        "test_df    = test_df.join(pclass_dummies_test)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "1820b3d4-5323-a973-103f-86b64afcfa6a"
      },
      "outputs": [],
      "source": [
        " # define training and testing sets\n",
        "\n",
        "X_train = titanic_df.drop(\"Survived\",axis=1)\n",
        "Y_train = titanic_df[\"Survived\"]\n",
        "X_test  = test_df.copy()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "c530bfac-055b-53c1-67c8-d23b565e84f3"
      },
      "outputs": [],
      "source": [
        "X_train.info()\n",
        "X_test  = test_df.drop(\"PassengerId\",axis=1).copy()\n",
        "\n",
        "X_train.head()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "1f7038bb-42ee-4c77-213f-34aba43f1c56"
      },
      "outputs": [],
      "source": [
        "# Logistic Regression\n",
        "\n",
        "logreg = LogisticRegression()\n",
        "\n",
        "logreg.fit(X_train, Y_train)\n",
        "\n",
        "Y_pred_log = logreg.predict(X_test)\n",
        "\n",
        "logreg.score(X_train, Y_train)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "8f584cd4-c1a7-0071-d957-13c6102b0fa7"
      },
      "outputs": [],
      "source": [
        "# Support Vector Machines\n",
        "\n",
        "#svc = SVC()\n",
        "\n",
        "#svc.fit(X_train, Y_train)\n",
        "\n",
        "#Y_pred = svc.predict(X_test)\n",
        "\n",
        "#svc.score(X_train, Y_train)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "ed4f82b9-abb1-0b23-4606-efbb9f3a01f5"
      },
      "outputs": [],
      "source": [
        "# Random Forests\n",
        "\n",
        "random_forest = RandomForestClassifier(n_estimators=100)\n",
        "\n",
        "random_forest.fit(X_train, Y_train)\n",
        "\n",
        "Y_pred = random_forest.predict(X_test)\n",
        "\n",
        "random_forest.score(X_train, Y_train)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "b3e8160f-9073-0f1c-92cc-2b0c08284f09"
      },
      "outputs": [],
      "source": [
        "## KNeighbors\n",
        "\n",
        "#knn = KNeighborsClassifier(n_neighbors = 3)\n",
        "\n",
        "#knn.fit(X_train, Y_train)\n",
        "\n",
        "#Y_pred = knn.predict(X_test)\n",
        "\n",
        "#knn.score(X_train, Y_train)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "6eb6d830-2f1c-8ae8-d0a7-b2a13a3d2e2c"
      },
      "outputs": [],
      "source": [
        "# Gaussian Naive Bayes\n",
        "\n",
        "#gaussian = GaussianNB()\n",
        "\n",
        "#gaussian.fit(X_train, Y_train)\n",
        "\n",
        "#Y_pred = gaussian.predict(X_test)\n",
        "\n",
        "#gaussian.score(X_train, Y_train)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "67f9d6b0-9da5-61b0-8495-873f247ab455"
      },
      "outputs": [],
      "source": [
        "# get Correlation Coefficient for each feature using Logistic Regression\n",
        "coeff_df = DataFrame(titanic_df.columns.delete(0))\n",
        "coeff_df.columns = ['Features']\n",
        "coeff_df[\"Coefficient Estimate\"] = pd.Series(logreg.coef_[0])\n",
        "\n",
        "# preview\n",
        "coeff_df"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "976c6fcb-cfc9-36ec-4bec-6322f8dd87b3"
      },
      "outputs": [],
      "source": [
        "submission = pd.DataFrame({\n",
        "        \"PassengerId\": test_df[\"PassengerId\"],\n",
        "        \"Survived\": Y_pred\n",
        "    })\n",
        "submission.to_csv('titanic.csv', index=False)"
      ]
    }
  ],
  "metadata": {
    "_change_revision": 0,
    "_is_fork": false,
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.6.0"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}