{"cells":[
 {
  "cell_type": "markdown",
  "metadata": {},
  "source": "# Voronoi diagrams of places\n\nFor a given `place_id`, we can find the median of all the x,y values for a check-in event. We will use this to approximate the location of the place being checked-in to.\n\n\nThen we will look at the [Voronoi diagrams](https://en.wikipedia.org/wiki/Voronoi_diagram) of those places, we will start with the upper left corner. Since the check-ins are pretty close to uniformly distributed over the given square, it suffices to take the square in the corner defined by `[(0,L), (0,L)]`, we can vary `L` to explore the Voronoi diagrams of a larger and larger swathe of the space, to get a feel for the landscape."
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nfrom scipy.spatial import Voronoi, voronoi_plot_2d\ndf_train = pd.read_csv(\"../input/train.csv\")\ncentroids = df_train[[\"x\",\"y\",\"place_id\"]].groupby(\"place_id\").median()"
 },
 {
  "cell_type": "markdown",
  "metadata": {},
  "source": "Since there are about 100,000 points uniformly distributed in a 10x10 square, we can choose any corner and have it be a reasonably representative sample. After experimenting a bit, I went with the square with corners `(0,0)` and `(0.125,0.125)`, which contains 12 points. This is a good small example to get some familiarity with a little bit of the data, and with using scipy's Voronoi class."
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "cent_small = centroids[(centroids[\"x\"] < 0.125) & (centroids[\"y\"] < 0.125)]\ncent_small"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "vor = Voronoi(cent_small, qhull_options=\"Qc\")\nvoronoi_plot_2d(vor)\nplt.show()"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "# Now let's look at a larger area:\nvor = Voronoi(centroids[(centroids[\"x\"] < 0.3) & (centroids[\"y\"] < 0.3)], qhull_options=\"Qc\")\nvoronoi_plot_2d(vor)\nplt.show()"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": ""
 }
],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"}}, "nbformat": 4, "nbformat_minor": 0}