{"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\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.\n\ndef getfreq(df, tmax, nbins):\n\n        t = list(df.time)\n\n        t.append(tmax)\n\n        hist, edges = np.histogram(t,bins=nbins)\n\n        n = len(hist)\n\n        r = np.correlate(hist, hist,mode=\"full\")[-n:]\n\n        sp = np.fft.fft(r)\n        freq = np.fft.fftfreq(len(r))\n\n        return freq, sp.real\n\n\ndf = pd.read_csv(\"../input/train.csv\")\n\ntmax = max(df.time)\n\n\nnbins = int(tmax/60)+1\ntmax = nbins*60.0\n\n\npids =  list(set(df.place_id))\n\nnpids = len(pids)\n\ntsp = [0.0 for i in range(nbins)]\n\nfor pid in pids[:100]:\n\n        data = df[df.place_id==pid]\n        f, sp = getfreq(data, tmax, nbins)\n\n        tsp = tsp+sp/npids\n\nhr = [int(1.0/ff) for ff in f[1:nbins/2]]\ntsp = tsp[1:nbins/2]\n\n#############################################\n\nt = hr\n\nsp = tsp\n\nct = {}\n\nfor i, tt in enumerate(t):\n        if tt in ct.keys():\n                if sp[i]>ct[tt]:\n                        ct[tt] = sp[i]\n        else:\n                ct[tt] = sp[i]\nx = []\ny = []\nfor k in sorted(ct.keys()):\n        x.append(k)\n        y.append(ct[k])\nplt.xlim([0,1000])\nplt.plot(x, y)\n\npeaks = []\nfor i in range(len(y)):\n        if i>0 and i<len(y)-1:\n                if y[i]-y[i-1]>0 and y[i]-y[i+1]>0:\n                        if y[i]-y[i-1]>30 or y[i]-y[i+1]>30:\n                                peaks.append(x[i])\n\n\ntmp = []\n\nfor i, peak in enumerate(reversed(peaks)):\n\n        plt.plot([peak,peak],[5,10000],c=\"k\",ls=\"--\")\n\nplt.show()"
 }
],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"}}, "nbformat": 4, "nbformat_minor": 0}