{"cells":[
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load in \n\nimport 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 #plotting the data\nfrom mpl_toolkits.mplot3d import Axes3D #plotting in 3D\n\n# Input data files are available in the \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory\n\nfrom subprocess import check_output\nprint(check_output([\"ls\", \"../input\"]).decode(\"utf8\"))\n\n# Any results you write to the current directory are saved as output."
 },
 {
  "cell_type": "markdown",
  "metadata": {},
  "source": "load the dataset"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "train = pd.read_csv(\"../input/train.csv\")\nprint(train.head())"
 },
 {
  "cell_type": "markdown",
  "metadata": {},
  "source": "The x and y coordinates represent distance unit in terms of km with 4 decimals, rounding the x and y to precision of 1km"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "train['X_1000'] = train.x.apply(lambda x: round(x,0))\ntrain['Y_1000'] = train.y.apply(lambda y: round(y,0))"
 },
 {
  "cell_type": "markdown",
  "metadata": {},
  "source": "Now we group the data w.r.t. pid's and assign each pid x and y values which are the most frequenct among them."
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "grouped_pid = train.groupby('place_id')\nX = grouped_pid['X_1000'].agg(lambda x: x.value_counts().index[0])\nY = grouped_pid['Y_1000'].agg(lambda x: x.value_counts().index[0])\ncoord = pd.concat([X, Y], axis=1)\ncoord = coord.reset_index()\ncoord.columns = ['place_id', 'pid_x', 'pid_y']\nprint(coord.head())"
 },
 {
  "cell_type": "markdown",
  "metadata": {},
  "source": "combining the pid table with the original dataset"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "train_merged = train.merge(coord, how='left')\nprint(train_merged.head())"
 },
 {
  "cell_type": "markdown",
  "metadata": {},
  "source": "calculating the distance of each point with its pid coordinates and plottingn the result"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "train_merged['dist'] = ((train_merged.x - train_merged.pid_x)**2 + (train_merged.y - train_merged.pid_y)**2).apply(lambda x: x**0.5)\nplt.scatter(train_merged.accuracy, train_merged.dist)\nplt.xlabel('Accuracy')\nplt.ylabel('Distance')\nplt.title('Distance vs Accuracy')\nplt.show()"
 },
 {
  "cell_type": "markdown",
  "metadata": {},
  "source": "the graph here poses more queries than answers, had expected some trend along it. need to investigate further."
 },
 {
  "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}