Skip to content

Instantly share code, notes, and snippets.

@mulloymorrow
Created November 30, 2015 06:29
Show Gist options
  • Save mulloymorrow/2df06214ce97300e6d05 to your computer and use it in GitHub Desktop.
Save mulloymorrow/2df06214ce97300e6d05 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Seattle 2014 Crime Reports - Hour Analysis"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We're looking at the [crime data collected in 2014 in Seattle, WA](https://github.com/uwescience/datasci_course_materials/blob/master/assignment6/seattle_incidents_summer_2014.csv?raw=true). \n",
"An interesting visualization might be to plot crimes by hour. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import datetime\n",
"import pandas as pd\n",
"import matplotlib.pyplot as plt\n",
"\n",
"webpath = \"https://raw.githubusercontent.com/uwescience/datasci_course_materials/master/assignment6/\"\n",
"dtype={'Date Reported': datetime, 'Occurred Date or Date Range Start': datetime,'Occurred Date Range End': datetime}\n",
"c = pd.read_csv(webpath+'seattle_incidents_summer_2014.csv', index_col=0, parse_dates=True, low_memory=False )\n",
"c[\"Occurred Date or Date Range Start\"] = pd.to_datetime(c[\"Occurred Date or Date Range Start\"])\n",
"c.set_index(\"Occurred Date or Date Range Start\", inplace=True)\n",
"c['h'] = c.index.hour\n",
" \n",
"fig = plt.figure()\n",
"ax = plt.subplot(111)\n",
"\n",
"grp_agg = c.groupby('h').size()\n",
"grp_agg.plot(x='h',label='count').set_title('Crime Rate Hours')\n",
"\n",
"ax.set_xlabel(\"hour of day\")\n",
"ax.set_ylabel(\"count\")\n",
"plt.legend(loc='best',bbox_to_anchor=(1.1, 1.05))\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"First conclusion we can draw is that the probability of crime occuring in Seattle in 2014 was lowest at 5am. Are criminals nocturnal? By breaking down this plot into crime types, can we infer anymore from the data? Below, we see that Car Prowl's are common in Seattle, WA and are most frequent in the late evening and early mornings. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"fig = plt.figure()\n",
"ax = plt.subplot(111)\n",
"\n",
"for key, grp in c.groupby('Summarized Offense Description'):\n",
" grp_agg = grp.groupby('h').size()\n",
" grp_agg.plot(x='h',label='{k}'.format(k=key)).set_title('Crime Rate Hours')\n",
"\n",
"ax.set_xlabel(\"hour of day\")\n",
"ax.set_ylabel(\"count\")\n",
"plt.legend(loc='best',bbox_to_anchor=(1.1, 1.05))\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Moreover, Car Prowls peak between the hours of 8pm and 1am (the ones reported, at least. See below). "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"fig = plt.figure()\n",
"ax = plt.subplot(111)\n",
"\n",
"for key, grp in c.groupby('Summarized Offense Description'):\n",
" if 'car prowl' in key.lower():\n",
" grp_agg = grp.groupby('h').size()\n",
" grp_agg.plot(x='h',label='{k}'.format(k=key)).set_title('Crime Rate Hours')\n",
"\n",
"ax.set_xlabel(\"hour of day\")\n",
"ax.set_ylabel(\"count\")\n",
"plt.legend(loc='best',bbox_to_anchor=(1.1, 1.05))\n",
"plt.show()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2.0
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.10"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment