{"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 pylab as plt\n\ndata = pd.read_csv(\"../input/train.csv\")\n\nN = 786240\nb = 420\n\nsignal = np.zeros(N)\nsignal_2 = np.zeros(N//b)\ndaily = np.zeros(10080)\nh = np.zeros(10080//60)\n\nfor i in data[\"time\"]:\n    signal[i] = signal[i] + 1\n    signal_2[i//b] = signal_2[i//b] + 1\n    daily[(i%10080)] = daily[(i%10080)] + 1\n    h[(i%10080)//60] = h[(i%10080)//60] + 1\n    \nplt.plot(signal)\nplt.show()"
 },
 {
  "cell_type": "markdown",
  "metadata": {},
  "source": "**The periodic structure is lost in the noise, so I averaged over 420 timestamps**"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "plt.plot(signal_2)\nplt.show()"
 },
 {
  "cell_type": "markdown",
  "metadata": {},
  "source": "**Zooming in the graph will reveal the period between peaks**"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "W    = np.fft.fft(signal)\nfreq = np.fft.fftfreq(N,1)\n\nplt.plot(1.0/freq[:N//2], abs(W[:N//2]))\nplt.xlim(0,20000)\nplt.ylim(0,500000)\n\nplt.show()"
 },
 {
  "cell_type": "markdown",
  "metadata": {},
  "source": "**The peak coresponds to 10080, the number of time units in a period, which turns out to be a week**"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "plt.plot(daily)\nplt.show()"
 },
 {
  "cell_type": "markdown",
  "metadata": {},
  "source": "**Average check-in histogram over a week**"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "plt.plot(h)\nplt.show()"
 },
 {
  "cell_type": "markdown",
  "metadata": {},
  "source": "**Even after averaging, the structure inside the week is lost when averaging over all places**"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "W    = np.fft.fft(h)\nfreq = np.fft.fftfreq(168,1)\n\nplt.plot(1.0/freq[:168//2], abs(W[:168//2]))\nplt.xlim(0,168)\n\nplt.show()"
 },
 {
  "cell_type": "markdown",
  "metadata": {},
  "source": "**FFT doesn't help finding a period**"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "    one_place = data.loc[data['place_id'] == 9129780742]\n    \n    h = np.zeros(10080//60)\n    for i in one_place[\"time\"]:\n        h[(i%10080//60)] = h[(i%10080//60)] + 1\n    \n    plt.plot(h)\n    plt.show()\n"
 },
 {
  "cell_type": "markdown",
  "metadata": {},
  "source": "**But when the plotting is done for each sepparate place, the 7 peaks in a week are easily visible** "
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "    one_place = data.loc[data['place_id'] == 1623394281]\n    \n    h = np.zeros(10080//60)\n    for i in one_place[\"time\"]:\n        h[(i%10080//60)] = h[(i%10080//60)] + 1\n    \n    plt.plot(h)\n    plt.show() "
 },
 {
  "cell_type": "markdown",
  "metadata": {},
  "source": "**A few more examples**"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "    one_place = data.loc[data['place_id'] == 1308450003]\n    \n    h = np.zeros(10080//60)\n    for i in one_place[\"time\"]:\n        h[(i%10080//60)] = h[(i%10080//60)] + 1\n    \n    plt.plot(h)\n    plt.show() "
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "    one_place = data.loc[data['place_id'] == 4371034975]\n    \n    h = np.zeros(10080//60)\n    for i in one_place[\"time\"]:\n        h[(i%10080//60)] = h[(i%10080//60)] + 1\n    \n    plt.plot(h)\n    plt.show() "
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "    one_place = data.loc[data['place_id'] == 7698408658]\n    \n    h = np.zeros(10080//60)\n    for i in one_place[\"time\"]:\n        h[(i%10080//60)] = h[(i%10080//60)] + 1\n    \n    plt.plot(h)\n    plt.show() "
 }
],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"}}, "nbformat": 4, "nbformat_minor": 0}