{"cells":[
 {
  "cell_type": "markdown",
  "metadata": {},
  "source": "# The Space\n\nIn this script we explore the area and dimentions of places. \nWe also see that the space is anisotropic, variance along x about 33 time more than y. \n"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport matplotlib.pyplot as plt"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "train = pd.read_csv('../input/train.csv', index_col='row_id')"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "plt.rcParams['figure.figsize'] = (12,10)\ntrain.sample(n=1000000).plot(x='x', y='y', kind='hexbin', gridsize=100)\nplt.title('Density of checkins')\nplt.xlabel('x')"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "train.x.hist(bins=1000)"
 },
 {
  "cell_type": "markdown",
  "metadata": {},
  "source": "## Radius of Gyration and Area\n\nThey tells us how big a place is."
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "g = train.groupby('place_id')\nplace = g.mean()\n\nplace['counts'] = g.x.count()\n\nstd = g.std()\nmean = g.mean()\nplace['rg'] = np.sqrt(std.x**2 + std.y**2)\nplace['area'] = std.x * std.y"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "plt.rcParams['figure.figsize'] = (16,4)\nplace.rg.hist(bins=200, log=True);\nplt.xlabel('Radius of Gyration')"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "place.area.hist(bins=200, log=True);\nplt.xlabel('Area')"
 },
 {
  "cell_type": "markdown",
  "metadata": {},
  "source": "Some places are really big.  Let zoom in:"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "min_count = 1500\nprint(place.rg[place.counts[place.counts > min_count].index].max())\nprint(place.rg[place.counts[place.counts > min_count].index].min())\nlargest_place = place.rg[place.counts[place.counts > min_count].index].argmax()\nsmallest_place = place.rg[place.counts[place.counts > min_count].index].argmin()\n"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "plt.rcParams['figure.figsize'] = (4,4)\n\ntrain.ix[(train.place_id == largest_place),:].plot(x='x', y='y', kind='scatter')\nplt.title('largest rg')\nplt.xlim(0,10)\nplt.ylim(0,10);"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "train.ix[(train.place_id == smallest_place),: ].plot(x='x', y='y', kind='scatter')\nplt.title('smallest rg')\nplt.xlim(0,10)\nplt.ylim(0,10);"
 },
 {
  "cell_type": "markdown",
  "metadata": {},
  "source": "This is a place with more than 1500 checkins. \ny goes from 0.31 to 0.46, but x goes from **0.02** to **9.3**. \nThat should have been a very long place!"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "min_count = 1000\nprint(place.area[place.counts[place.counts > min_count].index].max())\nprint(place.area[place.counts[place.counts > min_count].index].min())\nlargest_area = place.area[place.counts[place.counts > min_count].index].argmax()\nsmallest_area = place.area[place.counts[place.counts > min_count].index].argmin()"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "plt.rcParams['figure.figsize'] = (4,4)\ntrain[train.place_id == largest_area].plot(x='x', y='y', kind='scatter')\nplt.title('largest area')\n\nplt.xlim(0,10)\nplt.ylim(0,10);"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "plt.rcParams['figure.figsize'] = (4,4)\ntrain[train.place_id == smallest_area].plot(x='x', y='y', kind='scatter')\nplt.title('smallest area')\n\nplt.xlim(0,10)\nplt.ylim(0,10);"
 },
 {
  "cell_type": "markdown",
  "metadata": {},
  "source": "## Anisotropy of the space\n\nThe standard deviation of places along x is 32.7 time more than y:"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "std.x.mean()/std.y.mean()"
 }
],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"}}, "nbformat": 4, "nbformat_minor": 0}