{"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)\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."
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "train = pd.read_csv('../input/train.csv')\ntest = pd.read_csv('../input/test.csv')"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "print(\"train min %d\" % train.time.min())\nprint(\"train max %d\" % train.time.max())\nprint(\"train %4.4f years\" % ((train.time.max() - train.time.min()) / (60 * 24 * 365)))"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "print(\"test min %d\" % test.time.min())\nprint(\"test max %d\" % test.time.max())\nprint(\"test %4.4f years\" % ((test.time.max() - test.time.min()) / (60 * 24 * 365)))"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "print(\"train x min %d\" % train.x.min())\nprint(\"train x max %d\" % train.x.max())\nprint(\"train y min %d\" % train.y.min())\nprint(\"train y max %d\" % train.y.max())"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "print(\"test x min %d\" % test.x.min())\nprint(\"test x max %d\" % test.x.max())\nprint(\"test y min %d\" % test.y.min())\nprint(\"test y max %d\" % test.y.max())"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "import math\ndef int_xy(x, range_x):\n    ix = math.floor(range_x * x / 10)\n    if ix < 0:\n        ix = 0\n    if ix >= range_x:\n        ix = range_x - 1\n    return ix"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "x_range = 1000\ny_range = 2000\ntrain[\"ix\"] = train[\"x\"].apply(lambda x: int_xy(x, x_range))\ntrain[\"iy\"] = train[\"y\"].apply(lambda x: int_xy(x, y_range))\nuni_mat = np.empty((x_range, y_range))\ntrain.groupby([\"ix\", \"iy\"])[\"place_id\"].nunique()"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "mat = train.groupby([\"ix\", \"iy\"])[\"place_id\"].nunique()"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "np.reshape(mat.values, (x_range, y_range))"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "mat.values.shape"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "place_mat = [[mat[\"ix] for j in range(y_range)] for i in range(x_range)]"
 },
 {
  "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}