{"cells":[
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "# most of code is copied from https://www.kaggle.com/andersonk/facebook-v-predicting-check-ins/timestamps-structure\n\n\nimport pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport datetime\nimport scipy.signal\n\n%matplotlib inline\nplt.rcParams['figure.figsize'] = (14.0, 100.0)\n\ndef calculate_time(minutes):\n    \"\"\"\n    0 for morning\n    1 for lunch time\n    2 for afternoon\n    3 for dinner time\n    4 for late night\n    \"\"\"\n    time_of_day = [4,4,4,4,4,4,0,0,0,0,0,1,1,1,1,2,2,2,3,3,3,3,4,4]\n    return ((minutes%(24*60*7))//(24*60))*5+time_of_day[(minutes%(24*60))//60]\n\ntrain = pd.read_csv('../input/train.csv')\n\nn,m = np.shape(train)\ntime = train['time']\ntrain['TODOW'] = time.apply(calculate_time)\n\nplaces_by_frequency = train.groupby('place_id')['place_id'].agg('count').sort_values(ascending=False).index.tolist()\nnplaces = 20\nplaces_by_frequency = places_by_frequency[:nplaces]\n\nplace_n = 0\ntime_axis = np.array([i for i in range(35)])\ntime_axis = np.reshape(time_axis,(35,1))\nfig, axs = plt.subplots(nplaces,1)\n\nfor place_id in places_by_frequency:\n  times = np.squeeze(train[train['place_id']==place_id].as_matrix(columns=['TODOW']))\n  times_by_frequency = np.bincount(times)/len(times)\n  times_by_frequency = np.append(times_by_frequency,[0.0]*(35-len(times_by_frequency)))\n  times_by_frequency = np.reshape(times_by_frequency,(35,1))\n  min_prob, max_prob = np.min(times_by_frequency),np.max(times_by_frequency)\n  axs[place_n].plot(time_axis, times_by_frequency)\n  axs[place_n].set_xlim([0, 34])\n  axs[place_n].set_ylim([0.0, max_prob+0.01])\n  axs[place_n].set_xticks(np.arange(0, 34, 1))\n  axs[place_n].grid(True)\n  title = 'distribution of time for place: '+str(place_id)\n  axs[place_n].set_title(title)\n  axs[place_n].set_xlabel('time of day, day of week')\n  place_n += 1\n\n##plt.setp([a.get_yticklabels() for a in axarr[:, 0]], visible=False)\nfig.tight_layout()\nplt.show()"
 },
 {
  "cell_type": "markdown",
  "metadata": {},
  "source": "\nThese graphs try to show that these places are mostly related to one specific time of the day or even mostly related to one day of the week. \nSome places are mostly for late nights and some places are for breakfast. \n0 to 34 show 5 different times (0 for morning, 1 for noon, 2 for afternoon, 3 for evening, 4 for late-night) in 7 days of the week (0 for Monday). \nFor example, 12 means noon time of Wednesday.\n"
 }
],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"}}, "nbformat": 4, "nbformat_minor": 0}