{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "f48b7a61-f920-bba0-2465-f0e208a664b9"
      },
      "outputs": [],
      "source": [
        "# Ignore warnings\n",
        "import warnings\n",
        "warnings.filterwarnings('ignore')\n",
        "\n",
        "# Handle table-like data and matrices\n",
        "import numpy as np\n",
        "import pandas as pd\n",
        "\n",
        "# Modelling Algorithms\n",
        "from sklearn.tree import DecisionTreeClassifier\n",
        "from sklearn.linear_model import LogisticRegression\n",
        "from sklearn.neighbors import KNeighborsClassifier\n",
        "from sklearn.naive_bayes import GaussianNB\n",
        "from sklearn.svm import SVC, LinearSVC\n",
        "from sklearn.ensemble import RandomForestClassifier , GradientBoostingClassifier\n",
        "\n",
        "# Modelling Helpers\n",
        "from sklearn.preprocessing import Imputer , Normalizer , scale\n",
        "from sklearn.cross_validation import train_test_split , StratifiedKFold\n",
        "from sklearn.feature_selection import RFECV\n",
        "\n",
        "# Visualisation\n",
        "import matplotlib as mpl\n",
        "import matplotlib.pyplot as plt\n",
        "import matplotlib.pylab as pylab\n",
        "import seaborn as sns\n",
        "\n",
        "# Configure visualisations\n",
        "%matplotlib inline\n",
        "mpl.style.use( 'ggplot' )\n",
        "sns.set_style( 'white' )\n",
        "pylab.rcParams[ 'figure.figsize' ] = 8 , 6\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "19f1924f-fba1-d61d-0ade-14dba78429b8"
      },
      "outputs": [],
      "source": [
        "def plot_histograms( df , variables , n_rows , n_cols ):\n",
        "    fig = plt.figure( figsize = ( 16 , 12 ) )\n",
        "    for i, var_name in enumerate( variables ):\n",
        "        ax=fig.add_subplot( n_rows , n_cols , i+1 )\n",
        "        df[ var_name ].hist( bins=10 , ax=ax )\n",
        "        ax.set_title( 'Skew: ' + str( round( float( df[ var_name ].skew() ) , ) ) ) # + ' ' + var_name ) #var_name+\" Distribution\")\n",
        "        ax.set_xticklabels( [] , visible=False )\n",
        "        ax.set_yticklabels( [] , visible=False )\n",
        "    fig.tight_layout()  # Improves appearance a bit.\n",
        "    plt.show()\n",
        "\n",
        "def plot_distribution( df , var , target , **kwargs ):\n",
        "    row = kwargs.get( 'row' , None )\n",
        "    col = kwargs.get( 'col' , None )\n",
        "    facet = sns.FacetGrid( df , hue=target , aspect=4 , row = row , col = col )\n",
        "    facet.map( sns.kdeplot , var , shade= True )\n",
        "    facet.set( xlim=( 0 , df[ var ].max() ) )\n",
        "    facet.add_legend()\n",
        "\n",
        "def plot_categories( df , cat , target , **kwargs ):\n",
        "    row = kwargs.get( 'row' , None )\n",
        "    col = kwargs.get( 'col' , None )\n",
        "    facet = sns.FacetGrid( df , row = row , col = col )\n",
        "    facet.map( sns.barplot , cat , target )\n",
        "    facet.add_legend()\n",
        "\n",
        "def plot_correlation_map( df ):\n",
        "    corr = titanic.corr()\n",
        "    _ , ax = plt.subplots( figsize =( 12 , 10 ) )\n",
        "    cmap = sns.diverging_palette( 220 , 10 , as_cmap = True )\n",
        "    _ = sns.heatmap(\n",
        "        corr, \n",
        "        cmap = cmap,\n",
        "        square=True, \n",
        "        cbar_kws={ 'shrink' : .9 }, \n",
        "        ax=ax, \n",
        "        annot = True, \n",
        "        annot_kws = { 'fontsize' : 12 }\n",
        "    )\n",
        "\n",
        "def describe_more( df ):\n",
        "    var = [] ; l = [] ; t = []\n",
        "    for x in df:\n",
        "        var.append( x )\n",
        "        l.append( len( pd.value_counts( df[ x ] ) ) )\n",
        "        t.append( df[ x ].dtypes )\n",
        "    levels = pd.DataFrame( { 'Variable' : var , 'Levels' : l , 'Datatype' : t } )\n",
        "    levels.sort_values( by = 'Levels' , inplace = True )\n",
        "    return levels\n",
        "\n",
        "def plot_variable_importance( X , y ):\n",
        "    tree = DecisionTreeClassifier( random_state = 99 )\n",
        "    tree.fit( X , y )\n",
        "    plot_model_var_imp( tree , X , y )\n",
        "    \n",
        "def plot_model_var_imp( model , X , y ):\n",
        "    imp = pd.DataFrame( \n",
        "        model.feature_importances_  , \n",
        "        columns = [ 'Importance' ] , \n",
        "        index = X.columns \n",
        "    )\n",
        "    imp = imp.sort_values( [ 'Importance' ] , ascending = True )\n",
        "    imp[ : 10 ].plot( kind = 'barh' )\n",
        "    print (model.score( X , y ))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "d374cc1c-3327-bf8c-ec08-34aa09d9aff1"
      },
      "outputs": [],
      "source": [
        "# get titanic & test csv files as a DataFrame\n",
        "train = pd.read_csv(\"../input/train.csv\")\n",
        "test    = pd.read_csv(\"../input/test.csv\")\n",
        "\n",
        "full = train.append( test , ignore_index = True )\n",
        "titanic = full[ :891 ]\n",
        "\n",
        "del train , test\n",
        "\n",
        "print ('Datasets:' , 'full:' , full.shape , 'titanic:' , titanic.shape)"
      ]
    }
  ],
  "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
}