{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "\n",
    "import numpy\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "numpy.random.seed(5)\n",
    "\n",
    "bx = 1.5\n",
    "by = 0.5\n",
    "data = numpy.random.rand(512, 2)\n",
    "label = ((numpy.square(bx + data[:,0]) + numpy.square(by + data[:,1])) < numpy.square(2.3)).astype(int)\n",
    "print(label.sum())\n",
    "\n",
    "plt.figure(figsize=(12,12))\n",
    "plt.scatter(data[label==0,0], data[label==0,1], c='b')\n",
    "plt.scatter(data[label==1,0], data[label==1,1], c='r')\n",
    "plt.show()\n",
    "\n",
    "with open('synthetic.txt', 'w') as fout:\n",
    "    for row, l in zip(data, label):\n",
    "        fout.write('{:.03f} {:.03f} {}\\n'.format(row[0], row[1], l))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "angle = numpy.arctan2(by+0.5, bx+0.5)\n",
    "proj = numpy.cos(angle)*data[:,0] + numpy.sin(angle)*data[:, 1]\n",
    "\n",
    "plt.figure(figsize=(12,12))\n",
    "plt.scatter(proj, numpy.random.rand(proj.shape[0]), c=[('r' if l==0 else 'b') for l in label])\n",
    "plt.show()\n",
    "\n",
    "guess = 0.7\n",
    "\n",
    "correct = numpy.logical_and(proj<guess,label==1).sum() + numpy.logical_and(proj>guess,label==0).sum()\n",
    "print(100 * correct / proj.shape[0])\n"
   ]
  }
 ],
 "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": 2
}
