Rats in NYC

by lksfr


from IPython.display import display
from IPython.display import HTML
import IPython.core.display as di # Example: di.display_html('<h3>%s:</h3>' % str, raw=True)

# This line will hide code by default when the notebook is exported as HTML
di.display_html('<script>jQuery(function() {if (jQuery("body.notebook_app").length == 0) { jQuery(".input_area").toggle(); jQuery(".prompt").toggle();}});</script>', raw=True)

# This line will add a button to toggle visibility of code blocks, for use with the HTML export version
di.display_html('''<button onclick="jQuery('.input_area').toggle(); jQuery('.prompt').toggle();">Toggle code</button>''', raw=True)
import warnings

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.

#importing packages
%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
#importing data set
df = pd.read_csv('Rat_Sightings.csv')
#removing all rows with NaN's in Longitude or Latitude column
df = df[np.isfinite(df['Latitude'])]
df = df[np.isfinite(df['Longitude'])]
#converting date column to datetime format
df['Created Date'] = pd.to_datetime(df['Created Date'])
#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]
#generating folium map
m = folium.Map([40.7, -73.9], zoom_start=11)
# 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))