{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "9ac07922-be35-55c9-02b7-ca02b67376ae"
      },
      "outputs": [],
      "source": [
        "# Garima and Ghaida Notebook kaggle decal Fall 2016\n",
        "# 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",
        "import numpy as np # linear algebra\n",
        "import pandas as pd \n",
        "# data processing, CSV file I/O (e.g. pd.read_csv)\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",
        "\n",
        "from subprocess import check_output\n",
        "print(check_output([\"ls\", \"../input\"]).decode(\"utf8\"))\n",
        "\n",
        "# Any results you write to the current directory are saved as output.\n",
        "import os\n",
        "import matplotlib.pyplot as plt\n",
        "from scipy.stats import gaussian_kde\n",
        "import time\n",
        "import seaborn as sns \n",
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "7fe8d93d-29b5-b33b-2aea-5dc719177777"
      },
      "outputs": [],
      "source": [
        "df_train = pd.read_csv(\"../input/train.csv\")\n",
        "df_test = pd.read_csv(\"../input/test.csv\")\n",
        "df_train.head()\n",
        "\n",
        "df_train_sample = df_train.sample(n=150000)\n",
        "df_test_sample = df_test.sample(n=150000)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "70ce26c2-6f5d-d97b-b811-b0f70aabef33"
      },
      "outputs": [],
      "source": [
        "counts1, bins1 = np.histogram(df_train[\"accuracy\"], bins=50)\n",
        "binsc1 = bins1[:-1] + np.diff(bins1)/2.\n",
        "\n",
        "counts2, bins2 = np.histogram(df_test[\"accuracy\"], bins=50)\n",
        "binsc2 = bins2[:-1] + np.diff(bins2)/2.\n",
        "\n",
        "plt.figure(0, figsize=(14,4))\n",
        "\n",
        "plt.subplot(121)\n",
        "plt.bar(binsc1, counts1/(counts1.sum()*1.0), width=np.diff(bins1)[0])\n",
        "plt.grid(True)\n",
        "plt.xlabel(\"Accuracy\")\n",
        "plt.ylabel(\"Fraction\")\n",
        "plt.title(\"Train\")\n",
        "\n",
        "plt.subplot(122)\n",
        "plt.bar(binsc2, counts2/(counts2.sum()*1.0), width=np.diff(bins2)[0])\n",
        "plt.grid(True)\n",
        "plt.xlabel(\"Accuracy\")\n",
        "plt.ylabel(\"Fraction\")\n",
        "plt.title(\"Test\")\n",
        "\n",
        "plt.show()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "4e3dcff5-6fc1-083e-df8f-8f64a279e1f4"
      },
      "outputs": [],
      "source": [
        "import datetime\n",
        "from heapq import nlargest\n",
        "from operator import itemgetter\n",
        "import os\n",
        "import time\n",
        "import math\n",
        "from collections import defaultdict\n",
        "\n",
        "\n",
        "def prep_xy(x, y):\n",
        "    range = 800\n",
        "    ix = math.floor(range*x/10)\n",
        "    if ix < 0:\n",
        "        ix = 0\n",
        "    if ix >= range:\n",
        "        ix = range-1\n",
        "\n",
        "    iy = math.floor(range*y/10)\n",
        "    if iy < 0:\n",
        "        iy = 0\n",
        "    if iy >= range:\n",
        "        iy = range-1\n",
        "\n",
        "    return ix, iy\n",
        "\n",
        "\n",
        "def run_solution():\n",
        "    print('Preparing data...')\n",
        "    f = open(\"../input/train.csv\", \"r\")\n",
        "    f.readline()\n",
        "    total = 0\n",
        "\n",
        "    grid = defaultdict(lambda: defaultdict(int))\n",
        "    grid_sorted = dict()\n",
        "\n",
        "    # Calc counts\n",
        "    while 1:\n",
        "        line = f.readline().strip()\n",
        "        total += 1\n",
        "\n",
        "        if line == '':\n",
        "            break\n",
        "\n",
        "        arr = line.split(\",\")\n",
        "        row_id = arr[0]\n",
        "        x = float(arr[1])\n",
        "        y = float(arr[2])\n",
        "        accuracy = arr[3]\n",
        "        time = arr[4]\n",
        "        place_id = arr[5]\n",
        "\n",
        "        ix, iy = prep_xy(x, y)\n",
        "\n",
        "        grid[(ix, iy)][place_id] += 1\n",
        "\n",
        "    f.close()\n",
        "\n",
        "    # Sort array\n",
        "    for el in grid:\n",
        "        grid_sorted[el] = nlargest(3, sorted(grid[el].items()), key=itemgetter(1))\n",
        "\n",
        "    print('Generate submission...')\n",
        "    sub_file = os.path.join('submission_' + str(datetime.datetime.now().strftime(\"%Y-%m-%d-%H-%M\")) + '.csv')\n",
        "    out = open(sub_file, \"w\")\n",
        "    f = open(\"../input/test.csv\", \"r\")\n",
        "    f.readline()\n",
        "    total = 0\n",
        "    out.write(\"row_id,place_id\\n\")\n",
        "\n",
        "    while 1:\n",
        "        line = f.readline().strip()\n",
        "        total += 1\n",
        "\n",
        "        if line == '':\n",
        "            break\n",
        "\n",
        "        arr = line.split(\",\")\n",
        "        row_id = arr[0]\n",
        "        x = float(arr[1])\n",
        "        y = float(arr[2])\n",
        "        accuracy = arr[3]\n",
        "        time = arr[4]\n",
        "\n",
        "        out.write(str(row_id) + ',')\n",
        "        filled = []\n",
        "\n",
        "        ix, iy = prep_xy(x, y)\n",
        "\n",
        "        s1 = (ix, iy)\n",
        "        if s1 in grid_sorted:\n",
        "            topitems = grid_sorted[s1]\n",
        "            for i in range(len(topitems)):\n",
        "                if topitems[i][0] in filled:\n",
        "                    continue\n",
        "                if len(filled) == 3:\n",
        "                    break\n",
        "                out.write(' ' + topitems[i][0])\n",
        "                filled.append(topitems[i][0])\n",
        "\n",
        "        out.write(\"\\n\")\n",
        "\n",
        "    out.close()\n",
        "    f.close()\n",
        "\n",
        "\n",
        "start_time = time.time()\n",
        "run_solution()\n",
        "print(\"Elapsed time overall: %s seconds\" % (time.time() - start_time))\n"
      ]
    }
  ],
  "metadata": {
    "_change_revision": 0,
    "_is_fork": false,
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.5.2"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}