{"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# 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#Feature weights taken from Sandro forum post.\nfrom __future__ import print_function\nfrom subprocess import check_output\nprint(check_output([\"ls\", \"../input\"]).decode(\"utf8\"))\n\n# -*- coding: utf-8 -*-\n\"\"\"\nCreated on Fri Jun 03 21:00:28 2016\n@author: hardy_000\n\"\"\"\n\nimport matplotlib.pyplot as plt\nimport pandas as pd\nimport numpy as np\nimport matplotlib as mpl\nfrom mpl_toolkits.mplot3d import Axes3D\nfrom sklearn import neighbors, datasets\nfrom sklearn.neighbors import NearestNeighbors\nfrom sklearn import cross_validation\nfrom sklearn.metrics import pairwise\nfrom scipy.spatial import distance\nimport scipy as sp\nimport sklearn as skl\nfrom collections import Counter\nimport logging\n\n\n\nfw = [500, 1000, 4, 3, 2, 10] \n\ndef loadtraindata():\n    z= pd.read_csv(\"../input/train.csv\")\n    return z\n    \ndef loadtestdata():\n    z= pd.read_csv(\"../input/test.csv\")\ndef prepareData(dat):\n    dat['accuracy']=normalize(train['accuracy'])\n    dat=convertTime(dat)\n    return dat\n    return z\ndef discrete_cmap(N, base_cmap=None):\n    \"\"\"Create an N-bin discrete colormap from the specified input map\"\"\"\n    # By Jake VanderPlas\n    # License: BSD-style\n\n    base = plt.cm.get_cmap(base_cmap)\n    color_list = base(np.linspace(0, 1, N))\n    cmap_name = base.name + str(N)\n    return base.from_list(cmap_name, color_list, N)\ndef getScore(a,b):\n    res=a==b\n    return sum(res)/res.size\ndef convertTime(df):\n    df['x']=df['x']*fw[0]\n    df['y']=df['y']*fw[1]\n    \n    initial_date = np.datetime64('2014-01-01T01:01', dtype='datetime64[m]')\n    d_times = pd.DatetimeIndex(initial_date + np.timedelta64(int(mn), 'm') \n                               for mn in df.time.values)    \n    df['hour'] = (d_times.hour+ d_times.minute/60) * fw[2]\n    df['month'] = d_times.month * fw[4]\n    df['year'] = (d_times.year - 2013) * fw[5]\n\n    df = df.drop(['time'], axis=1) \n    return df\n    \ndef getSquare(data,i,j,buff):\n    data=data[((((j-1-buff)<=data['y']) & (data['y']<=j+buff))&(((i-1-buff)<=data['x']) & (data['x']<=i+buff)))]\n    \n    return data\ndef removeSquare(data,i,j):\n    data=data[~((((j-1)<=data['y']) & (data['y']<=j))&(((i-1)<=data['x']) & (data['x']<=i)))]\n\n    \n\n    return data\ndef getPercentError(guess,label):\n    correct=(guess==label)\n    return (1./correct.shape[0])*sum(correct)\ndef plot3DScatter(data):\n\n    fig = plt.figure()\n    ax = fig.add_subplot(111, projection='3d')\n    plt.xlabel('x')\n    plt.ylabel('y')\n    ax.set_zlabel('time')\n    ax.scatter(normalize(data.x),normalize(data.y),normalize(data.time),c=data.place_id,marker='o',depthshade=False,lw = 0)\n    plt.show()\n#places=train.groupby('place_id')\n#meanplaces=places.mean()\n#meanplaces=meanplaces.reset_index(drop=False)\n#plot a few locations means\n#meanplaces[:100].plot(kind='scatter',x='x',y='y',c='place_id',colormap='RdYlGn')\n\ndef getSamplePlaces(data,numberOfPlaces=1):\n        \n    return data[data.place_id.isin((data.place_id).unique()[1:numberOfPlaces+1])]\ndef getSamplePlace(numberOfPlaces=1):\n        \n    return train[train.place_id==(train.place_id).unique()[numberOfPlaces]]\n\ndef normalize(series):\n    return (series-min(series))/(max(series)-min(series))    \n    \ndef myf(a,w):\n    lookupTable, indexed_dataSet = np.unique(a, return_inverse=True)\n    \n    y= np.bincount(indexed_dataSet,w)\n    lookupTable[y.argsort()]\n    res=(lookupTable[y.argsort()][::-1][:3])\n    ret=np.empty((3))\n    ret.fill(res[-1])\n    ret[0:res.shape[0]]=res\n    return ret\ndef trainData(X,y):\n    X_train=X\n    y_train=y\n\n    #clf = neighbors.KNeighborsClassifier(25)\n    clf = neighbors.KNeighborsClassifier(n_neighbors=25, weights='distance', \n                               metric='manhattan')\n    ft=clf.fit(X_train, y_train)\n    return ft\n    \n    \n\ndef predictTest(test,clf,y_train,X_train,X_train_acc):\n    distances,indices=clf.kneighbors(test)\n    knearest_locs=np.array(X_train)[indices]\n    knearest_labels=np.array(y_train)[indices]\n    knearest_acc=np.array(X_train_acc)[indices]\n    points=np.array(test)\n    reppoints=np.tile(points,(25,1,1))\n    reppoints=reppoints.transpose((1,0,2))\n\n    ty=knearest_locs-reppoints\n    tys=np.square(ty)\n    tyssum=np.sum(tys,axis=2)\n    tyssums=np.sqrt(tyssum)\n    tyssumsac=knearest_acc/tyssums\n\n\n    \n    result = np.empty_like(knearest_labels[:,0:3])\n    for i,(x,y) in enumerate(zip(knearest_labels,tyssumsac)):\n        result[i] = myf(x,y)\n    return result\n\ndef predictRegion(train,test):\n\n\n\n\n\n\n    y_train=train.place_id\n    X_train=train.drop('place_id',1)\n    X_train_acc=X_train.accuracy\n    X_train=X_train.drop('accuracy',1)\n    X_train=X_train.drop('row_id',1)\n    ft=trainData(X_train,y_train)\n    \n\n\n    X_test=test.drop('accuracy',1)\n    row_id=X_test['row_id']\n    X_test=X_test.drop('row_id',1)\n\n\n\n    pred=predictTest(X_test,ft,y_train,X_train,X_train_acc)\n    result=np.insert(pred,0,row_id,axis=1)\n    return result\n    \nnumberOfPlaces=8\n\ntrain=loadtraindata()\n#test=loadtestdata()\n#test=prepareData(test)\ntrain=prepareData(train)\n\ntrain, test = cross_validation.train_test_split(train, test_size=0.4, random_state=0)\n\npred= np.empty((0,4), int)\n#iterate over grid\nlogging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')\nlogging.debug('This is a log message.')\nfor i in range(1,2):\n    for j in range(1,2):\n        temp=getSquare(test,i,j,0)\n        lab=temp.place_id\n        temp=temp.drop('place_id',axis=1)\n        res=predictRegion(getSquare(train,i,j,0.1),temp)\n        test=removeSquare(test,i,j)\n        \n        pred=np.append(pred,res,0)\n        p=getPercentError(pred,places)\n        \n        log = open(\"goat.txt\", \"w\")\n        logging.debug(p)\n        \n        \n\nprint(\"blah\")\n#pred=pred[np.argsort(pred[:,0])]\n#re=pd.DataFrame(pred)\n#x=re[1].astype(str)+\" \"+re[2].astype(str)+\" \"+re[3].astype(str)\n#re=pd.DataFrame(np.concatenate((pred[:,0].reshape(-1,1),x.reshape(-1,1)),1))\n#re.columns=[\"row_id\",\"place_id\"]\n#re.to_csv(\"C:\\\\Users\\\\hardy_000\\\\fbcomp\\\\submission.csv\",index=False)\n"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "pred\n"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": ""
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "train=loadtraindata()\n\ntrain=prepareData(train)\n\ntrain, test = cross_validation.train_test_split(train, test_size=0.4, random_state=0)\n#res=predictRegion(getSquare(train,1,1,0),getSquare(test,1,1,0))"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "train.columns"
 },
 {
  "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}