{ "cells": [ { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "from IPython.display import display\n", "from IPython.display import HTML\n", "import IPython.core.display as di # Example: di.display_html('

%s:

' % str, raw=True)\n", "\n", "# This line will hide code by default when the notebook is exported as HTML\n", "di.display_html('', raw=True)\n", "\n", "# This line will add a button to toggle visibility of code blocks, for use with the HTML export version\n", "di.display_html('''''', raw=True)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "import warnings\n", "warnings.filterwarnings('ignore')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Interactive Heat Map of Rat Sightings in NYC" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This is the heat map described in my Medium article on NYC's rat issue: https://towardsdatascience.com/rat-city-visualizing-new-york-citys-rat-problem-f7aabd6900b2. \n", "\n", "To check out your neighborhood, simply use the **+ / -** buttons to zoom in and out and use your mouse to move the map by clicking and dragging." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "#importing packages\n", "%matplotlib inline\n", "import scipy.stats as stats\n", "import numpy as np\n", "import pandas as pd\n", "import matplotlib.pyplot as plt\n", "import seaborn as sns\n", "import folium\n", "from folium import plugins\n", "plt.style.use('seaborn')" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "#importing data set\n", "df = pd.read_csv('Rat_Sightings.csv')" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "#removing all rows with NaN's in Longitude or Latitude column\n", "df = df[np.isfinite(df['Latitude'])]" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "df = df[np.isfinite(df['Longitude'])]" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "#converting date column to datetime format\n", "df['Created Date'] = pd.to_datetime(df['Created Date'])" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "#splitting the data up into separate years\n", "tten = df[df['Created Date'].dt.year == 2010]\n", "televen = df[df['Created Date'].dt.year == 2011]\n", "ttwelve = df[df['Created Date'].dt.year == 2012]\n", "tthirteen = df[df['Created Date'].dt.year == 2013]\n", "tfourteen = df[df['Created Date'].dt.year == 2014]\n", "tfifteen = df[df['Created Date'].dt.year == 2015]\n", "tsixteen = df[df['Created Date'].dt.year == 2016]\n", "tseventeen = df[df['Created Date'].dt.year == 2017]" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "#generating folium map\n", "m = folium.Map([40.7, -73.9], zoom_start=11)" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# convert to (n, 2) nd-array format for heatmap\n", "rats = tseventeen[['Latitude', 'Longitude']].as_matrix()\n", "\n", "# plot heatmap\n", "m.add_children(plugins.HeatMap(rats, radius=15))\n", "m" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "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.5" } }, "nbformat": 4, "nbformat_minor": 2 }