{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "feb59d1e-00be-2d1d-360a-685860eb7f4d"
      },
      "source": ""
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "7d66795e-2ac8-822b-4438-035823b7d28a"
      },
      "outputs": [],
      "source": [
        "import numpy as np\n",
        "import pandas as pd"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "0428a260-ce99-0092-2815-492e1a086619"
      },
      "outputs": [],
      "source": [
        "import matplotlib.pyplot as plt\n",
        "import seaborn as sns\n",
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "96168b2a-58c9-cee5-8699-30e1b22cae6e"
      },
      "outputs": [],
      "source": [
        "train = pd.read_csv('../input/train.csv')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "85e1b7ae-f314-ea95-3546-a5120ca7ced0"
      },
      "outputs": [],
      "source": [
        "train.head()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "2938330c-6d4c-0cc8-cb43-6d18a79676d4"
      },
      "outputs": [],
      "source": [
        "train.info()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "a0f3938e-5174-1aa5-d9fd-cd5f5363d563"
      },
      "outputs": [],
      "source": [
        "train.describe()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "3b8d0367-5ec7-3f3e-4276-728f4e092ca5"
      },
      "outputs": [],
      "source": [
        "train.isnull()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "2e7ef1e0-1532-b390-6bb7-b421b727a5b1"
      },
      "source": ""
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "cf92a5be-7ffe-be9b-2e91-f72288859604"
      },
      "outputs": [],
      "source": [
        "sns.heatmap(train.isnull(),yticklabels=False,cbar=False,cmap='viridis')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "6bbce754-d133-ca8b-2cf0-66a80b00587a"
      },
      "source": ""
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "65860948-7524-ed54-4b43-c934bbecde33"
      },
      "outputs": [],
      "source": [
        "sns.set_style('whitegrid')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "9e42c3d7-8320-ee90-9127-30c7f819313b"
      },
      "outputs": [],
      "source": [
        "sns.countplot(x='Survived',data=train)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "095733bd-734b-466e-412a-fc70b15fa538"
      },
      "outputs": [],
      "source": [
        "sns.countplot(x='Survived',data=train,hue='Sex',palette='RdBu_r')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "b60517a0-326f-b451-bd7f-073ff573d1fb"
      },
      "outputs": [],
      "source": [
        "sns.countplot(x='Survived',data=train,hue='Pclass')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "c641029e-bf3e-3e91-89bd-037787678ef2"
      },
      "outputs": [],
      "source": [
        "sns.distplot(train['Age'].dropna(),kde=False,bins=30)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "f47038a7-f7f5-bdf0-2d4f-740724ed8995"
      },
      "outputs": [],
      "source": [
        "# Check out the sibling or spouse column\n",
        "sns.countplot(x='SibSp',data=train)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "d3989f56-5377-2103-b41f-05b1a668ecf5"
      },
      "outputs": [],
      "source": [
        "sns.set(rc={\"figure.figsize\": (10, 6)})\n",
        "sns.distplot(train['Fare'].dropna(),kde=False,bins=50)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "e93f21de-2f00-caf6-53f9-52ea1488fe86"
      },
      "outputs": [],
      "source": [
        "#import cufflinks as cf"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "a094c3c4-a356-1155-f3d9-16ecf069e574"
      },
      "outputs": [],
      "source": [
        "#cf.go_offline()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "0665acad-d036-9407-5302-e64ddc904a7e"
      },
      "outputs": [],
      "source": [
        "#train['Fare'].iplot(kind='hist',bins=100)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "eeb170e6-a68b-efd5-8708-39d70bc1e2f3"
      },
      "source": ""
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "28f202d1-0276-a6fb-1acf-8d35a01f03cb"
      },
      "outputs": [],
      "source": [
        "plt.figure(figsize=(10,7))\n",
        "sns.boxplot(x='Pclass',y='Age',data=train)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "52aacac8-314a-0795-b64d-19a7ee040699"
      },
      "outputs": [],
      "source": [
        "def impute_age(cols):\n",
        "    Age = cols[0]\n",
        "    Pclass = cols[1]\n",
        "    \n",
        "    if pd.isnull(Age):\n",
        "        if (Pclass == 1):\n",
        "            return 37\n",
        "        elif (Pclass == 2):\n",
        "            return 29\n",
        "        else:\n",
        "            return 24\n",
        "    else:\n",
        "        return Age"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "06329701-3c0e-2f04-0762-5a3fb6a99f15"
      },
      "outputs": [],
      "source": [
        "train['Age'] = train[['Age','Pclass']].apply(impute_age, axis=1)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "4c6aaec6-67b4-b6a6-bbdb-98c647fc2583"
      },
      "outputs": [],
      "source": [
        "train.info()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "5bec0b58-91ce-58c8-4614-c475f3dd6bce"
      },
      "outputs": [],
      "source": [
        "# We drop the cabin column, since it has too much missing information\n",
        "train.drop('Cabin', inplace=True, axis=1)\n",
        "train.drop('Ticket', inplace=True, axis=1)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "4c3394ab-cac6-b1d0-a13b-34e042f75885"
      },
      "outputs": [],
      "source": [
        "train.info()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "0cbb86bd-c703-1c22-921f-e9921f02a202"
      },
      "outputs": [],
      "source": [
        "train.dropna(inplace=True)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "f917fe77-9675-3fe1-517b-0a3e29c486d5"
      },
      "outputs": [],
      "source": [
        "# Categorical features have to be converted to numerical dummy variables, in order for the machine learning\n",
        "# algorithm to accept that data\n",
        "train.head()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "3f23f834-52b0-52cb-cfd0-9cb40f3db806"
      },
      "outputs": [],
      "source": [
        "train['Sex'] = pd.get_dummies(train['Sex'],drop_first=True)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "dc9f41c4-bf04-a6dd-8502-11cc03807670"
      },
      "outputs": [],
      "source": [
        "train.head()\n",
        "# The 'Sex' column shows 1 for Male and 0 for Female"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "d6ecc7e5-a008-9902-10f6-dfa9932145ad"
      },
      "outputs": [],
      "source": [
        "train.drop('Embarked', inplace=True, axis=1)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "95d653e7-d2a7-69c1-a624-9969e7c5b60b"
      },
      "outputs": [],
      "source": [
        "train.head()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "e9f354ba-64f9-a7fc-7d2c-fae5ab8a0275"
      },
      "outputs": [],
      "source": [
        "# We also currently dont use the name\n",
        "train.drop('Name', inplace=True, axis=1)\n",
        "train.head()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "3d2f7eee-f19f-3801-a4ed-3b10f1c2d5ae"
      },
      "outputs": [],
      "source": [
        "# We also dont need the passengerID since it plays no role in predicting survivability\n",
        "train.drop('PassengerId', inplace=True, axis=1)\n",
        "train.head()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "8caa25d2-8499-e836-8f7d-3482efbeef4d"
      },
      "outputs": [],
      "source": [
        "# The Pclass column is also categorical data, and should be replaced with the dummy variables\n",
        "pclass = pd.get_dummies(train['Pclass'], drop_first=True)\n",
        "pclass.columns=['Class=2','Class=3']\n",
        "pclass"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "efc0c8db-3764-91d9-b45c-b85369894ff7"
      },
      "outputs": [],
      "source": [
        "train.head()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "2bc517ff-1bdb-05e6-b28f-cb6545eeefc4"
      },
      "outputs": [],
      "source": [
        "train=pd.concat([train,pclass],axis=1)\n",
        "train.head()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "3e90139f-2229-7d6f-7c25-91af5ab53f2c"
      },
      "outputs": [],
      "source": [
        "train.drop('Pclass',inplace=True,axis=1)\n",
        "train.head()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "0ac45f06-17c5-7403-b1ba-7634c3c9eddd"
      },
      "outputs": [],
      "source": [
        "train.columns"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "42595c29-97b6-d4a0-5211-87da6c4ef45b"
      },
      "outputs": [],
      "source": [
        "X = train[['Sex', 'Age', 'SibSp', 'Parch', 'Fare', 'Class=2','Class=3']]\n",
        "y = train['Survived']"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "4cf9ed66-773b-83e8-9e4b-761382b8c723"
      },
      "outputs": [],
      "source": [
        "from sklearn.model_selection import train_test_split"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "65e026de-7fb7-b816-305f-76d4719a593c"
      },
      "outputs": [],
      "source": [
        "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=101)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "e3994461-7aa9-a07b-5aa6-f50bb61561ed"
      },
      "outputs": [],
      "source": [
        "# Create and train model\n",
        "from sklearn.linear_model import LogisticRegression"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "fdeacafd-eabd-d3cf-8e75-7b8b0248d581"
      },
      "outputs": [],
      "source": [
        "logmod = LogisticRegression()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "6d871cf6-ae11-bb2c-99a3-d8841033266e"
      },
      "outputs": [],
      "source": [
        "logmod.fit(X_train,y_train)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "56f4aadd-a793-77c3-6b7d-d9f69eec3ed6"
      },
      "outputs": [],
      "source": [
        "predictions = logmod.predict(X_test)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "19ae60f7-ed69-b2fe-c81e-62a86b48dd7c"
      },
      "outputs": [],
      "source": [
        "from sklearn.metrics import classification_report"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "9184ea9a-6839-b5c8-313a-9aa7bbedb3b2"
      },
      "outputs": [],
      "source": [
        "print(classification_report(y_test, predictions))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "7dff1daf-68bb-dd4a-d905-c081f619a6c1"
      },
      "outputs": [],
      "source": [
        "from sklearn.metrics import confusion_matrix"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "6a2ecf4c-bfa5-585f-cbac-7f9e333b0ac8"
      },
      "outputs": [],
      "source": [
        "sns.heatmap(confusion_matrix(y_test,predictions),annot=True)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "1d94a7a3-7ade-730b-04ac-aa3c3d7af703"
      },
      "outputs": [],
      "source": ""
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "0b95492d-47ad-c0d6-66ba-07ce087b49f3"
      },
      "outputs": [],
      "source": ""
    }
  ],
  "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
}