{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "74b393a4-235c-dea6-7704-f5afefa33b86"
      },
      "source": [
        "Yeah idk"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "a2d898b5-eb8a-5446-8c7d-d73077c35685",
        "collapsed": true
      },
      "outputs": [],
      "source": [
        "# data analysis and wrangling\n",
        "import pandas as pd\n",
        "import numpy as np\n",
        "import random as rnd\n",
        "\n",
        "# visualization\n",
        "import seaborn as sns\n",
        "import matplotlib.pyplot as plt\n",
        "%matplotlib inline\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",
        "from sklearn.linear_model import Perceptron\n",
        "from sklearn.linear_model import SGDClassifier\n",
        "from sklearn.tree import DecisionTreeClassifier"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "d215d049-c7a8-367c-1474-23b2c80998ba"
      },
      "outputs": [],
      "source": [
        "train_df = pd.read_csv('../input/train.csv')\n",
        "test_df = pd.read_csv('../input/test.csv')\n",
        "combine = [train_df, test_df]"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "42802b33-85d5-f532-506a-9bbcfe01da14"
      },
      "outputs": [],
      "source": [
        "print(train_df.columns.values)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "93bc8668-37f7-ea76-469a-cbc2a8ea2100"
      },
      "outputs": [],
      "source": [
        "#what are the categories like? which are numerical, which are categorical\n",
        "#Which features are categorical?\n",
        "#These values classify the samples into sets of similar samples. Within categorical features are the values nominal, ordinal, ratio, or interval based? Among other things this helps us select the appropriate plots for visualization.\n",
        "#Categorical: Survived, Sex, and Embarked. Ordinal: Pclass.\n",
        "#Which features are numerical?\n",
        "#Which features are numerical? These values change from sample to sample. Within numerical features are the values discrete, continuous, or timeseries based? Among other things this helps us select the appropriate plots for visualization.\n",
        "#Continous: Age, Fare. Discrete: SibSp, Parch.\n",
        "train_df.head()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "8d225e35-245b-9b98-f7ca-fedaefa11c34"
      },
      "outputs": [],
      "source": [
        "train_df.tail()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "1298afd0-3d7d-1a62-2611-522c1e10786c"
      },
      "outputs": [],
      "source": [
        "train_df.info()\n",
        "print('_'*40)\n",
        "test_df.info()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "cad4b1bd-097d-2ea2-a5e1-1c0057098998"
      },
      "outputs": [],
      "source": [
        "train_df.describe()\n",
        "# Review survived rate using `percentiles=[.61, .62]` knowing our problem description mentions 38% survival rate.\n",
        "# Review Parch distribution using `percentiles=[.75, .8]`\n",
        "# SibSp distribution `[.68, .69]`\n",
        "# Age and Fare `[.1, .2, .3, .4, .5, .6, .7, .8, .9, .99]`\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "cf6ccd50-6ba7-76a6-5774-adf76f2adcb8"
      },
      "outputs": [],
      "source": [
        "train_df.describe(include=['O'])"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "e40f1f53-95c0-f114-f2c4-84025c9274a6"
      },
      "outputs": [],
      "source": [
        "train_df[['Pclass', 'Survived']].groupby(['Pclass'], as_index=False).mean().sort_values(by='Survived', ascending=False)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "9cd1932b-22c3-92f7-218f-f8bc138919ad"
      },
      "outputs": [],
      "source": [
        "train_df[[\"Sex\", \"Survived\"]].groupby(['Sex'], as_index=False).mean().sort_values(by='Survived', ascending=False)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "5a553625-4648-e0d5-af18-696fba44e0e4"
      },
      "outputs": [],
      "source": [
        "train_df[[\"SibSp\", \"Survived\"]].groupby(['SibSp'], as_index=False).mean().sort_values(by='Survived', ascending=False)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "594592ef-71fd-6f85-1a8a-13a54ec09946"
      },
      "outputs": [],
      "source": [
        "train_df[[\"Parch\", \"Survived\"]].groupby(['Parch'], as_index=False).mean().sort_values(by='Survived', ascending=False)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "513e9242-6a11-bba2-6488-c75925f1eb5c"
      },
      "outputs": [],
      "source": [
        "g = sns.FacetGrid(train_df, col='Survived')\n",
        "g.map(plt.hist, 'Age', bins=20)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "ec63c67c-2439-c650-7511-95e157b61d40"
      },
      "outputs": [],
      "source": [
        "# grid = sns.FacetGrid(train_df, col='Pclass', hue='Survived')\n",
        "grid = sns.FacetGrid(train_df, col='Survived', row='Pclass', size=2.2, aspect=1.6)\n",
        "grid.map(plt.hist, 'Age', alpha=.5, bins=20)\n",
        "grid.add_legend();"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "f972b734-4a9c-a6f5-acac-7fda4e10396d"
      },
      "outputs": [],
      "source": [
        "# grid = sns.FacetGrid(train_df, col='Embarked')\n",
        "grid = sns.FacetGrid(train_df, row='Embarked', size=2.2, aspect=1.6)\n",
        "grid.map(sns.pointplot, 'Pclass', 'Survived', 'Sex', palette='deep')\n",
        "grid.add_legend()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "a5a88c61-6609-f3c6-57eb-906c1151433d"
      },
      "outputs": [],
      "source": [
        "# grid = sns.FacetGrid(train_df, col='Embarked', hue='Survived', palette={0: 'k', 1: 'w'})\n",
        "grid = sns.FacetGrid(train_df, row='Embarked', col='Survived', size=2.2, aspect=1.6)\n",
        "grid.map(sns.barplot, 'Sex', 'Fare', alpha=.5, ci=None)\n",
        "grid.add_legend()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "008defd2-5dd7-dbf0-0be0-eeff4c54960d"
      },
      "outputs": [],
      "source": [
        "print(\"Before\", train_df.shape, test_df.shape, combine[0].shape, combine[1].shape)\n",
        "\n",
        "train_df = train_df.drop(['Ticket', 'Cabin'], axis=1)\n",
        "test_df = test_df.drop(['Ticket', 'Cabin'], axis=1)\n",
        "combine = [train_df, test_df]\n",
        "\n",
        "\"After\", train_df.shape, test_df.shape, combine[0].shape, combine[1].shape"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "723025e0-bfde-3395-8007-bc99890021f2"
      },
      "outputs": [],
      "source": [
        "for dataset in combine:\n",
        "    dataset['Title'] = dataset.Name.str.extract(' ([A-Za-z]+)\\.', expand=False)\n",
        "\n",
        "pd.crosstab(train_df['Title'], train_df['Sex'])"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "b3561e31-d0a7-3fb1-242e-2e7a205e197b"
      },
      "outputs": [],
      "source": [
        "for dataset in combine:\n",
        "    dataset['Title'] = dataset['Title'].replace(['Lady', 'Countess','Capt', 'Col',\\\n",
        " \t'Don', 'Dr', 'Major', 'Rev', 'Sir', 'Jonkheer', 'Dona'], 'Rare')\n",
        "\n",
        "    dataset['Title'] = dataset['Title'].replace('Mlle', 'Miss')\n",
        "    dataset['Title'] = dataset['Title'].replace('Ms', 'Miss')\n",
        "    dataset['Title'] = dataset['Title'].replace('Mme', 'Mrs')\n",
        "    \n",
        "train_df[['Title', 'Survived']].groupby(['Title'], as_index=False).mean()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "8a53e225-a864-4cd8-5cae-aeb749b7340d"
      },
      "outputs": [],
      "source": [
        "title_mapping = {\"Mr\": 1, \"Miss\": 2, \"Mrs\": 3, \"Master\": 4, \"Rare\": 5}\n",
        "for dataset in combine:\n",
        "    dataset['Title'] = dataset['Title'].map(title_mapping)\n",
        "    dataset['Title'] = dataset['Title'].fillna(0)\n",
        "\n",
        "train_df.head()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "5fe24118-b152-182c-99a6-8fd7a69d7c8a"
      },
      "outputs": [],
      "source": [
        "train_df = train_df.drop(['Name', 'PassengerId'], axis=1)\n",
        "test_df = test_df.drop(['Name'], axis=1)\n",
        "combine = [train_df, test_df]\n",
        "train_df.shape, test_df.shape"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "141d6346-a358-d76b-8c1a-d8ca232380dd"
      },
      "outputs": [],
      "source": [
        "for dataset in combine:\n",
        "    dataset['Sex'] = dataset['Sex'].map( {'female': 1, 'male': 0} ).astype(int)\n",
        "\n",
        "train_df.head()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "b0564b06-206f-bef6-9e93-3d63a0a4eb0c"
      },
      "outputs": [],
      "source": [
        "#for dataset in combine: \n",
        "#    dataset['Sex'] = dataset['Sex'].map({'female': 1, 'male': 0}).astype(int)\n",
        "#train_df.head()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "75fe77ec-2f26-b770-e673-d25ef72535bf"
      },
      "outputs": [],
      "source": [
        "grid = sns.FacetGrid(train_df, row='Pclass', col='Sex', size=2.2, aspect=1.6)\n",
        "grid.map(plt.hist, 'Age', alpha=.5, bins=20)\n",
        "grid.add_legend()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "5d4e7768-bd67-5655-e3c4-9d4bbdfd6d8e"
      },
      "outputs": [],
      "source": [
        "guess_ages = np.zeros((2,3))\n",
        "guess_ages"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "a5f67dbb-09ee-1d97-7711-1b52a3cdb42c"
      },
      "outputs": [],
      "source": [
        "for dataset in combine:\n",
        "    for i in range(0, 2):\n",
        "        for j in range(0, 3):\n",
        "            guess_df = dataset[(dataset['Sex'] == i) & \\\n",
        "                                  (dataset['Pclass'] == j+1)]['Age'].dropna()\n",
        "\n",
        "            # age_mean = guess_df.mean()\n",
        "            # age_std = guess_df.std()\n",
        "            # age_guess = rnd.uniform(age_mean - age_std, age_mean + age_std)\n",
        "\n",
        "            age_guess = guess_df.median()\n",
        "\n",
        "            # Convert random age float to nearest .5 age\n",
        "            guess_ages[i,j] = int( age_guess/0.5 + 0.5 ) * 0.5\n",
        "            \n",
        "    for i in range(0, 2):\n",
        "        for j in range(0, 3):\n",
        "            dataset.loc[ (dataset.Age.isnull()) & (dataset.Sex == i) & (dataset.Pclass == j+1),\\\n",
        "                    'Age'] = guess_ages[i,j]\n",
        "\n",
        "    dataset['Age'] = dataset['Age'].astype(int)\n",
        "\n",
        "train_df.head()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "fb1bda08-bfe6-9be7-7fae-468e555e825a"
      },
      "source": [
        "train_df['AgeBand'] = pd.cut(train_df['Age'], 5)\n",
        "train_df[['AgeBand', 'Survived']].groupby(['AgeBand'], as_index=False).mean().sort_values(by='AgeBand', ascending=True)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "ab4a819a-a7f0-33e0-719d-3053b4f7e387"
      },
      "source": [
        "for dataset in combine:    \n",
        "    dataset.loc[ dataset['Age'] <= 16, 'Age'] = 0\n",
        "    dataset.loc[(dataset['Age'] > 16) & (dataset['Age'] <= 32), 'Age'] = 1\n",
        "    dataset.loc[(dataset['Age'] > 32) & (dataset['Age'] <= 48), 'Age'] = 2\n",
        "    dataset.loc[(dataset['Age'] > 48) & (dataset['Age'] <= 64), 'Age'] = 3\n",
        "    dataset.loc[ dataset['Age'] > 64, 'Age']\n",
        "train_df.head()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "26ab8552-e3fa-1ec8-43ef-732986d8cc8a"
      },
      "outputs": [],
      "source": [
        "#train_df = train_df.drop(['AgeBand'], axis=1)\n",
        "combine = [train_df, test_df]\n",
        "train_df.head()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "ae409e28-89ff-b52c-ef1a-26bc4a76ffaf"
      },
      "outputs": [],
      "source": [
        "for dataset in combine:\n",
        "    dataset['FamilySize'] = dataset['SibSp'] + dataset['Parch'] + 1\n",
        "\n",
        "train_df[['FamilySize', 'Survived']].groupby(['FamilySize'], as_index=False).mean().sort_values(by='Survived', ascending=False)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "beaa6cdd-cb19-2d70-da84-e134720c3c1b"
      },
      "outputs": [],
      "source": [
        "for dataset in combine:\n",
        "    dataset['IsAlone'] = 0\n",
        "    dataset.loc[dataset['FamilySize'] == 1, 'IsAlone'] = 1\n",
        "\n",
        "train_df[['IsAlone', 'Survived']].groupby(['IsAlone'], as_index=False).mean()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "b981f49e-3a51-ffa5-fea2-fc1569a81037"
      },
      "outputs": [],
      "source": [
        "train_df = train_df.drop(['Parch', 'SibSp', 'FamilySize'], axis=1)\n",
        "test_df = test_df.drop(['Parch', 'SibSp', 'FamilySize'], axis=1)\n",
        "combine = [train_df, test_df]\n",
        "\n",
        "train_df.head()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "47c8c04f-4b74-8543-cd3e-edd96c605bb7"
      },
      "outputs": [],
      "source": [
        "for dataset in combine:\n",
        "    dataset['Age*Class'] = dataset.Age * dataset.Pclass\n",
        "\n",
        "train_df.loc[:, ['Age*Class', 'Age', 'Pclass']].head(10)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "5456d3ff-cdd1-49e2-9be1-ea21e8faf385"
      },
      "outputs": [],
      "source": [
        "freq_port = train_df.Embarked.dropna().mode()[0]\n",
        "freq_port"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "ab6f5807-d19c-09a8-4e37-8554693f1b3c"
      },
      "outputs": [],
      "source": [
        "for dataset in combine:\n",
        "    dataset['Embarked'] = dataset['Embarked'].fillna(freq_port)\n",
        "    \n",
        "train_df[['Embarked', 'Survived']].groupby(['Embarked'], as_index=False).mean().sort_values(by='Survived', ascending=False)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "88c3db37-9110-a109-9c28-9e3ee8d995d1"
      },
      "outputs": [],
      "source": [
        "for dataset in combine:\n",
        "    dataset['Embarked'] = dataset['Embarked'].map( {'S': 0, 'C': 1, 'Q': 2} ).astype(int)\n",
        "\n",
        "train_df.head()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "481e4074-cd72-55b1-7a6b-a40925e1578a"
      },
      "outputs": [],
      "source": [
        "test_df['Fare'].fillna(test_df['Fare'].dropna().median(), inplace=True)\n",
        "test_df.head()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "a814983c-4195-80ab-97ad-2497455a57f2"
      },
      "outputs": [],
      "source": [
        "train_df['FareBand'] = pd.qcut(train_df['Fare'], 4)\n",
        "train_df[['FareBand', 'Survived']].groupby(['FareBand'], as_index=False).mean().sort_values(by='FareBand', ascending=True)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "ce7d7c52-0ebe-0db1-7876-f35402a13b8d"
      },
      "outputs": [],
      "source": [
        "for dataset in combine:\n",
        "    dataset.loc[ dataset['Fare'] <= 7.91, 'Fare'] = 0\n",
        "    dataset.loc[(dataset['Fare'] > 7.91) & (dataset['Fare'] <= 14.454), 'Fare'] = 1\n",
        "    dataset.loc[(dataset['Fare'] > 14.454) & (dataset['Fare'] <= 31), 'Fare']   = 2\n",
        "    dataset.loc[ dataset['Fare'] > 31, 'Fare'] = 3\n",
        "    dataset['Fare'] = dataset['Fare'].astype(int)\n",
        "\n",
        "train_df = train_df.drop(['FareBand'], axis=1)\n",
        "combine = [train_df, test_df]\n",
        "    \n",
        "train_df.head(10)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "27838aed-dd2f-0d74-7953-15bc787b6cd0"
      },
      "outputs": [],
      "source": [
        "test_df.head(10)\n",
        "train_df = train_df.drop(\"Age*Class\", axis=1)\n",
        "test_df  = test_df.drop(\"Age*Class\", axis=1).copy()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "c784bc2e-eb7d-98b0-db7a-dbe87975c04b"
      },
      "outputs": [],
      "source": [
        "X_train = train_df.drop(\"Survived\", axis=1)\n",
        "Y_train = train_df[\"Survived\"]\n",
        "X_test  = test_df.drop(\"PassengerId\", axis=1).copy()\n",
        "X_train.shape, Y_train.shape, X_test.shape"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "2b9b40dc-873f-baf8-d6f6-ac819f954f76"
      },
      "outputs": [],
      "source": [
        "# Logistic Regression\n",
        "\n",
        "logreg = LogisticRegression()\n",
        "logreg.fit(X_train, Y_train)\n",
        "Y_pred = logreg.predict(X_test)\n",
        "acc_log = round(logreg.score(X_train, Y_train) * 100, 2)\n",
        "acc_log"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "95b0db8c-c3f8-2ed6-9bfc-375926deb6ef"
      },
      "outputs": [],
      "source": [
        "coeff_df = pd.DataFrame(train_df.columns.delete(0))\n",
        "coeff_df.columns = ['Feature']\n",
        "coeff_df[\"Correlation\"] = pd.Series(logreg.coef_[0])\n",
        "\n",
        "coeff_df.sort_values(by='Correlation', ascending=False)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "855c5fbc-a5c0-e6f0-2d84-b98c14b2b6e8"
      },
      "outputs": [],
      "source": [
        "# Support Vector Machines\n",
        "\n",
        "svc = SVC()\n",
        "svc.fit(X_train, Y_train)\n",
        "Y_pred = svc.predict(X_test)\n",
        "acc_svc = round(svc.score(X_train, Y_train) * 100, 2)\n",
        "acc_svc"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "b4312347-a13e-1a7d-6a3d-b7c0c0b22c4a"
      },
      "outputs": [],
      "source": [
        "knn = KNeighborsClassifier(n_neighbors = 3)\n",
        "knn.fit(X_train, Y_train)\n",
        "Y_pred = knn.predict(X_test)\n",
        "acc_knn = round(knn.score(X_train, Y_train) * 100, 2)\n",
        "acc_knn"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "81dfeba4-7d3b-ae59-67bb-037687ccffe3"
      },
      "outputs": [],
      "source": [
        "# Gaussian Naive Bayes\n",
        "\n",
        "gaussian = GaussianNB()\n",
        "gaussian.fit(X_train, Y_train)\n",
        "Y_pred = gaussian.predict(X_test)\n",
        "acc_gaussian = round(gaussian.score(X_train, Y_train) * 100, 2)\n",
        "acc_gaussian"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "859792f7-6c97-e505-1cee-79bc9976189b"
      },
      "outputs": [],
      "source": [
        "# Perceptron\n",
        "\n",
        "perceptron = Perceptron()\n",
        "perceptron.fit(X_train, Y_train)\n",
        "Y_pred = perceptron.predict(X_test)\n",
        "acc_perceptron = round(perceptron.score(X_train, Y_train) * 100, 2)\n",
        "acc_perceptron"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "8389f2ce-fe06-54fa-5428-3cde7939156e"
      },
      "outputs": [],
      "source": [
        "# Linear SVC\n",
        "\n",
        "linear_svc = LinearSVC()\n",
        "linear_svc.fit(X_train, Y_train)\n",
        "Y_pred = linear_svc.predict(X_test)\n",
        "acc_linear_svc = round(linear_svc.score(X_train, Y_train) * 100, 2)\n",
        "acc_linear_svc"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "e9cb89f9-311a-4d64-f3e9-e286aee37189"
      },
      "outputs": [],
      "source": [
        "# Stochastic Gradient Descent\n",
        "\n",
        "sgd = SGDClassifier()\n",
        "sgd.fit(X_train, Y_train)\n",
        "Y_pred = sgd.predict(X_test)\n",
        "acc_sgd = round(sgd.score(X_train, Y_train) * 100, 2)\n",
        "acc_sgd"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "0f8760bf-b0f3-ba01-340c-275452f2d03e"
      },
      "outputs": [],
      "source": [
        "# Decision Tree\n",
        "\n",
        "decision_tree = DecisionTreeClassifier()\n",
        "decision_tree.fit(X_train, Y_train)\n",
        "Y_pred = decision_tree.predict(X_test)\n",
        "acc_decision_tree = round(decision_tree.score(X_train, Y_train) * 100, 2)\n",
        "acc_decision_tree"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "6976410f-92c7-a79a-16f1-382f593c5daf"
      },
      "outputs": [],
      "source": [
        "# Random Forest\n",
        "\n",
        "random_forest = RandomForestClassifier(n_estimators=100)\n",
        "random_forest.fit(X_train, Y_train)\n",
        "Y_pred = random_forest.predict(X_test)\n",
        "acc_random_forest = round(random_forest.score(X_train, Y_train) * 100, 2)\n",
        "acc_random_forest"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "854e79d6-e351-6604-ec1c-038ec12059e7"
      },
      "outputs": [],
      "source": [
        "models = pd.DataFrame({\n",
        "    'Model': ['Support Vector Machines', 'KNN', 'Logistic Regression', \n",
        "              'Random Forest', 'Naive Bayes', 'Perceptron', \n",
        "              'Stochastic Gradient Decent', 'Linear SVC', \n",
        "              'Decision Tree'],\n",
        "    'Score': [acc_svc, acc_knn, acc_log, \n",
        "              acc_random_forest, acc_gaussian, acc_perceptron, \n",
        "              acc_sgd, acc_linear_svc, acc_decision_tree]})\n",
        "models.sort_values(by='Score', ascending=False)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "bbcf1dec-fb18-354d-9d55-ae696ac42f78"
      },
      "outputs": [],
      "source": [
        "Score = [acc_svc, acc_knn, acc_log, \n",
        "              acc_random_forest, acc_gaussian, acc_perceptron, \n",
        "              acc_sgd, acc_linear_svc, acc_decision_tree]\n",
        "average = sum(Score)/len(Score)\n",
        "average"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "8651e581-c4dc-38c0-b494-89d6207ad0c7"
      },
      "outputs": [],
      "source": [
        "submission = pd.DataFrame({\n",
        "        \"PassengerId\": test_df[\"PassengerId\"],\n",
        "        \"Survived\": Y_pred\n",
        "    })\n",
        "submission.to_csv('submission.csv', index=False)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "277dfa22-938c-7746-6163-aa66e3c6578c"
      },
      "outputs": [],
      "source": [
        ""
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "28f64245-3cfb-d023-581b-d17c6c575bda"
      },
      "outputs": [],
      "source": [
        "#import xgboost as xgb\n",
        "# read in data\n",
        "#dtrain = xgb.DMatrix('demo/data/agaricus.txt.train')\n",
        "#dtest = xgb.DMatrix('demo/data/agaricus.txt.test')\n",
        "# specify parameters via map\n",
        "#param = {'max_depth':2, 'eta':1, 'silent':1, 'objective':'binary:logistic' }\n",
        "#num_round = 2\n",
        "#bst = xgb.train(param, dtrain, num_round)\n",
        "# make prediction\n",
        "#preds = bst.predict(dtest)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "69e9ff04-92f6-f4eb-9530-2c8090d03ba8"
      },
      "outputs": [],
      "source": [
        "X_train"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "1eb84b62-1dbf-cdc8-3aed-432cf44bede3",
        "collapsed": true
      },
      "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
}