{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-08-11T06:53:44.152554Z",
     "start_time": "2019-08-11T06:53:44.147312Z"
    },
    "run_control": {
     "marked": false
    }
   },
   "outputs": [],
   "source": [
    "from sklearn.naive_bayes import GaussianNB\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-08-11T07:03:57.307101Z",
     "start_time": "2019-08-11T07:03:57.290922Z"
    },
    "run_control": {
     "marked": false
    }
   },
   "outputs": [],
   "source": [
    "#!/usr/bin/python\n",
    "\n",
    "import base64\n",
    "import json\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "import pylab as pl\n",
    "\n",
    "\n",
    "def prettyPicture(clf, X_test, y_test):\n",
    "    x_min = 0.0\n",
    "    x_max = 1.0\n",
    "    y_min = 0.0\n",
    "    y_max = 1.0\n",
    "\n",
    "    # Plot the decision boundary. For that, we will assign a color to each\n",
    "    # point in the mesh [x_min, m_max]x[y_min, y_max].\n",
    "    h = .01  # step size in the mesh\n",
    "    xx, yy = np.meshgrid(np.arange(x_min, x_max, h),\n",
    "                         np.arange(y_min, y_max, h))\n",
    "    Z = clf.predict(np.c_[xx.ravel(), yy.ravel()])\n",
    "\n",
    "    # Put the result into a color plot\n",
    "    Z = Z.reshape(xx.shape)\n",
    "    plt.xlim(xx.min(), xx.max())\n",
    "    plt.ylim(yy.min(), yy.max())\n",
    "\n",
    "    plt.pcolormesh(xx, yy, Z, cmap=pl.cm.seismic)\n",
    "\n",
    "    # Plot also the test points\n",
    "    grade_sig = [X_test[ii][0]\n",
    "                 for ii in range(0, len(X_test)) if y_test[ii] == 0]\n",
    "    bumpy_sig = [X_test[ii][1]\n",
    "                 for ii in range(0, len(X_test)) if y_test[ii] == 0]\n",
    "    grade_bkg = [X_test[ii][0]\n",
    "                 for ii in range(0, len(X_test)) if y_test[ii] == 1]\n",
    "    bumpy_bkg = [X_test[ii][1]\n",
    "                 for ii in range(0, len(X_test)) if y_test[ii] == 1]\n",
    "\n",
    "    plt.scatter(grade_sig, bumpy_sig, color=\"b\", label=\"fast\")\n",
    "    plt.scatter(grade_bkg, bumpy_bkg, color=\"r\", label=\"slow\")\n",
    "    plt.legend()\n",
    "    plt.xlabel(\"bumpiness\")\n",
    "    plt.ylabel(\"grade\")\n",
    "\n",
    "    plt.savefig(\"test.png\",dpi=300)\n",
    "\n",
    "\n",
    "def output_image(name, format, bytes):\n",
    "    image_start = \"BEGIN_IMAGE_f9825uweof8jw9fj4r8\"\n",
    "    image_end = \"END_IMAGE_0238jfw08fjsiufhw8frs\"\n",
    "    data = {}\n",
    "    data['name'] = name\n",
    "    data['format'] = format\n",
    "    data['bytes'] = base64.encodestring(bytes)\n",
    "    print(image_start+json.dumps(data)+image_end)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-08-11T07:03:57.841214Z",
     "start_time": "2019-08-11T07:03:57.824989Z"
    }
   },
   "outputs": [],
   "source": [
    "#!/usr/bin/python\n",
    "import random\n",
    "\n",
    "\n",
    "def makeTerrainData(n_points=5000):\n",
    "    ###############################################################################\n",
    "    # make the toy dataset\n",
    "    random.seed(42)\n",
    "    grade = [random.random() for ii in range(0, n_points)]\n",
    "    bumpy = [random.random() for ii in range(0, n_points)]\n",
    "    error = [random.random() for ii in range(0, n_points)]\n",
    "    y = [round(grade[ii]*bumpy[ii]+0.3+0.1*error[ii])\n",
    "         for ii in range(0, n_points)]\n",
    "    for ii in range(0, len(y)):\n",
    "        if grade[ii] > 0.8 or bumpy[ii] > 0.8:\n",
    "            y[ii] = 1.0\n",
    "\n",
    "# split into train/test sets\n",
    "    X = [[gg, ss] for gg, ss in zip(grade, bumpy)]\n",
    "    split = int(0.75*n_points)\n",
    "    X_train = X[0:split]\n",
    "    X_test = X[split:]\n",
    "    y_train = y[0:split]\n",
    "    y_test = y[split:]\n",
    "\n",
    "    grade_sig = [X_train[ii][0]\n",
    "                 for ii in range(0, len(X_train)) if y_train[ii] == 0]\n",
    "    bumpy_sig = [X_train[ii][1]\n",
    "                 for ii in range(0, len(X_train)) if y_train[ii] == 0]\n",
    "    grade_bkg = [X_train[ii][0]\n",
    "                 for ii in range(0, len(X_train)) if y_train[ii] == 1]\n",
    "    bumpy_bkg = [X_train[ii][1]\n",
    "                 for ii in range(0, len(X_train)) if y_train[ii] == 1]\n",
    "\n",
    "    training_data = {\"fast\": {\"grade\": grade_sig, \"bumpiness\": bumpy_sig}, \"slow\": {\n",
    "        \"grade\": grade_bkg, \"bumpiness\": bumpy_bkg}}\n",
    "\n",
    "    grade_sig = [X_test[ii][0]\n",
    "                 for ii in range(0, len(X_test)) if y_test[ii] == 0]\n",
    "    bumpy_sig = [X_test[ii][1]\n",
    "                 for ii in range(0, len(X_test)) if y_test[ii] == 0]\n",
    "    grade_bkg = [X_test[ii][0]\n",
    "                 for ii in range(0, len(X_test)) if y_test[ii] == 1]\n",
    "    bumpy_bkg = [X_test[ii][1]\n",
    "                 for ii in range(0, len(X_test)) if y_test[ii] == 1]\n",
    "\n",
    "    test_data = {\"fast\": {\"grade\": grade_sig, \"bumpiness\": bumpy_sig},\n",
    "                 \"slow\": {\"grade\": grade_bkg, \"bumpiness\": bumpy_bkg}}\n",
    "\n",
    "    return X_train, y_train, X_test, y_test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-08-11T07:04:15.015777Z",
     "start_time": "2019-08-11T07:04:14.437679Z"
    },
    "hide_output": false,
    "run_control": {
     "marked": false
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Accuracy is 89.68 %\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 46076 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "data_train, label_train, data_test, label_test = makeTerrainData()\n",
    "clf = GaussianNB()\n",
    "clf.fit(data_train, label_train)\n",
    "\n",
    "result = clf.predict(data_test)\n",
    "\n",
    "num = 0\n",
    "corr = 0\n",
    "for x in result:\n",
    "    if x == label_test[num]:\n",
    "        corr += 1\n",
    "    num += 1\n",
    "print(\"Accuracy is\", corr/num*100,\"%\")\n",
    "prettyPicture(clf, data_test, label_test)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "run_control": {
     "marked": false
    }
   },
   "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.7.3"
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
