{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import dautil as dl\n",
    "from sklearn.tree import DecisionTreeClassifier\n",
    "import numpy as np\n",
    "import ch9util\n",
    "from sklearn.ensemble import VotingClassifier\n",
    "from sklearn.grid_search import GridSearchCV\n",
    "from IPython.display import HTML\n",
    "import warnings\n",
    "\n",
    "warnings.filterwarnings(action='ignore', category=FutureWarning)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "X_train, X_test, y_train, y_test = ch9util.rain_split()\n",
    "default = DecisionTreeClassifier(random_state=53, min_samples_leaf=3,\n",
    "                                 max_depth=4)\n",
    "entropy = DecisionTreeClassifier(criterion='entropy',\n",
    "                                 min_samples_leaf=3, max_depth=4,\n",
    "                                 random_state=57)\n",
    "random = DecisionTreeClassifier(splitter='random', min_samples_leaf=3,\n",
    "                                max_depth=4, random_state=5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "clf = VotingClassifier([('default', default), \n",
    "                        ('entropy', entropy), ('random', random)])\n",
    "params = {'voting': ['soft', 'hard'],\n",
    "         'weights': [None, (2, 1, 1), (1, 2, 1), (1, 1, 2)]}\n",
    "gscv = GridSearchCV(clf, param_grid=params, n_jobs=-1, cv=5)\n",
    "gscv.fit(X_train, y_train)\n",
    "votes = gscv.predict(X_test)\n",
    "\n",
    "preds = []\n",
    "\n",
    "for n, c in zip(['default', 'entropy', 'random'], \n",
    "             [default, entropy, random]):\n",
    "    c.fit(X_train, y_train)\n",
    "    preds.append(c.predict(X_test))\n",
    "    ch9util.npy_save(n, preds[-1])\n",
    "                 \n",
    "preds = np.array(preds)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "context = dl.nb.Context('stacking_multiple')\n",
    "dl.nb.RcWidget(context)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "sp = dl.plotting.Subplotter(2, 2, context)\n",
    "html = ch9util.report_rain(votes, y_test, gscv.best_params_, sp.ax)\n",
    "sp.ax.set_title(sp.ax.get_title() + ' | Voting')\n",
    "\n",
    "preds_train = np.array([clf.predict(X_train)\n",
    "                        for clf in [default, entropy, random]])\n",
    "\n",
    "default.fit(preds_train.T, y_train)\n",
    "stacked_preds = default.predict(preds.T)\n",
    "ch9util.npy_save('stacking', stacked_preds)\n",
    "html += ch9util.report_rain(stacked_preds, \n",
    "                            y_test, default.get_params(), sp.next_ax())\n",
    "sp.ax.set_title(sp.ax.get_title() + ' | Stacking')\n",
    "\n",
    "ch9util.plot_learn_curve(sp.next_ax(), gscv.best_estimator_, X_train,\n",
    "                         y_train, title='Voting')\n",
    "\n",
    "ch9util.plot_learn_curve(sp.next_ax(), default, X_train,\n",
    "                         y_train, title='Stacking')\n",
    "HTML(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
}
