{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import dautil as dl\n",
    "from sklearn.grid_search import GridSearchCV\n",
    "from sklearn.ensemble import RandomForestClassifier\n",
    "import ch9util\n",
    "import numpy as np\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",
    "clf = RandomForestClassifier(random_state=44)\n",
    "params = {\n",
    "    'max_depth': [2,  4],\n",
    "    'min_samples_leaf': [1, 3],\n",
    "    'criterion': ['gini', 'entropy'],\n",
    "    'n_estimators': [100, 200]\n",
    "}\n",
    "\n",
    "rfc = GridSearchCV(estimator=RandomForestClassifier(),\n",
    "                   param_grid=params, cv=5, n_jobs=-1)\n",
    "rfc.fit(X_train, y_train)\n",
    "preds = rfc.predict(X_test)\n",
    "ch9util.npy_save('rfc', preds)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "context = dl.nb.Context('random_forest')\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(preds, y_test, rfc.best_params_, sp.ax)\n",
    "\n",
    "ntrees = 2 ** np.arange(9)\n",
    "ch9util.plot_validation(sp.next_ax(), rfc.best_estimator_, \n",
    "                        X_train, y_train, 'n_estimators', ntrees)\n",
    "\n",
    "depths = np.arange(2, 9)\n",
    "ch9util.plot_validation(sp.next_ax(), rfc.best_estimator_, \n",
    "                        X_train, y_train, 'max_depth', depths)\n",
    "\n",
    "ch9util.plot_learn_curve(sp.next_ax(), \n",
    "                         rfc.best_estimator_, 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
}
