{"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\n\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\nimport os\nimport numpy as np\nimport pandas as pd\nfrom pylab import *\nfrom mpl_toolkits.mplot3d import Axes3D\nfrom bokeh.plotting import figure, output_file, show\nimport matplotlib.pyplot as plt\nimport matplotlib.mlab as mlab\nfrom matplotlib.colors import LogNorm\n\n# Any results you write to the current directory are saved as output.\nprint('done')\ntrain_dir = \"../input\"\ntrain_file = \"train.csv\"\nfbcheckin_train_tbl = pd.read_csv(os.path.join(train_dir, train_file))\nprint (fbcheckin_train_tbl.head(10))\n\n\n# Assuming time values were time elapsed epoch times. we can now calculate the hours, minutes, days and seconds from last checkin \n\nfbcheckin_train_tbl['timeinmin'] = fbcheckin_train_tbl['time']\nfbcheckin_train_tbl['time_of_week'] = fbcheckin_train_tbl['time'] % 10080\nfbcheckin_train_tbl['hour_of_day']  = (fbcheckin_train_tbl['time_of_week'] / 60) % 24\nfbcheckin_train_tbl['hour_number_for_week'] = fbcheckin_train_tbl['time'] % (10080) //60.\nfbcheckin_train_tbl['day_of_week'] = fbcheckin_train_tbl['hour_number_for_week'] // 24.\n\n\n\n\none_id = fbcheckin_train_tbl[fbcheckin_train_tbl['place_id']==8523065625]['hour_number_for_week']\nn, bins, patches = plt.hist(one_id, 168 , histtype='bar')\nplt.show()\n\n\n\n                                  \n\n\n\n\n\n\n"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "\n# check for cycles over the course of a day (top) and week (bottom)\ndef plotMeanbyday(selected_id):\n    f, axarr = plt.subplots(3,3,figsize=(15, 9));\n    for j in range(0,7):\n        fd_by_day = selected_id[selected_id['day_of_week'] == j]['hour_number_for_week'] % 24\n        #print('Day',j,'Median',int(fd_by_day.median()))\n        #n, bins, patches = axs[j].hist(fd_by_day,7);\n        \n        if j == 0:\n            axarr[0,0].hist(fd_by_day,7);\n            axarr[0,0].axvline(fd_by_day.mean(), color='r', linestyle='dashed', linewidth=2)\n            axarr[0,0].set_title('Day 1')\n        if j == 1:\n            axarr[0,1].hist(fd_by_day,7);\n            axarr[0,1].axvline(fd_by_day.mean(), color='r', linestyle='dashed', linewidth=2)\n            axarr[0,1].set_title('Day 2')\n        if j == 2:\n            axarr[0,2].hist(fd_by_day,7);\n            axarr[0,2].axvline(fd_by_day.mean(), color='r', linestyle='dashed', linewidth=2)\n            axarr[0,2].set_title('Day 3')\n        if j == 3:\n            axarr[1,0].hist(fd_by_day,7);\n            axarr[1,0].axvline(fd_by_day.mean(), color='r', linestyle='dashed', linewidth=2)\n            axarr[1,0].set_title('Day 3')\n        if j == 4:\n            axarr[1,1].hist(fd_by_day,7);\n            axarr[1,1].axvline(fd_by_day.mean(), color='r', linestyle='dashed', linewidth=2)\n            axarr[1,1].set_title('Day 4')\n        if j == 5:\n            axarr[1,2].hist(fd_by_day,7);\n            axarr[1,2].axvline(fd_by_day.mean(), color='r', linestyle='dashed', linewidth=2)\n            axarr[1,2].set_title('Day 5')\n        if j == 6:\n            axarr[2,0].hist(fd_by_day,7);\n            axarr[2,0].axvline(fd_by_day.mean(), color='r', linestyle='dashed', linewidth=2)\n            axarr[2,0].set_title('Day 6')\n        \n            \n        plt.xlabel('Time (Hours)');\n        plt.ylabel('Frequency');\n        f.show();\n        \nselected_id = fbcheckin_train_tbl[fbcheckin_train_tbl['place_id']==8523065625]   \nplotMeanbyday(selected_id)\nfd_by_day_allweeks = selected_id['hour_number_for_week'] % 24\nfd_by_day_allweeksmedian = fd_by_day_allweeks.median()\nprint('Place_id_median',fd_by_day_allweeksmedian) \nprint('Place_id_mean',int(fd_by_day_allweeks.mean())) "
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "# check for cycles over the course of a day (top) and week (bottom)\ndef plotMeanbyday(selected_id):\n    f, axarr = plt.subplots(3,3,figsize=(15, 9));\n    for j in range(0,7):\n        fd_by_day = selected_id[selected_id['day_of_week'] == j]['hour_number_for_week'] % 24\n        #print('Day',j,'Median',int(fd_by_day.median()))\n        #n, bins, patches = axs[j].hist(fd_by_day,7);\n        \n        if j == 0:\n            axarr[0,0].hist(fd_by_day,7);\n            axarr[0,0].axvline(fd_by_day.mean(), color='r', linestyle='dashed', linewidth=2)\n            axarr[0,0].set_title('Day 1')\n        if j == 1:\n            axarr[0,1].hist(fd_by_day,7);\n            axarr[0,1].axvline(fd_by_day.mean(), color='r', linestyle='dashed', linewidth=2)\n            axarr[0,1].set_title('Day 2')\n        if j == 2:\n            axarr[0,2].hist(fd_by_day,7);\n            axarr[0,2].axvline(fd_by_day.mean(), color='r', linestyle='dashed', linewidth=2)\n            axarr[0,2].set_title('Day 3')\n        if j == 3:\n            axarr[1,0].hist(fd_by_day,7);\n            axarr[1,0].axvline(fd_by_day.mean(), color='r', linestyle='dashed', linewidth=2)\n            axarr[1,0].set_title('Day 3')\n        if j == 4:\n            axarr[1,1].hist(fd_by_day,7);\n            axarr[1,1].axvline(fd_by_day.mean(), color='r', linestyle='dashed', linewidth=2)\n            axarr[1,1].set_title('Day 4')\n        if j == 5:\n            axarr[1,2].hist(fd_by_day,7);\n            axarr[1,2].axvline(fd_by_day.mean(), color='r', linestyle='dashed', linewidth=2)\n            axarr[1,2].set_title('Day 5')\n        if j == 6:\n            axarr[2,0].hist(fd_by_day,7);\n            axarr[2,0].axvline(fd_by_day.mean(), color='r', linestyle='dashed', linewidth=2)\n            axarr[2,0].set_title('Day 6')\n        \n            \n        plt.xlabel('Time (Hours)');\n        plt.ylabel('Frequency');\n        f.show();\n        \nselected_id = fbcheckin_train_tbl[fbcheckin_train_tbl['place_id']==1308450003]   \nplotMeanbyday(selected_id)\nfd_by_day_allweeks = selected_id['hour_number_for_week'] % 24\nfd_by_day_allweeksmedian = fd_by_day_allweeks.median()\nprint('Place_id_median',fd_by_day_allweeksmedian) \nprint('Place_id_mean',int(fd_by_day_allweeks.mean())) "
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "fbcheckin_train_tbl['hour_dist'] = fbcheckin_train_tbl['hour_number_for_week'] % 24 \none_id = fbcheckin_train_tbl[fbcheckin_train_tbl['place_id']==8523065625]\nfiltered = fbcheckin_train_tbl[['x','y','hour_dist']]\n\n### And the same exploration by weekday\n### based on localization info\nids = ['8523065625','1623394281']\nfig = plt.figure(figsize=(15,15))\n\nfor day in range(7):\n    sp = fig.add_subplot(3,3,day+1)\n    x = one_id[one_id['day_of_week'] == day]['x']\n    y = one_id[one_id['day_of_week'] == day]['y']\n    sp.hist2d(x, y, bins=20, norm=LogNorm())\n    sp.set_title('x and y location histogram day %d' % day)\n    sp.set_xlabel('x')\n    sp.set_ylabel('y')\nplt.show()"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "# seems the outliers are hugley skeyed along the x axis \nfbcheckin_train_tbl['hour_dist'] = fbcheckin_train_tbl['hour_number_for_week'] % 24 \none_id = fbcheckin_train_tbl[fbcheckin_train_tbl['place_id']==1308450003]\nfiltered = fbcheckin_train_tbl[['x','y','hour_dist']]\n\n### And the same exploration by weekday\n### based on localization info\nids = ['8523065625','1623394281']\nfig = plt.figure(figsize=(10,10))\n\nfor day in range(7):\n    sp = fig.add_subplot(3,3,day+1)\n    x = one_id[one_id['day_of_week'] == day]['x']\n    y = one_id[one_id['day_of_week'] == day]['y']\n    sp.hist2d(x, y, bins=20, norm=LogNorm())\n    sp.set_title('x and y location histogram day %d' % day)\n    sp.set_xlabel('x')\n    sp.set_ylabel('y')\nplt.show()"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "\n\n# the deviation seems to be consistantly on the x axis ! the spread on x is huge ??\none_id = fbcheckin_train_tbl[fbcheckin_train_tbl['place_id']==8523065625]\nx_centre = one_id['x'].sum() / len(one_id['x'])\ny_centre = one_id['y'].sum() / len(one_id['y'])\none_id['deviation_x'] = one_id['x'] - x_centre\none_id['deviation_y'] = one_id['y'] - y_centre\n#print (one_id.head(10))\n# red dashes, blue squares and green triangles\n#plt.plot(one_id['deviation_y'],one_id['accuracy'],'.')\n#plt.show()\nfig = plt.figure(figsize=(10,10))\nids = ['8523065625','1623394281','1308450003','6289802927','9931249544','5662813655','1137537235']\n\n\nfor id in ids:\n    sp = fig.add_subplot(3,3,int(ids.index(id)+1))\n    one_id = fbcheckin_train_tbl[fbcheckin_train_tbl['place_id']==int(id)]\n    x_centre = one_id['x'].sum() / len(one_id['x'])\n    y_centre = one_id['y'].sum() / len(one_id['y'])\n    one_id['deviation_x'] = one_id['x'] - x_centre\n    one_id['deviation_y'] = one_id['y'] - y_centre\n    sp.plot(one_id['deviation_x'],one_id['accuracy'],'.')\n    sp.set_title('x-deviation and accuracy')\n    sp.set_xlabel('x')\n    sp.set_ylabel('accuracy')\nplt.show()\n# From the deviations of x from the centroid and plotting the accuracy we can now see that the accuracy\n# goes beserk as the checkin is more near to the location. Or the GPS goes crazy when the customer is \n# inside the building. "
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "# Also look at the spread. Some are more clustered around the 0s. Why is that. This means that the\n# accuracys are more often to be inaccurate. May be these places are near high rise buildings. Why so ? \n# as high rise buildings block sky visibiity. So does visibility to GPS Satellites. So some locations are \n# near downtown may be ? or surrounded by sky rise buildings. \n"
 },
 {
  "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}