<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 CodeForMiami
Monday, June 29, 2015
LAB Miami
The goals for this hack are
Step1:  Fetch open data
Next, we need to import the data.  I accessed the 311 open data at Miami-Dade County’s Open Data Portal.
I saved it as a .csv file.
Step2:  It has almost 400,000 rows!
Step3:  Yuck, that's a scatter plot.  Let's make a 2D (Hess) diagram.
Step4:  That looks better.  The red-dashed line is the 1
<ASSISTANT_TASK:>
Python Code:
%matplotlib inline
import pandas as pd
import seaborn as sns
from scipy import stats
import numpy as np
import matplotlib.pyplot as plt # side-stepping mpl backend
import warnings
warnings.filterwarnings("ignore") #YOLO
import seaborn as sns
sns.set_context('notebook', font_scale=1.5)
Explanation: CodeForMiami
Monday, June 29, 2015
LAB Miami
The goals for this hack are:
- Explore open data
- Fetch open data
- Data munge/clean
- Make a plot
- (optionally) Make a blog post
First, we will import all the modules we will need.
I especially like pandas and seaborn.
End of explanation
!ls data/
dat = pd.read_csv('data/311_Service_Requests_-_Miami-Dade_County.csv')
Explanation: Fetch open data
Next, we need to import the data.  I accessed the 311 open data at Miami-Dade County’s Open Data Portal.
I saved it as a .csv file.
End of explanation
len(dat.City)
dat.head()
dat.columns
plt.plot(dat['Goal Days'], dat['Actual Completed Days'], '.', alpha=0.1)
plt.xlim(0, 30)
plt.ylim(0, 30)
plt.title('Does 311 meet its goals?')
plt.xlabel('Goal (Days)')
plt.ylabel('Actual (Days)')
Explanation: It has almost 400,000 rows!
End of explanation
H, xbins, ybins = np.histogram2d(dat['Goal Days'], dat['Actual Completed Days'],
                                 bins=(np.linspace(-60, 60, 120),
                                       np.linspace(-60, 60, 120)))
sns.set_style('dark')
# Create a black and white color map where bad data (NaNs) are white
cmap = plt.cm.bone
cmap.set_bad('w', 1.)
# Use the image display function imshow() to plot the result
fig, ax = plt.subplots(figsize=(7, 7))
H[H == 0] = 1  # prevent warnings in log10
ax.imshow(np.log10(H).T, origin='lower',
          extent=[xbins[0], xbins[-1], ybins[0], ybins[-1]],
          cmap=cmap, interpolation='nearest',
          aspect='auto')
ax.plot([0,60],[0, 60], 'r--')
plt.xlim(0,60)
plt.ylim(0,60)
plt.xlabel('Goal (days)')
plt.ylabel('Actual (days)')
Explanation: Yuck, that's a scatter plot.  Let's make a 2D (Hess) diagram.
End of explanation
dat.columns
diff = dat['Actual Completed Days'] - dat['Goal Days']
good_diff = diff[diff == diff] 
plt.hist(good_diff, range=(-100, 100), bins=100)
plt.yscale('linear')
plt.xlabel('Actual - Goal')
plt.ylabel('$N$')
n_good = np.sum(good_diff <= 0)
n_bad = np.sum(good_diff > 0)
n_tot = len(good_diff)
n_good, n_bad, n_tot
print "Miami 311 meets their goal {:.1f}% of the time.".format(n_good*100.0/n_tot)
Explanation: That looks better.  The red-dashed line is the 1:1 line.  Above this line, 311 did not meet its goal to complete the task, below this line 311 completed the task faster than its goal.  It sort of looks like most 311 tasks are completed below the 1:1 line.  But let's just compute it.
End of explanation
<END_TASK>