{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import ch9util\n",
    "from sklearn.grid_search import GridSearchCV\n",
    "from sklearn.ensemble import AdaBoostRegressor\n",
    "from sklearn.tree import DecisionTreeRegressor\n",
    "import numpy as np\n",
    "import dautil as dl\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.temp_split()\n",
    "params = {\n",
    "    'loss': ['linear', 'square', 'exponential'],\n",
    "    'base_estimator__min_samples_leaf': [1, 2]\n",
    "}\n",
    "reg = AdaBoostRegressor(base_estimator=DecisionTreeRegressor(random_state=28),\n",
    "                        random_state=17)\n",
    "gscv = GridSearchCV(estimator=reg,\n",
    "                    param_grid=params, cv=5, n_jobs=-1)\n",
    "gscv.fit(X_train, y_train)\n",
    "preds = gscv.predict(X_test)\n",
    "ch9util.npy_save('boosting', preds)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "context = dl.nb.Context('boosting')\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.scatter_predictions(preds, y_test, gscv.best_params_,  \n",
    "                                   gscv.best_score_, sp.ax)\n",
    "\n",
    "nestimators = 2 ** np.arange(3, 9)\n",
    "ch9util.plot_validation(sp.next_ax(), gscv.best_estimator_, \n",
    "                        X_train, y_train, 'n_estimators', nestimators)\n",
    "\n",
    "learn_rate = np.linspace(0.1, 1, 9)\n",
    "ch9util.plot_validation(sp.next_ax(), gscv.best_estimator_, \n",
    "                        X_train, y_train, 'learning_rate', learn_rate)\n",
    "\n",
    "ch9util.plot_learn_curve(sp.next_ax(), gscv.best_estimator_, \n",
    "                         X_train, y_train)\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
}
