{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import ch9util\n",
    "from sklearn import linear_model\n",
    "from sklearn.grid_search import GridSearchCV\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",
    "ransac = linear_model.RANSACRegressor(random_state=27)\n",
    "params = {\n",
    "    'max_trials': [50, 100, 200],\n",
    "    'stop_probability': [0.98, 0.99]\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "gscv = GridSearchCV(estimator=ransac, param_grid=params, cv=5)\n",
    "gscv.fit(X_train, y_train)\n",
    "preds = gscv.predict(X_test)\n",
    "ch9util.npy_save('ransac', preds)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "context = dl.nb.Context('fit_ransac')\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",
    "trials = 10 * np.arange(5, 20)\n",
    "ch9util.plot_validation(sp.next_ax(), gscv.best_estimator_, \n",
    "                        X_train, y_train, 'max_trials', trials)\n",
    "\n",
    "probs = 0.01 * np.arange(90, 99)\n",
    "ch9util.plot_validation(sp.next_ax(), gscv.best_estimator_, \n",
    "                        X_train, y_train, 'stop_probability', probs)\n",
    "\n",
    "ninliers = 2 ** np.arange(4, 14)\n",
    "ch9util.plot_validation(sp.next_ax(), gscv.best_estimator_, \n",
    "                        X_train, y_train, 'stop_n_inliers', ninliers)\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
}
