-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathproject_7.html
372 lines (361 loc) · 18.6 KB
/
project_7.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://fonts.googleapis.com/css?family=Open+Sans:300,400,600,700,800&display=swap" rel="stylesheet">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.8.2/css/all.css"
integrity="sha384-oS3vJWv+0UjzBfQzYUhtDYW+Pj2yciDJxpsK1OYPAYjqT085Qq/1cq5FLXAZQ7Ay" crossorigin="anonymous">
<link href="https://unpkg.com/[email protected]/dist/aos.css" rel="stylesheet">
<link rel="stylesheet" href="./style.css">
<script src="https://cdn.jsdelivr.net/npm/vue"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]"></script>
<script src="https://cdn.jsdelivr.net/gh/google/code-prettify@master/loader/run_prettify.js"></script>
<script src="https://unpkg.com/[email protected]/dist/aos.js"></script>
<link rel="stylesheet" href="./code.css">
<title>Pascal Schlaak | Portfolio</title>
<link rel="icon" type="image/png" href="./imgs/icons/logo_small.svg">
</head>
<body>
<div id="app">
<navbar-normal></navbar-normal>
<navbar-small></navbar-small>
<hamburger></hamburger>
<section id="project">
<img src="./imgs/dots.png" id="dots">
<div class="container project-text">
<h1>Visualizing NY shooting incident data</h1>
<div class="flex-row">
<div class="column-65">
<p class="text">
After finishing my bachelors thesis I wanted to continue improving my data analytics skills.
Therefore I was looking for
datasets that where open source, offered a big amount of data and interesting to analyize.
Although
there were plenty of options I
decided to work with data reporting New York shooting incidents between the year 2006 to
2018. The
dataset was published at
<a href="https://catalog.data.gov/dataset/nypd-shooting-incident-data-historic"
style="color: #0366d6">data.gov</a> for public
access and use containing labels like:
</p>
<ul>
<li>Identification number</li>
<li>Occur date and time</li>
<li>Location by longitude and latitude</li>
<li>Statistical murder flag</li>
<li>Age group and gender of perpetrators</li>
<li>Age group and gender of victims</li>
</ul>
<p class="text" style="margin-bottom: 0px">
As programming language I wanted to use <code>Python 3</code> again due to its rapid
development
speed. Also I was looking forward to exploring
more of the functionality the data analysis library <code>Pandas</code> offers. For data
visualization I used <code>Matplotlib</code> by which I was able to
plot data in suited ways. Nevertheless for publishing my results on my website I switched to
<code>Chart.js</code>. The dataset itself can be downloaded in different formats like
<code>CSV, JSON</code> and contains 20.660 entries. <code>Pandas</code>
offers multiple functions to read files in <code>DataFrames</code>. I was able to read the
dataset
from a <code>CSV</code> file into a newly declared <code>DataFrame</code>
by calling the <code>read_csv()</code> function and passing the files path. In the following
you can
see some interesting results I was able to
extract from the dataset.
</p>
<pre class="prettyprint text fact">
<code class="lang-python">
# Read data from csv in DataFrame
data = pd.read_csv(PATH_TO_DATA)</code>
</pre>
<h2 style="padding-top: 2.5vh;"><span style="color: #DCDCDC;">#</span> Extracting general
knowledge</h2>
<p class="text">
To get a general understanding of all columns and their meanings, I created different
functionality
to display any informations
like the number of shooting incidents recorded by NYPD since 2006. Below chart visualizes
the number
of shooting incidents
from 2006 until 2018. You can see how the overall number of incidents generally decreased
with time.
</p>
<div class="graph">
<p class="text bold">Shooting incidents by year</p>
<canvas id="plot_years"></canvas>
</div>
<p class="text">
Thurder more I calculated the likelyhood to die in shootings by counting the number of all
entries
where the <code>STATISTICAL_MURDER_FLAG</code>
was set <code>True</code> and dividing the result by the length of the dataset.
</p>
<pre class="prettyprint text fact">
<code class="lang-python">
# Check if murder flag is true
for entry in data:
if entry:
murder_counter += 1
# Calculate and return percentile of murder by incidents
murder_rate = round((murder_counter / len(data)) * 100, 3)</code>
</pre>
<p class="text">
I also tried to describe a perpetrator and victim by gender and age group. Unfortunally
there is a
high number of unknown pepatrators whereby I was not able to exactly specify perpetrators.
This
could
be if a high percentage of perpetrators were not caught but it's just a hunch. Nevertheless
you can
see some facts below.
</p>
<div class="facts-grid">
<div class="text fact"><span
style="font-size: 2.6rem; font-weight: 700; margin: 0; color: #bf2828;">Male, 18-24
y/o<br></span>Perpetrators</div>
<div class="text fact"><span
style="font-size: 2.6rem; font-weight: 700; margin: 0; color: #bf2828;">Male, 25-44
y/o<br></span>Victims</div>
<div class="text fact"><span
style="font-size: 2.6rem; font-weight: 700; margin: 0; color: #bf2828;">~19.091%</span><br>General
murder rate</div>
</div>
<h2 style="padding-top: 2.5vh;"><span style="color: #DCDCDC;">#</span> Investigation of recent
incidents
</h2>
<p class="text">
After extracting general information of the whole dataset, I wanted to explore current data.
For
this purpose I kept myself busy
by analyzing every entry which was added in 2018 and displays newest recordings. This could
be
realized by iterating through the
dataset line by line and checking if the column <code>OCCUR_DATE</code> inherits 2018 as
specified
year. To reduce computation
power needed to process the <code>DataFrame</code>, a copy containing all entries from 2018
was
created. Below are several gathered
informations refering to newest data.
</p>
<pre class="prettyprint text fact">
<code class="lang-python">
# Filter data by year and append row new DataFrame
for index, row in data.iterrows():
if ("/" + str(year)) in str(row["OCCUR_DATE"]):
altered_data = altered_data.append(row, ignore_index = True)
# Define min and max longitude, latitude
bounding_box = [-74.24930372699998, -73.70308204399998,
40.51158633800003, 40.910818945000074]
# Get background image
image_map = plt.imread(r"../data/map.png")
# Plot data
plt.scatter(altered_data["Longitude"], altered_data["Latitude"],
c="r", alpha=0.2, zorder=1)
...</code>
</pre>
<p class="text">
To find certain shooting hotspots I took all incident locations from every entry in 2018 by
longitude and latitude. This allowed
myself to plot every incident on the map of New York. Every red dot displays an incident,
whereas
transparency decreases
when multiple incidents happended at the same location. As you can see there are two main
hotspots
for criminal activity:
</p>
<ul style="margin-bottom: 2.5vh;">
<li>Harlem / The Bronx</li>
<li>Brooklyn</li>
</ul>
<div class="graph">
<p class="text bold">Locations of reported New York shooting incidents in 2018</p>
<div id="map" style="width: 100%; ">
<img src="./imgs/project_7/nypd_shootings_2018.jpg"
alt="Reported shootings in NY (2018)" style="margin: 0;">
</div>
</div>
<p class="text">
I next wondered if shooting incidents depend on seasonal conditions. This should be visible
by
analyzing the number of incidents
on a monthly base. As you can see in the following chart there are definitely more incidents
in
warmer months than in autumn/winter except
January. This might correlate with drive for change at the beginning of every new year but
still a
hunch.
</p>
<div class="graph">
<p class="text bold">New York shooting incidents in 2018 by month</p>
<canvas id="plot_months"></canvas>
</div>
<p class="text">
Next to seasonal dependencies, I wanted to get the most likely daytime for shooting
incidents. I
excepted it to be at night but
still wanted to check my thesis.
</p>
<pre class="prettyprint text fact">
<code class="lang-python">
# Count shootings per hour of day
for entry in data:
hour, _, _ = entry.split(":")
if int(hour) not in hour_counters:
hour_counters[int(hour)] = 0
else:
hour_counters[int(hour)] += 1
# Sort dict by hours
sorted_hour_counters = OrderedDict(sorted(hour_counters.items()))
# Plot shootings by month
plt.plot(sorted_hour_counters.keys(), sorted_hour_counters.values())
...</code>
</pre>
<p class="text">
As you can see there's definitely an uptrend after lunch peaking at 9pm. There's clearly a
higher number of incidents between 8pm and 4am.
</p>
<div class="graph">
<p class="text bold">New York shooting incidents in 2018 by daytime</p>
<canvas id="plot_daytime"></canvas>
</div>
<p class="text">In total a pretty interesting dataset. Looking forward to practice my data
analytics
skills with new datasets.
You can find the complete code I developed for this dataset on my <a
href="https://github.com/Schlagoo/nypd_crime_data_analytics"
style="color: #2F58F7">Github
repository</a>!</p>
</div>
<div>
<div class="information border">
<p class="text"><b>Type</b></p>
<p class="text">Private project</p>
<p class="text"><b>Tools</b></p>
<p class="text">Python, Pandas, Numpy, Matplotlib</p>
<p class="text"><b>Partners</b></p>
<p class="text">-</p>
<p class="text"><b>Date</b></p>
<p class="text">2020-01-16</p>
<p class="text"><b>Source</b></p>
<p class="text">
<a href="https://github.com/Schlagoo/nypd_crime_data_analytics"
style="color: #2F58F7">Github
repository</a>
</p>
</div>
</div>
</div>
</div>
</section>
<footer-portfolio></footer-portfolio>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
$(document).ready(function () {
$('#hamburger, #home, #proj, abo').click(function () {
$("#hamburger").toggleClass('open');
});
});
</script>
<script type="text/javascript" src="./main.js"></script>
<script>
// General ----------------------------------------------------------------------------------------------------------
// Shootings per year plot
var ye = document.getElementById("plot_years").getContext("2d");
var plot_daytime = new Chart(ye, {
type: 'bar',
data: {
labels: ["2006", "2007", "2008", "2009", "2010", "2011", "2012", "2013", "2014", "2015", "2016", "2017", "2018"],
datasets: [{
backgroundColor: "rgb(202, 103, 103)",
borderColor: "rgb(191, 40, 40)",
borderWidth: 2,
data: [2054, 1886, 1957, 1827, 1909, 1938, 1716, 1338, 1463, 1433, 1207, 968, 950]
}]
},
options: {
legend: {
display: false
},
tooltips: {
callbacks: {
label: function (tooltipItem) {
return tooltipItem.yLabel;
}
}
},
scales: {
yAxes: [{
ticks: {
beginAtZero: true
}
}]
}
}
});
// 2018 -------------------------------------------------------------------------------------------------------------
// Months plot
var me = document.getElementById("plot_months").getContext("2d");
var plot_daytime = new Chart(me, {
type: 'bar',
data: {
labels: ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],
datasets: [{
label: "Hour of day",
backgroundColor: "rgb(202, 103, 103)",
borderColor: "rgb(191, 40, 40)",
borderWidth: 2,
data: [88, 41, 56, 68, 98, 90, 114, 91, 81, 82, 71, 71]
}]
},
options: {
legend: {
display: false
},
tooltips: {
callbacks: {
label: function (tooltipItem) {
return tooltipItem.yLabel;
}
}
},
scales: {
yAxes: [{
ticks: {
beginAtZero: true
}
}]
}
}
});
// Daytime plot
var dte = document.getElementById("plot_daytime").getContext("2d");
var plot_daytime = new Chart(dte, {
type: 'line',
data: {
labels: ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23"],
datasets: [{
backgroundColor: "rgb(202, 103, 103)",
borderColor: "rgb(191, 40, 40)",
borderWidth: 2,
data: [68, 55, 51, 50, 50, 24, 15, 12, 8, 9, 5, 21, 23, 13, 26, 23, 40, 28, 40, 51, 75, 92, 78, 70]
}]
},
options: {
legend: {
display: false
},
tooltips: {
callbacks: {
label: function (tooltipItem) {
return tooltipItem.yLabel;
}
}
}
}
});
</script>
</body>
</html>