{ "cells": [ { "cell_type": "markdown", "id": "ba848798-fcfd-4d2d-96fd-7b1671342cbe", "metadata": { "tags": [] }, "source": [ "# Crimes in Chicago" ] }, { "cell_type": "markdown", "id": "b2f1793e-5ce8-42c3-a835-7c92cd9645f9", "metadata": {}, "source": [ "This work aims to uncover spatial and temporal crime patterns in Chicago. The study of spatial and temporal crime patterns is essential since it helps advance academic understanding of criminal activities and provides insight into criminal events, which benefits optimizing police presence and public safety. This work focuses on investigating and visualizing crime patterns in Chicago based on the unit of community. How do spatial crime patterns differ across crime types, and how do spatial crime patterns change across different time scales (e.g., day vs. night, month, year)? This notebook walks through the whole project from collecting data to generating results." ] }, { "cell_type": "code", "execution_count": 97, "id": "64d62e50-655a-40b0-9ac0-d83f3cafb03c", "metadata": {}, "outputs": [], "source": [ "# import required libraries\n", "%matplotlib inline\n", "import os\n", "import fiona\n", "\n", "import pprint\n", "import IPython\n", "from matplotlib import pyplot as plt\n", "\n", "import pandas as pd\n", "import geopandas as gpd\n", "import folium\n", "from folium.plugins import HeatMap, HeatMapWithTime" ] }, { "cell_type": "markdown", "id": "fc7c2c28-f8ba-4aff-8e39-9f59c7f2f33d", "metadata": { "tags": [] }, "source": [ "## File Structure\n", "```\n", "project/\n", "│ Project Crimes in Chicago.ipynb \n", "│\n", "└───data/\n", "│ │ Community.zip \n", "│ │ chicago_pop.csv\n", "│ │ rows.csv\n", "│ │\n", "└───result/\n", " │ ...\n", "\n", "```" ] }, { "cell_type": "markdown", "id": "249ce44b-0c88-4406-add2-1a5374ebee90", "metadata": {}, "source": [ "## Data Decription \n", "\n", "We need three data for this project: \n", "1. rows.csv (criminal events in Chicago): data provided by city of Chicago contains details on all Chicago criminal events since 2001, including date, time, crime types, district, community, location coordinates, and location description (e.g., store, apartment). \n", "2. Community.zip (community boundaries in the Chicago city): use this link download it and put it the folder: https://data.cityofchicago.org/api/geospatial/cauq-8yn6?method=export&format=Shapefile\n", "3. chicago_pop.csv (population data for each community in chicago): This contains population data for year 2000, 2010, 2020. I collect it myself, please contact weih9@illinois.edu if you need it." ] }, { "cell_type": "code", "execution_count": null, "id": "f701d809-a5d1-4e51-97ec-f04d4a87ad62", "metadata": {}, "outputs": [], "source": [ "# Run this line at the first time to get chicago crime data\n", "! wget -P ./data/ https://data.cityofchicago.org/api/views/ijzp-q8t2/rows.csv" ] }, { "cell_type": "code", "execution_count": 3, "id": "638ef5ca-3776-4f83-80a4-ebf0056715e1", "metadata": {}, "outputs": [], "source": [ "# Read data\n", "crimes = pd.read_csv('data/rows.csv')\n", "community_geo = gpd.read_file(os.path.join(r'data/Community.zip'))[['area_numbe', 'community', 'geometry']] \n", "community_pop = pd.read_csv('data/chicago_pop.csv').iloc[:,0:5]" ] }, { "cell_type": "code", "execution_count": 326, "id": "3282a6e6-0b69-48e5-8480-02d3882e4095", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
IDCase NumberDateBlockIUCRPrimary TypeDescriptionLocation DescriptionArrestDomestic...WardCommunity AreaFBI CodeX CoordinateY CoordinateYearUpdated OnLatitudeLongitudeLocation
010224738HY41164809/05/2015 01:30:00 PM043XX S WOOD ST0486BATTERYDOMESTIC BATTERY SIMPLERESIDENCEFalseTrue...12.061.008B1165074.01875917.0201502/10/2018 03:50:01 PM41.815117-87.6700(41.815117282, -87.669999562)
110224739HY41161509/04/2015 11:30:00 AM008XX N CENTRAL AVE0870THEFTPOCKET-PICKINGCTA BUSFalseFalse...29.025.0061138875.01904869.0201502/10/2018 03:50:01 PM41.895080-87.7654(41.895080471, -87.765400451)
211646166JC21352909/01/2018 12:01:00 AM082XX S INGLESIDE AVE0810THEFTOVER $500RESIDENCEFalseTrue...8.044.006NaNNaN201804/06/2019 04:04:43 PMNaNNaNNaN
\n", "

3 rows × 22 columns

\n", "
" ], "text/plain": [ " ID Case Number Date Block IUCR \\\n", "0 10224738 HY411648 09/05/2015 01:30:00 PM 043XX S WOOD ST 0486 \n", "1 10224739 HY411615 09/04/2015 11:30:00 AM 008XX N CENTRAL AVE 0870 \n", "2 11646166 JC213529 09/01/2018 12:01:00 AM 082XX S INGLESIDE AVE 0810 \n", "\n", " Primary Type Description Location Description Arrest \\\n", "0 BATTERY DOMESTIC BATTERY SIMPLE RESIDENCE False \n", "1 THEFT POCKET-PICKING CTA BUS False \n", "2 THEFT OVER $500 RESIDENCE False \n", "\n", " Domestic ... Ward Community Area FBI Code X Coordinate Y Coordinate \\\n", "0 True ... 12.0 61.0 08B 1165074.0 1875917.0 \n", "1 False ... 29.0 25.0 06 1138875.0 1904869.0 \n", "2 True ... 8.0 44.0 06 NaN NaN \n", "\n", " Year Updated On Latitude Longitude \\\n", "0 2015 02/10/2018 03:50:01 PM 41.815117 -87.6700 \n", "1 2015 02/10/2018 03:50:01 PM 41.895080 -87.7654 \n", "2 2018 04/06/2019 04:04:43 PM NaN NaN \n", "\n", " Location \n", "0 (41.815117282, -87.669999562) \n", "1 (41.895080471, -87.765400451) \n", "2 NaN \n", "\n", "[3 rows x 22 columns]" ] }, "execution_count": 326, "metadata": {}, "output_type": "execute_result" } ], "source": [ "crimes.head(3)" ] }, { "cell_type": "code", "execution_count": 327, "id": "e8f4619b-a148-4154-9a57-683692cc2299", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Community AreacommunitygeometryCommunity Pointcommunity_xcommunity_y
035DOUGLASPOLYGON ((-87.60914 41.84469, -87.60915 41.844...POINT (-87.61868 41.83512)-87.61867841.835118
136OAKLANDPOLYGON ((-87.59215 41.81693, -87.59231 41.816...POINT (-87.60322 41.82375)-87.60321641.823750
237FULLER PARKPOLYGON ((-87.62880 41.80189, -87.62879 41.801...POINT (-87.63242 41.80909)-87.63242541.809085
\n", "
" ], "text/plain": [ " Community Area community \\\n", "0 35 DOUGLAS \n", "1 36 OAKLAND \n", "2 37 FULLER PARK \n", "\n", " geometry \\\n", "0 POLYGON ((-87.60914 41.84469, -87.60915 41.844... \n", "1 POLYGON ((-87.59215 41.81693, -87.59231 41.816... \n", "2 POLYGON ((-87.62880 41.80189, -87.62879 41.801... \n", "\n", " Community Point community_x community_y \n", "0 POINT (-87.61868 41.83512) -87.618678 41.835118 \n", "1 POINT (-87.60322 41.82375) -87.603216 41.823750 \n", "2 POINT (-87.63242 41.80909) -87.632425 41.809085 " ] }, "execution_count": 327, "metadata": {}, "output_type": "execute_result" } ], "source": [ "community_geo.head(3)" ] }, { "cell_type": "code", "execution_count": 328, "id": "dec170be-3c78-411f-9fe5-81134c545bf0", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
nonamepop_2020pop_2010pop_2000
01Rogers Park556285499163484
12West Ridge771227194273199
23Uptown571825636263551
\n", "
" ], "text/plain": [ " no name pop_2020 pop_2010 pop_2000\n", "0 1 Rogers Park 55628 54991 63484\n", "1 2 West Ridge 77122 71942 73199\n", "2 3 Uptown 57182 56362 63551" ] }, "execution_count": 328, "metadata": {}, "output_type": "execute_result" } ], "source": [ "community_pop.head(3)" ] }, { "cell_type": "markdown", "id": "1e0865fd-7161-40dc-a060-29ecbfc871e1", "metadata": { "tags": [] }, "source": [ "## Data Cleaning and Pre-processing" ] }, { "cell_type": "code", "execution_count": 44, "id": "fa40673f-b5fa-4bd6-8977-b16d51e444cd", "metadata": {}, "outputs": [], "source": [ "def cleandata(dataset):\n", " \n", " # necessary information in the columns\n", " columns_tokeep = ['Date','Year','Primary Type','Community Area','X Coordinate',\n", " 'Y Coordinate','Latitude', 'Longitude']\n", " dataset = dataset[columns_tokeep]\n", " \n", " # columns who should not have nan values\n", " columns_dropnan = ['Date','Year','Primary Type','Community Area','X Coordinate',\n", " 'Y Coordinate','Latitude', 'Longitude']\n", " dataset = dataset.dropna(how='any')\n", " \n", " # change dtype of columns\n", " dataset['Date'] = pd.to_datetime(dataset['Date'])\n", " dataset['Community Area'] = dataset['Community Area'].astype(int)\n", " \n", " # time analysis\n", " # 6 - 18 - Day\n", " # 0 - 6; 18 - 0 - Night\n", " dataset['Month'] = dataset['Date'].dt.month\n", " dataset['DayNight'] = dataset['Date'].dt.hour // 6 \n", " dataset['DayNight'] = dataset['DayNight'].replace({0: 'Night', 1: 'Day',\n", " 2: 'Day', 3: 'Night'})\n", " \n", " # Primary Types re-catergory\n", " property_loss_list = ['THEFT','BURGLARY', 'MOTOR VEHICLE THEFT', 'DECEPTIVE PRACTICE']\n", " safety_list = ['BATTERY', 'WEAPONS VIOLATION', 'CRIMINAL DAMAGE', 'ASSAULT', \n", " 'ROBBERY', 'SEX OFFENSE', 'CRIM SEXUAL ASSAULT', 'ARSON',\n", " 'HOMICIDE', 'KIDNAPPING', 'CRIMINAL SEXUAL ASSAULT', 'INTIMIDATION',\n", " 'STALKING', 'CONCEALED CARRY LICENSE VIOLATION', 'PUBLIC INDECENCY',\n", " 'HUMAN TRAFFICKING', 'DOMESTIC VIOLENCE']\n", " others_list = ['NARCOTICS', 'OTHER OFFENSE', 'CRIMINAL TRESPASS', 'PROSTITUTION', \n", " 'OFFENSE INVOLVING CHILDREN', 'PUBLIC PEACE VIOLATION', \n", " 'INTERFERENCE WITH PUBLIC OFFICER', 'LIQUOR LAW VIOLATION', \n", " 'GAMBLING', 'OBSCENITY', 'NON-CRIMINAL', 'OTHER NARCOTIC VIOLATION',\n", " 'NON - CRIMINAL', 'NON - CRIMINAL', 'RITUALISM', 'NON-CRIMINAL (SUBJECT SPECIFIED)'] \n", " \n", " dataset = dataset.replace({'Primary Type': dict.fromkeys(property_loss_list, 'Property')})\n", " dataset = dataset.replace({'Primary Type': dict.fromkeys(safety_list, 'Safety')})\n", " dataset = dataset.replace({'Primary Type': dict.fromkeys(others_list, 'Others')})\n", "\n", " return dataset" ] }, { "cell_type": "code", "execution_count": 47, "id": "75a763a4-40ff-45d9-8da7-bb88cffbc944", "metadata": {}, "outputs": [], "source": [ "crimes_clean = cleandata(crimes)" ] }, { "cell_type": "code", "execution_count": 329, "id": "c9b4dbfc-de01-48c2-889e-b6969a9b9ac3", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
DateYearPrimary TypeCommunity AreaX CoordinateY CoordinateLatitudeLongitudeMonthDayNight
02015-09-05 13:30:002015Safety611165074.01875917.041.815117-87.670009Day
12015-09-04 11:30:002015Property251138875.01904869.041.895080-87.765409Day
32015-09-05 12:45:002015Others211152037.01920384.041.937406-87.716659Day
\n", "
" ], "text/plain": [ " Date Year Primary Type Community Area X Coordinate \\\n", "0 2015-09-05 13:30:00 2015 Safety 61 1165074.0 \n", "1 2015-09-04 11:30:00 2015 Property 25 1138875.0 \n", "3 2015-09-05 12:45:00 2015 Others 21 1152037.0 \n", "\n", " Y Coordinate Latitude Longitude Month DayNight \n", "0 1875917.0 41.815117 -87.67000 9 Day \n", "1 1904869.0 41.895080 -87.76540 9 Day \n", "3 1920384.0 41.937406 -87.71665 9 Day " ] }, "execution_count": 329, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 6882748 after clean\n", "crimes_clean.head(3)" ] }, { "cell_type": "code", "execution_count": 111, "id": "b64e1150-d3da-42b7-b22a-1f8db2377162", "metadata": {}, "outputs": [], "source": [ "crimes_count = crimes_clean.groupby(['Year','Month','DayNight','Primary Type','Community Area']).agg({'Date':'count'}).reset_index()\n", "crimes_count = crimes_count.rename(columns={'Date': 'Count'})" ] }, { "cell_type": "code", "execution_count": 112, "id": "75977377-77a0-44b5-81f2-b9b019282959", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
YearMonthDayNightPrimary TypeCommunity AreaCount
020011DayOthers41
120011DayOthers71
220011DayOthers81
320011DayOthers131
420011DayOthers151
\n", "
" ], "text/plain": [ " Year Month DayNight Primary Type Community Area Count\n", "0 2001 1 Day Others 4 1\n", "1 2001 1 Day Others 7 1\n", "2 2001 1 Day Others 8 1\n", "3 2001 1 Day Others 13 1\n", "4 2001 1 Day Others 15 1" ] }, "execution_count": 112, "metadata": {}, "output_type": "execute_result" } ], "source": [ "crimes_count.head()" ] }, { "cell_type": "code", "execution_count": null, "id": "2b51690a-9f6a-410a-9c04-31f868b73953", "metadata": {}, "outputs": [], "source": [ "# Here, just want a point represent for each community, thus I calculate on\n", "# geographic CRS directly, a right way should re-project geometries to a \n", "# projected CRS before this operation \n", "community_geo['Community Point'] = community_geo.centroid\n", "community_geo['community_x'] = community_geo['Community Point'].x\n", "community_geo['community_y'] = community_geo['Community Point'].y\n", "community_geo = community_geo.rename(columns={'area_numbe': 'Community Area'})\n", "community_geo['Community Area'] = community_geo['Community Area'].astype(int)" ] }, { "cell_type": "code", "execution_count": 330, "id": "31e1edeb-c863-4878-ba65-c9d5850e798a", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Community AreacommunitygeometryCommunity Pointcommunity_xcommunity_y
035DOUGLASPOLYGON ((-87.60914 41.84469, -87.60915 41.844...POINT (-87.61868 41.83512)-87.61867841.835118
136OAKLANDPOLYGON ((-87.59215 41.81693, -87.59231 41.816...POINT (-87.60322 41.82375)-87.60321641.823750
237FULLER PARKPOLYGON ((-87.62880 41.80189, -87.62879 41.801...POINT (-87.63242 41.80909)-87.63242541.809085
\n", "
" ], "text/plain": [ " Community Area community \\\n", "0 35 DOUGLAS \n", "1 36 OAKLAND \n", "2 37 FULLER PARK \n", "\n", " geometry \\\n", "0 POLYGON ((-87.60914 41.84469, -87.60915 41.844... \n", "1 POLYGON ((-87.59215 41.81693, -87.59231 41.816... \n", "2 POLYGON ((-87.62880 41.80189, -87.62879 41.801... \n", "\n", " Community Point community_x community_y \n", "0 POINT (-87.61868 41.83512) -87.618678 41.835118 \n", "1 POINT (-87.60322 41.82375) -87.603216 41.823750 \n", "2 POINT (-87.63242 41.80909) -87.632425 41.809085 " ] }, "execution_count": 330, "metadata": {}, "output_type": "execute_result" } ], "source": [ "community_geo.head(3)" ] }, { "cell_type": "markdown", "id": "6b104b69-4677-4177-9a03-f69bd9cb2e69", "metadata": {}, "source": [ "## Crime Heat Map with Time" ] }, { "cell_type": "code", "execution_count": 294, "id": "cead4d12-d2bf-4e2d-b70e-c15db1c7b747", "metadata": {}, "outputs": [], "source": [ "def CrimesHeatMapWithTime(crimes_count, community_geo, bytime, dayornight = None, crimetype = None):\n", "\n", " if bytime not in ['Year', 'Month']:\n", " return 0\n", " if dayornight is not None:\n", " if dayornight == 'Day' or dayornight == 'Night':\n", " crimes_count = crimes_count.loc[crimes_count['DayNight'] == dayornight]\n", " else:\n", " return 0\n", " if crimetype is not None:\n", " if crimetype in ['Property', 'Safety', 'Others']:\n", " crimes_count = crimes_count.loc[crimes_count['Primary Type'] == crimetype]\n", " else:\n", " return 0\n", " \n", " crimes_count_bytime = crimes_count.groupby([bytime,'Community Area']).agg({'Count':'sum'}).reset_index()\n", " time_index = list(crimes_count_bytime[bytime].sort_values().astype('str').unique())\n", " crimes_count_bytime = crimes_count_bytime.merge(community_geo, on='Community Area')\n", " crimes_count_bytime = crimes_count_bytime.sort_values(by = [bytime,'Community Area'], ascending=True)\n", " \n", " data = []\n", " for _, d in crimes_count_bytime.groupby(bytime):\n", " data.append([[row['community_y'], row['community_x'], row['Count']] for _, row in d.iterrows()])\n", "\n", " heatmap_layer = HeatMapWithTime(data,\n", " index=time_index,\n", " auto_play=True,\n", " use_local_extrema=True\n", " )\n", "\n", " return heatmap_layer" ] }, { "cell_type": "markdown", "id": "68802e40-b27d-4a79-be1a-d3bd72d03940", "metadata": {}, "source": [ "### Crimes change by year" ] }, { "cell_type": "code", "execution_count": 315, "id": "49a8d236-0d2d-4827-ad21-fceac68a806b", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
Make this Notebook Trusted to load map: File -> Trust Notebook
" ], "text/plain": [ "" ] }, "execution_count": 315, "metadata": {}, "output_type": "execute_result" } ], "source": [ "heatmap = folium.Map(location=[41.87, -87.62], # Chicago\n", " tiles='stamentoner',#'cartodbpositron', stamentoner\n", " zoom_start=10.2,\n", " control_scale=True)\n", "# # Add communitiy boundry\n", "# folium.GeoJson(\n", "# data=community_geo['geometry'], \n", "# style_function = lambda x: {'fillOpacity' : 0, 'weight': 1, 'Opacity' : 0.1}\n", "# ).add_to(heatmap)\n", "\n", "CrimesHeatMapWithTime(crimes_count, community_geo, 'Year').add_to(heatmap)\n", "\n", "heatmap\n", "# heatmap.save('result/year.html')" ] }, { "cell_type": "markdown", "id": "4eaf6195-40a9-4f1b-8cff-6a08ebfc4d8c", "metadata": {}, "source": [ "### Crimes change by month" ] }, { "cell_type": "code", "execution_count": 316, "id": "3b25dd4b-3d90-4e12-a9be-36916667bbce", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
Make this Notebook Trusted to load map: File -> Trust Notebook
" ], "text/plain": [ "" ] }, "execution_count": 316, "metadata": {}, "output_type": "execute_result" } ], "source": [ "heatmap = folium.Map(location=[41.87, -87.62], # Chicago\n", " tiles='stamentoner',#'cartodbpositron', stamentoner\n", " zoom_start=10.2,\n", " control_scale=True)\n", "CrimesHeatMapWithTime(crimes_count, community_geo, 'Month').add_to(heatmap)\n", "# heatmap.save('result/month.html')\n", "heatmap" ] }, { "cell_type": "markdown", "id": "028f4a98-7609-4535-b07f-4073b95bc0b7", "metadata": {}, "source": [ "### Day v.s. Night Crimes change by year" ] }, { "cell_type": "code", "execution_count": 320, "id": "cf0b874d-424f-4479-a3a4-dffc37a7e9cc", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
Make this Notebook Trusted to load map: File -> Trust Notebook
" ], "text/plain": [ "" ] }, "execution_count": 320, "metadata": {}, "output_type": "execute_result" } ], "source": [ "daynightmap = folium.plugins.DualMap(location=[41.8, -87.62], tiles='stamentoner', zoom_start=10)\n", "\n", "CrimesHeatMapWithTime(crimes_count, community_geo, 'Year', dayornight='Day').add_to(daynightmap.m1)\n", "CrimesHeatMapWithTime(crimes_count, community_geo, 'Year', dayornight='Night').add_to(daynightmap.m2)\n", "# daynightmap.save('result/daynight.html')\n", "daynightmap" ] }, { "cell_type": "markdown", "id": "1ca6a657-1cb9-4f78-a61d-9114205fcb49", "metadata": {}, "source": [ "### Changes by year among crime types" ] }, { "cell_type": "code", "execution_count": 321, "id": "9d551016-a141-414a-b725-5cac1f498b83", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
Make this Notebook Trusted to load map: File -> Trust Notebook
" ], "text/plain": [ "" ] }, "execution_count": 321, "metadata": {}, "output_type": "execute_result" } ], "source": [ "crimetypemap = folium.plugins.DualMap(location=[41.8, -87.62], tiles='stamentoner', zoom_start=10)\n", "CrimesHeatMapWithTime(crimes_count, community_geo, 'Year', crimetype = 'Safety').add_to(crimetypemap.m1)\n", "CrimesHeatMapWithTime(crimes_count, community_geo, 'Year', crimetype = 'Property').add_to(crimetypemap.m2)\n", "# crimetypemap.save('result/crimetype.html')\n", "crimetypemap" ] }, { "cell_type": "markdown", "id": "5a328ed6-cc83-4a1a-9206-92ef7d9c0985", "metadata": {}, "source": [ "## Cirmes Considering Population" ] }, { "cell_type": "code", "execution_count": 304, "id": "12a7b48d-35b1-403e-a5f7-0e9a524df81b", "metadata": {}, "outputs": [], "source": [ "def find_pop(community_pop, row):\n", " index = (row['Year']-1996)//10 + 1\n", " pop = community_pop[community_pop['no'] == row['Community Area']].iloc[:,-index]\n", " return pop\n", "\n", "def CrimesDenseHeatMapWithTime(crimes_count, community_geo, bytime, dayornight = None, crimetype = None):\n", "\n", " if bytime not in ['Year', 'Month']:\n", " return 0\n", " if dayornight is not None:\n", " if dayornight == 'Day' or dayornight == 'Night':\n", " crimes_count = crimes_count.loc[crimes_count['DayNight'] == dayornight]\n", " else:\n", " return 0\n", " if crimetype is not None:\n", " if crimetype in ['Property', 'Safety', 'Others']:\n", " crimes_count = crimes_count.loc[crimes_count['Primary Type'] == crimetype]\n", " else:\n", " return 0\n", " \n", " crimes_count_bytime = crimes_count.groupby([bytime,'Community Area']).agg({'Count':'sum'}).reset_index()\n", " time_index = list(crimes_count_bytime[bytime].sort_values().astype('str').unique())\n", " crimes_count_bytime = crimes_count_bytime.merge(community_geo, on='Community Area')\n", " crimes_count_bytime = crimes_count_bytime.sort_values(by = [bytime,'Community Area'], ascending=True)\n", " # divided by population\n", " crimes_count_bytime['pop'] = crimes_count_bytime.apply(lambda row: find_pop(community_pop, row), axis=1).sum(axis=1)\n", " crimes_count_bytime['count_pop'] = crimes_count_bytime['Count']/crimes_count_bytime['pop']\n", " \n", " data = []\n", " for _, d in crimes_count_bytime.groupby(bytime):\n", " data.append([[row['community_y'], row['community_x'], row['count_pop']] for _, row in d.iterrows()])\n", "\n", " heatmap_layer = HeatMapWithTime(data,\n", " index=time_index,\n", " auto_play=True,\n", " use_local_extrema=True\n", " )\n", "\n", " return heatmap_layer" ] }, { "cell_type": "code", "execution_count": 322, "id": "3e758af4-031a-42ed-8c54-780d70f7584c", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
Make this Notebook Trusted to load map: File -> Trust Notebook
" ], "text/plain": [ "" ] }, "execution_count": 322, "metadata": {}, "output_type": "execute_result" } ], "source": [ "heatmap = folium.Map(location=[41.87, -87.62], # Chicago\n", " tiles='stamentoner',#'cartodbpositron', stamentoner\n", " zoom_start=10.2,\n", " control_scale=True)\n", "\n", "CrimesDenseHeatMapWithTime(crimes_count, community_geo, 'Year').add_to(heatmap)\n", "\n", "# heatmap.save('result/yearpop.html')\n", "heatmap" ] }, { "cell_type": "code", "execution_count": 323, "id": "6eb63531-09c4-43a5-8815-14001c32f0bf", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
Make this Notebook Trusted to load map: File -> Trust Notebook
" ], "text/plain": [ "" ] }, "execution_count": 323, "metadata": {}, "output_type": "execute_result" } ], "source": [ "popmap = folium.plugins.DualMap(location=[41.8, -87.62], tiles='stamentoner', zoom_start=10)\n", "\n", "CrimesHeatMapWithTime(crimes_count, community_geo, 'Year').add_to(popmap.m1)\n", "CrimesDenseHeatMapWithTime(crimes_count, community_geo, 'Year').add_to(popmap.m2)\n", "\n", "# popmap.save('result/yearpopcom.html')\n", "popmap" ] }, { "cell_type": "code", "execution_count": 324, "id": "361c8618-a2a6-4eaa-a224-05e6c88f7041", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
Make this Notebook Trusted to load map: File -> Trust Notebook
" ], "text/plain": [ "" ] }, "execution_count": 324, "metadata": {}, "output_type": "execute_result" } ], "source": [ "popcrimetypemap = folium.plugins.DualMap(location=[41.8, -87.62], tiles='stamentoner', zoom_start=10)\n", "CrimesDenseHeatMapWithTime(crimes_count, community_geo, 'Year', crimetype = 'Safety').add_to(popcrimetypemap.m1)\n", "CrimesDenseHeatMapWithTime(crimes_count, community_geo, 'Year', crimetype = 'Property').add_to(popcrimetypemap.m2)\n", "# popcrimetypemap.save('result/crimetypepop.html')\n", "popcrimetypemap" ] }, { "cell_type": "code", "execution_count": null, "id": "11b64599-2097-4faa-93b1-bc75c48fd842", "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.8.12" } }, "nbformat": 4, "nbformat_minor": 5 }