{"cells":[
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "#this is the as is Pandas version of the Mad Scripts Battle by ZFTurbo:\n#https://www.kaggle.com/zfturbo/facebook-v-predicting-check-ins/mad-scripts-battle/code\n#further improvements coming\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Any results you write to the current directory are saved as output."
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "trainDF = pd.read_csv('../input/train.csv')\ntestDF = pd.read_csv('../input/test.csv')"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "trainDF['x'] = np.floor(trainDF['x'].values * 500.0/10.0)\ntrainDF['y'] = np.floor(trainDF['y'].values * 1000.0/10.0)\ntrainDF['time'] = np.floor(((trainDF['time'].astype(float)+180.0)/360.0) % 4)\n\ntestDF['x'] = np.floor(testDF['x'].values * 500.0/10.0)\ntestDF['y'] = np.floor(testDF['y'].values * 1000.0/10.0)\ntestDF['time'] = np.floor(((testDF['time'].astype(float)+180.0)/360.0) % 4)"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "trainDF['identifier'] = trainDF['x'].astype(str) + ' ' + trainDF['y'].astype(str) + ' ' + trainDF['time'].astype(str) \ntestDF['identifier']  = testDF['x'].astype(str)  + ' ' + testDF['y'].astype(str)  + ' ' + testDF['time'].astype(str) "
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "from collections import Counter\ndef threeMostCommon(listt):\n    return (' ').join([str(val[0]) for val in Counter(listt).most_common(3)])"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "pivotedTrain = trainDF.pivot_table('place_id','identifier', aggfunc = {'place_id':threeMostCommon}).reset_index()\n"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "out = pd.merge(testDF[['row_id','identifier']], pivotedTrain, how = 'left')\nout.sort('row_id', inplace = True)\n\nout.drop(['identifier'],axis = 1).fillna(0).to_csv('output.csv',index = False)"
 },
 {
  "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}