#!/usr/bin/env python # coding: utf-8 # In[10]: from IPython.display import display from IPython.display import HTML import IPython.core.display as di # Example: di.display_html('

%s:

' % str, raw=True) # This line will hide code by default when the notebook is exported as HTML di.display_html('', raw=True) # This line will add a button to toggle visibility of code blocks, for use with the HTML export version di.display_html('''''', raw=True) # In[11]: import warnings warnings.filterwarnings('ignore') # # Interactive Heat Map of Rat Sightings in NYC # 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. # # 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. # In[1]: #importing packages get_ipython().run_line_magic('matplotlib', 'inline') import scipy.stats as stats import numpy as np import pandas as pd import matplotlib.pyplot as plt import seaborn as sns import folium from folium import plugins plt.style.use('seaborn') # In[2]: #importing data set df = pd.read_csv('Rat_Sightings.csv') # In[3]: #removing all rows with NaN's in Longitude or Latitude column df = df[np.isfinite(df['Latitude'])] # In[4]: df = df[np.isfinite(df['Longitude'])] # In[6]: #converting date column to datetime format df['Created Date'] = pd.to_datetime(df['Created Date']) # In[7]: #splitting the data up into separate years tten = df[df['Created Date'].dt.year == 2010] televen = df[df['Created Date'].dt.year == 2011] ttwelve = df[df['Created Date'].dt.year == 2012] tthirteen = df[df['Created Date'].dt.year == 2013] tfourteen = df[df['Created Date'].dt.year == 2014] tfifteen = df[df['Created Date'].dt.year == 2015] tsixteen = df[df['Created Date'].dt.year == 2016] tseventeen = df[df['Created Date'].dt.year == 2017] # In[12]: #generating folium map m = folium.Map([40.7, -73.9], zoom_start=11) # In[13]: # convert to (n, 2) nd-array format for heatmap rats = tseventeen[['Latitude', 'Longitude']].as_matrix() # plot heatmap m.add_children(plugins.HeatMap(rats, radius=15)) m # In[ ]: