{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from sklearn.metrics import confusion_matrix\n",
    "import seaborn as sns\n",
    "import dautil as dl\n",
    "from IPython.display import HTML\n",
    "import ch10util"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def plot_cm(preds, y_test, title, cax):\n",
    "    cm = confusion_matrix(preds.T, y_test)\n",
    "    normalized_cm = cm/cm.sum().astype(float)\n",
    "    sns.heatmap(normalized_cm, annot=True, fmt='.2f', vmin=0, vmax=1,\n",
    "                xticklabels=['Rain', 'No Rain'],\n",
    "                yticklabels=['Rain', 'No Rain'], ax=cax)\n",
    "    cax.set_xlabel('Predicted class')\n",
    "    cax.set_ylabel('Expected class')\n",
    "    cax.set_title('Confusion Matrix for Rain Forecast | ' + title)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "context = dl.nb.Context('conf_matrix')\n",
    "dl.nb.RcWidget(context)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "y_test = np.load('rain_y_test.npy')\n",
    "sp = dl.plotting.Subplotter(2, 2, context)\n",
    "\n",
    "plot_cm(y_test, np.load('rfc.npy'), 'Random Forest', sp.ax)\n",
    "\n",
    "plot_cm(y_test, np.load('bagging.npy'), 'Bagging', sp.next_ax())\n",
    "\n",
    "plot_cm(y_test, np.load('votes.npy'),'Votes', sp.next_ax())\n",
    "\n",
    "plot_cm(y_test, np.load('stacking.npy'), 'Stacking', sp.next_ax())\n",
    "sp.fig.text(0, 1, ch10util.classifiers())\n",
    "HTML(sp.exit())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "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.4.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
