{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "f2d63c13-97d6-f6cb-d7bd-0d6f3bae1603"
      },
      "outputs": [],
      "source": [
        "import numpy as np # linear algebra\n",
        "import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n",
        "import matplotlib.pyplot as plt\n",
        "import seaborn as sns\n",
        "\n",
        "df = pd.read_json('../input/train.json')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "3fe403db-3fa1-602b-d07f-af660a1b26c9"
      },
      "outputs": [],
      "source": [
        "df.loc[df['bathrooms'] <= 0,'bathrooms'] = 1\n",
        "df.loc[df['bedrooms'] <= 0,'bedrooms'] = 1\n",
        "\n",
        "df.loc[df['bathrooms'] > 3,'bathrooms'] = 3\n",
        "df.loc[df['bedrooms'] > 3,'bedrooms'] = 3\n",
        "\n",
        "df['bedrooms_bathrooms'] = df['bedrooms'] * 10.0 + df['bathrooms']\n",
        "\n",
        "df = df[df['price'] < 10000]\n",
        "\n",
        "trans = {\n",
        "    'low':'red',\n",
        "    'medium': 'green',\n",
        "    'high':'blue'\n",
        "}\n",
        "colors = [trans[x.interest_level] for i,x in df.iterrows()]\n",
        "\n",
        "plt.scatter(df.price, df.bedrooms_bathrooms, c=colors)\n",
        "\n",
        "#plt.yticks(np.arange(0,40,1))\n",
        "plt.yticks([11,12,13,14,15,21,22,23,24,31,32,33,34])\n",
        "plt.show()\n",
        "#This new transformation allows combine the bedrooms and bathrooms. \n",
        "#For example 32(y axis) means 3 bedrooms with 2 bathrooms. \n",
        "#In fact this new feature improve the score in the LB, a tiny improvment."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "ec09d234-0aaf-41e8-c959-28f2970aeb30"
      },
      "source": [
        "Here you can see a interesting relation between the price and the numbers of bedrooms and bathrooms. At least for me, this is very important when I see for an apartment."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "557bf071-888a-880e-75f2-d2f3a1ae27be"
      },
      "outputs": [],
      "source": [
        "high_df = df[df[\"interest_level\"] == \"high\"]\n",
        "plt.scatter(high_df.price, high_df.bedrooms_bathrooms, c=\"blue\")\n",
        "\n",
        "#plt.yticks(np.arange(0,40,1))\n",
        "plt.yticks([11,12,13,14,15,21,22,23,24,31,32,33,34])\n",
        "plt.show()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "ae8e650a-1161-6ace-8db9-e8c79038f460"
      },
      "outputs": [],
      "source": [
        "medium_df = df[df[\"interest_level\"] == \"medium\"]\n",
        "plt.scatter(medium_df.price, medium_df.bedrooms_bathrooms, c=\"green\")\n",
        "\n",
        "#plt.yticks(np.arange(0,40,1))\n",
        "plt.yticks([11,12,13,14,15,21,22,23,24,31,32,33,34])\n",
        "plt.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "4d864e67-3245-3c2f-b29f-b7b689848a3c"
      },
      "source": [
        "Let's draw the points in the map"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "30a5512e-43fe-ad3e-afbd-8cc90d941601"
      },
      "outputs": [],
      "source": [
        "from bokeh.plotting import figure,show\n",
        "from bokeh.io import output_file, push_notebook, output_notebook\n",
        "from bokeh.models import (\n",
        "    GMapPlot, GMapOptions, ColumnDataSource, Circle, DataRange1d, PanTool, WheelZoomTool, BoxSelectTool\n",
        ")\n",
        "output_notebook()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "745e19f2-910f-ae6e-0156-50b6754a1f55"
      },
      "source": [
        "Extract the Google Maps key from\n",
        " https://developers.google.com/maps/documentation/javascript/get-api-key\n",
        "and use it in the next block"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "f97610ec-c64a-42b5-0828-24b3196f46cd"
      },
      "outputs": [],
      "source": [
        "map_options = GMapOptions(lat=40.75, lng=-74.00, map_type=\"roadmap\", zoom=11)\n",
        "\n",
        "plot = GMapPlot(\n",
        "    x_range=DataRange1d(), y_range=DataRange1d(), map_options=map_options\n",
        ")\n",
        "plot.title.text = \"New York\"\n",
        "\n",
        "plot.api_key = \"put your key\" #YOUR KEY GOES HERE\n",
        "\n",
        "#high\n",
        "sourceHigh = ColumnDataSource(\n",
        "    data=dict(\n",
        "        lat=df[df['interest_level'] == 'high'].latitude,\n",
        "        lon=df[df['interest_level'] == 'high'].longitude,\n",
        "    )\n",
        ")\n",
        "circleHigh = Circle(x=\"lon\", y=\"lat\", size=2, fill_color=\"#000099\", fill_alpha=0.8, line_color=None)\n",
        "# medium\n",
        "sourceMedium = ColumnDataSource(\n",
        "    data=dict(\n",
        "        lat=df[df['interest_level'] == 'medium'].latitude,\n",
        "        lon=df[df['interest_level'] == 'medium'].longitude,\n",
        "    )\n",
        ")\n",
        "circleMedium = Circle(x=\"lon\", y=\"lat\", size=2, fill_color=\"yellow\", fill_alpha=0.8, line_color=None)\n",
        "# low\n",
        "sourceLow = ColumnDataSource(\n",
        "    data=dict(\n",
        "        lat=df[df['interest_level'] == 'low'].latitude,\n",
        "        lon=df[df['interest_level'] == 'low'].longitude,\n",
        "    )\n",
        ")\n",
        "circleLow = Circle(x=\"lon\", y=\"lat\", size=2, fill_color=\"red\", fill_alpha=0.8, line_color=None)\n",
        "\n",
        "plot.add_glyph(sourceHigh, circleHigh)\n",
        "#plot.add_glyph(sourceMedium, circleMedium)\n",
        "#plot.add_glyph(sourceLow, circleLow)\n",
        "\n",
        "plot.add_tools(PanTool(), WheelZoomTool(), BoxSelectTool())\n",
        "show(plot,  notebook_handle=True)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "e9db404c-a2d0-fddb-fd5f-bacc5ce55654"
      },
      "source": [
        "Let's see something else. "
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "4c58d181-72c5-2053-d488-e8294fbced4f"
      },
      "outputs": [],
      "source": [
        "pd.set_option('display.float_format', lambda x: '%.3f' % x)\n",
        "print(df[df['interest_level']=='high'].price.describe())\n",
        "print(df[df['interest_level']=='medium'].price.describe())\n",
        "print(df[df['interest_level']=='low'].price.describe())"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "d0051256-b861-9531-a5b4-71265d9b9ef5"
      },
      "source": [
        "With this two data, the map and the describe information one can figure out that the high interest points are those close tho the center of Manhattan and cost around 2600 dollars, something that one believes it would be.\n",
        "Also we could see more insights if the data is split in more parts. It means, analyze the information by size of the apartment(rooms and bedrooms). "
      ]
    }
  ],
  "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.6.0"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}