/
smartcity_kubeflow_predict_speeding_tickets.html
1822 lines (1613 loc) · 75.9 KB
/
smartcity_kubeflow_predict_speeding_tickets.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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]> <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]> <html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js"> <!--<![endif]-->
<head>
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-119541534-1"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-119541534-1');
</script>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Patterson Consulting: Predicting Speeding Tickets in Chattanooga, TN with Kubeflow and TensorFlow</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="blog page for Patterson Consulting" />
<meta name="keywords" content="blog, patterson consulting, deep learning, machine learning, apache hadoop, apache spark, etl, consulting" />
<meta name="author" content="Patterson Consulting" />
<!-- Facebook and Twitter integration -->
<meta property="og:title" content="Predicting Speeding Tickets in Chattanooga, TN with Kubeflow and TensorFlow"/>
<meta property="og:image" content="http://www.pattersonconsultingtn.com/images/exec_strategy_bg.png"/>
<meta property="og:url" content="http://www.pattersonconsultingtn.com/blog/deploying_computer_vision_object_detection_with_kfserving.html"/>
<meta property="og:site_name" content=""/>
<meta property="og:description" content="In this example we ..."/>
<meta name="twitter:title" content="Predicting Speeding Tickets in Chattanooga, TN with Kubeflow and TensorFlow" />
<meta data-rh="true" property="twitter:description" content="In this example we ..."/>
<meta name="twitter:image" content="http://www.pattersonconsultingtn.com/images/exec_strategy_bg.png" />
<meta name="twitter:url" content="http://www.pattersonconsultingtn.com/blog/deploying_computer_vision_object_detection_with_kfserving.html" />
<meta name="twitter:card" content="summary_large_image" />
<!-- Place favicon.ico and apple-touch-icon.png in the root directory -->
<!-- <link rel="shortcut icon" href="favicon.ico"> -->
<link rel="stylesheet" href="../css/animate.css">
<link rel="stylesheet" href="../css/bootstrap.css">
<link rel="stylesheet" href="../css/icomoon.css">
<link rel="stylesheet" href="../css/owl.carousel.min.css">
<link rel="stylesheet" href="../css/owl.theme.default.min.css">
<link rel="stylesheet" href="../css/style.css">
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<link rel="shortcut icon" href="http://www.pattersonconsultingtn.com/pct.ico" type="image/x-icon" />
<style>
a {
color: #FF0000;
text-decoration: underline;
}
table {
font-family: arial, sans-serif;
border-collapse: collapse;
width: 100%;
}
td, th {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
}
tr:nth-child(even) {
background-color: #dddddd;
}
.news_item_row {
border: 0px solid #999999;
padding: 0px;
padding-top: 20px;
padding-bottom: 24px;
margin: 0px;
margin-bottom: 6px;
background-color: #ffffff;
}
.news_item_label {
border: 1px solid #cccccc;
border-bottom: 0px;
width: 50%;
padding: 12px;
padding-top: 18px;
margin: 0px;
margin-left: 0px;
background-color: #dddddd;
}
.news_item_body {
border: 2px solid #cccccc;
padding: 12px;
padding-top: 18px;
margin: 20px;
margin-left: 0px;
margin-top: 0px;
background-color: #ffffff;
}
</style>
<script src="../js/modernizr-2.6.2.min.js"></script>
<!--[if lt IE 9]>
<script src="js/respond.min.js"></script>
<![endif]-->
</head>
<body class="boxed">
<!-- Loader -->
<div class="fh5co-loader"></div>
<div id="wrap">
<div id="fh5co-page">
<header id="fh5co-header" role="banner">
<div class="container">
<a href="#" class="js-fh5co-nav-toggle fh5co-nav-toggle dark"><i></i></a>
<div id="fh5co-logo"><a href="index.html"><img src="../images/website_header_top_march2018_v0.png" ></a></div>
<nav id="fh5co-main-nav" role="navigation">
<ul>
<li><a href="../about.html">About</a></li>
<li class="has-sub">
<div class="drop-down-menu">
<a href="#">Services</a>
<div class="dropdown-menu-wrap">
<ul>
<li><a href="../offerings/data_science_offerings.html">Data Science Offerings</a></li>
<li><a href="../offerings/managed_kafka.html">Managed Kafka</a></li>
<li><a href="../offerings/managed_kubeflow.html">Managed Kubeflow</a></li>
<li><a href="../big_data_apps.html">Hadoop Applications</a></li>
<li><a href="../vision_apps.html">Computer Vision Applications</a></li>
<li><a href="../sensor_apps.html">Sensor Applications</a></li>
<li><a href="../exec_strategy.html">Executive Strategy</a></li>
</ul>
</div>
</div>
</li>
<!--
<li><a href="portfolio.html">Portfolio</a></li>
-->
<li class="has-sub">
<div class="drop-down-menu">
<a href="#">Technologies</a>
<div class="dropdown-menu-wrap">
<ul>
<li><a href="../deep_learning.html">Deep Learning</a></li>
<li><a href="../hadoop.html">Apache Hadoop</a></li>
</ul>
</div>
</div>
</li>
<li><a href="../blog/blog_index.html">Blog</a></li>
<li class="cta"><a href="../contact.html">Contact</a></li>
</ul>
</nav>
</div>
</header>
<div id="fh5co-intro" class="fh5co-section">
<div class="container">
<div class="row row-bottom-padded-sm">
<div class="col-md-12" id="fh5co-content">
<h1>Predicting Speeding Tickets in Chattanooga, TN with Kubeflow and TensorFlow</h1>
<p>Author: Pete Way</a>
<br/>
</p>
<div>
<img src="https://github.com/pattersonconsulting/pattersonconsulting.github.io/raw/master/blog/images/Citations_TF_Way_CiteDensity.png" class="center" style="width:900px">
</div>
<p>
In this example we demonstrate how to take a multi-layer perceptron neural network out of Tensorflow and Keras libraries, with additional result analysis provided by scikit-learn. Within this tutorial, the reader will begin to understand:</p>
<ul>
<li>Import of datasets from CSV, and feather files. </li>
<li>Addition of temporal specification variables</li>
<li>Addition of spatial markers to specific GPS coordinates<ul>
<li>GPS coordinates to Spatial Points</li>
<li>Adjusting projection of Points. </li>
</ul>
</li>
<li>Creation of negative samples based off of a collection of variable combinations.</li>
<li>Creation of a variety of neural networks with combinations of input metrics. </li>
<li>Analysis of the performance of the aforementioned networks based on overall Accuracy and Recall. </li>
</ul>
</p>
<h2>Introduction to Tensorflow</h2>
<p>For those who are not already familiar with the Tensorflow Python library, here is the definition of Tensorflow, from their website itself.</p>
<blockquote class="w3-panel w3-leftbar w3-light-grey" style="padding: 16px; font-size: 12px; border: 1px solid #999999;">
<p style="font-size: 14px;">"TensorFlow is an end-to-end open source platform for machine learning.
It has a comprehensive, flexible ecosystem of tools, libraries and community resources
that lets researchers push the state-of-the-art in ML and developers easily build and deploy
ML powered applications."
-<a href="https://www.tensorflow.org/">Tensorflow.org</a></p>
</blockquote>
<p>Tensorflow is an amazing backend for many common machine learning problems, and provides easy model building regardless of the coder's experience level. The Tensorflow webpage features many example dataset and code pairings to get one acclimated to the coding involved.</p>
<p>Furthermore, in this tutorial we will be exploring the Sequential model from Keras:</p>
<blockquote class="w3-panel w3-leftbar w3-light-grey" style="padding: 16px; font-size: 12px; border: 1px solid #999999;">
<p style="font-size: 14px;">
A Sequential model is appropriate for a plain stack of layers where each layer has exactly one input tensor and one output tensor.
-<a href="https://keras.io/guides/sequential_model/">Keras.io</a></p>
</blockquote>
<p>Sequential models are wonderful for linear questions, such as 'Will it rain today?', where each input is fed through the model without doubling-back. They are not meant for situations with multiple input/multiple output, or if the layers are meant to be shared.</p>
<h2>Use Case Question</h2>
<p>Where in Chattanooga are speeding tickets being issued?
Is it possible to predict which roadways are likely to see speeding violations based off of historical reports?</p>
<h2>Introduction to Data</h2>
<p>Both sets data utilized in this walkthrough can be accessed and are free for public usage.</p>
<p>The citation data set referenced is available from the ChattaData.org page here: <a href="https://internal.chattadata.org/Public-Safety/City-Court-Citations/th6b-88wc">City Court Citations</a>. </p>
<p>The citation data includes spatial and temporal data about the location of citations issued from city courts, as well as roughly anonymous data regarding the individual receiving the citation.</p>
<p>Roadway data for the area is also available via ChattaData, here: <a href="https://internal.chattadata.org/dataset/Chattanooga-Roadways/mw3f-d2mz">Chattanooga Roadways</a>. </p>
<p>Roadway data includes spatial data regarding to individual segments of roadways within the Chattanooga area. Rough address data is provided, and allows for the creation of a singular 'Segment' column.</p>
<img src="https://github.com/pattersonconsulting/pattersonconsulting.github.io/raw/master/blog/images/Citations_TF_Way_Roadways.png" class="center" width="550"/>
<hr>
<h2>PreProcessing</h2>
<h3>Importing and Exploring Data</h3>
<p>The first step to understanding any selection of data is taking a look into what data you actually have. Here, we're importing a dataset that includes rough anonymized data regarding Court Citations. First, let's import the dataset CSV using pandas, and take a look at how many entries we have.</p>
<script charset="UTF-8" src="http://gist-it.appspot.com/github.com/PeteWay/Consulting/blob/master/Citations.py?slice=76:78&footer=minimal"></script>
<pre>
212812
</pre>
<p>Next, let's take a look at the types of columns we have. Our data includes temporal and spatial data regarding the citations recorded.</p>
<p>Now, let's take a look at the first ten records.</p>
<p>This combination of commands lets us take a closer look into what data we have. Notice that there are multiple 'object' columns, which includes the type and date of the violation, as well as information regarding the individual receiving the citation.</p>
<script charset="UTF-8" src="http://gist-it.appspot.com/github.com/PeteWay/Consulting/blob/master/Citations.py?slice=89:91&footer=minimal"></script>
<pre>
Citation Year int64
Citation Number object
Offense Description object
Offense Code object
Race of Offender object
Sex of Offender object
Violation Date object
Address object
Longitude float64
Latitude float64
Location WKT object
Citation_Charge_Link int64
Agency object
dtype: object
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
</style>
<table class="dataframe" width="50%">
<thead>
<tr style="text-align: right;">
<th></th>
<th>Citation Year</th>
<th>Citation Number</th>
<th>Offense Description</th>
<th>Offense Code</th>
<th>Race of Offender</th>
<th>Sex of Offender</th>
<th>Violation Date</th>
<th>Address</th>
<th>Longitude</th>
<th>Latitude</th>
<th>Location WKT</th>
<th>Citation_Charge_Link</th>
<th>Agency</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>2019</td>
<td>Q33097</td>
<td>RUNNING A RED LIGHT: ACCIDENT</td>
<td>O0218A</td>
<td>White</td>
<td>Female</td>
<td>2/17/19 21:30</td>
<td>400 N MARKET ST</td>
<td>-85.308799</td>
<td>35.065202</td>
<td>POINT (-85.308798717696 35.065201611634)</td>
<td>1187952</td>
<td>CHATANOOGA PD</td>
</tr>
<tr>
<th>1</th>
<td>2019</td>
<td>Q33097</td>
<td>FAILURE TO YIELD RIGHT-OF-WAY: ACCIDENT</td>
<td>55-8-130</td>
<td>White</td>
<td>Female</td>
<td>2/17/19 21:30</td>
<td>400 N MARKET ST</td>
<td>-85.308799</td>
<td>35.065202</td>
<td>POINT (-85.308798717696 35.065201611634)</td>
<td>1187953</td>
<td>CHATANOOGA PD</td>
</tr>
<tr>
<th>2</th>
<td>2019</td>
<td>Y132034</td>
<td>LIGHT LAW VIOLATION</td>
<td>55-9-402</td>
<td>African American</td>
<td>Male</td>
<td>1/1/19 21:19</td>
<td>1000 N HOLTZCLAW AVE</td>
<td>-85.275350</td>
<td>35.051747</td>
<td>POINT (-85.275350006915 35.05174717652)</td>
<td>1220642</td>
<td>CHATANOOGA PD</td>
</tr>
<tr>
<th>3</th>
<td>2019</td>
<td>Y029161</td>
<td>SPEEDING</td>
<td>55-8-152</td>
<td>White</td>
<td>Male</td>
<td>1/5/19 21:15</td>
<td>5300 HW153 SB</td>
<td>-85.242198</td>
<td>35.127374</td>
<td>POINT (-85.242198247004 35.127374384516)</td>
<td>1221039</td>
<td>CHATANOOGA PD</td>
</tr>
<tr>
<th>4</th>
<td>2019</td>
<td>Y029161</td>
<td>FINANCIAL RESPONSIBILITY LAW (INSURANCE LAW)</td>
<td>55-12-139</td>
<td>White</td>
<td>Male</td>
<td>1/5/19 21:15</td>
<td>5300 HW153 SB</td>
<td>-85.242198</td>
<td>35.127374</td>
<td>POINT (-85.242198247004 35.127374384516)</td>
<td>1221040</td>
<td>CHATANOOGA PD</td>
</tr>
<tr>
<th>5</th>
<td>2019</td>
<td>Y050066</td>
<td>FINANCIAL RESPONSIBILITY LAW (INSURANCE LAW)</td>
<td>55-12-139</td>
<td>African American</td>
<td>Male</td>
<td>1/4/19 23:59</td>
<td>1000 TUNNEL BLVD</td>
<td>-85.239521</td>
<td>35.040177</td>
<td>POINT (-85.239521412228 35.040177111372)</td>
<td>1221041</td>
<td>CHATANOOGA PD</td>
</tr>
<tr>
<th>6</th>
<td>2019</td>
<td>Y050066</td>
<td>LIGHT LAW VIOLATION</td>
<td>55-9-402</td>
<td>African American</td>
<td>Male</td>
<td>1/4/19 23:59</td>
<td>1000 TUNNEL BLVD</td>
<td>-85.239521</td>
<td>35.040177</td>
<td>POINT (-85.239521412228 35.040177111372)</td>
<td>1221043</td>
<td>CHATANOOGA PD</td>
</tr>
<tr>
<th>7</th>
<td>2019</td>
<td>Y066034</td>
<td>SPEEDING</td>
<td>55-8-152</td>
<td>African American</td>
<td>Male</td>
<td>1/6/19 23:14</td>
<td>300 MANUFACTURERS RD</td>
<td>-85.313603</td>
<td>35.062448</td>
<td>POINT (-85.313603119038 35.062448419579)</td>
<td>1221114</td>
<td>CHATANOOGA PD</td>
</tr>
<tr>
<th>8</th>
<td>2019</td>
<td>Y087627</td>
<td>FINANCIAL RESPONSIBILITY LAW (INSURANCE LAW)</td>
<td>55-12-139</td>
<td>African American</td>
<td>Male</td>
<td>1/9/19 17:22</td>
<td>900 N CHAMBERLAIN AVE</td>
<td>-85.260147</td>
<td>35.045116</td>
<td>POINT (-85.260146624969 35.045115634392)</td>
<td>1222463</td>
<td>CHATANOOGA PD</td>
</tr>
<tr>
<th>9</th>
<td>2019</td>
<td>Y082581</td>
<td>CHILD RESTRAINT LAW VIOLATION</td>
<td>55-9-602</td>
<td>African American</td>
<td>Female</td>
<td>4/11/19 13:40</td>
<td>827 WOODMOORE CIR</td>
<td>-85.226635</td>
<td>35.034660</td>
<td>POINT (-85.226635370051 35.03465955868)</td>
<td>1231728</td>
<td>CHATANOOGA PD</td>
</tr>
</tbody>
</table>
</div>
</pre>
<p>Let's cut this data down to just the records where the violation was for speeding, to give us a better answer to our main questions we asked above. We'll also reindex the dataset for ease of understanding.</p>
<script charset="UTF-8" src="http://gist-it.appspot.com/github.com/PeteWay/Consulting/blob/master/Citations.py?slice=98:100&footer=minimal"></script>
<p>Now, we'll be taking a look at our reduced data, now that we've cut it down to just the records that pertain to our question. We can see that there were 20,815 speeding tickets within that larger dataset.</p>
<p>Again, we'll use the head command to take a look at the first ten records of speeding.</p>
<script charset="UTF-8" src="http://gist-it.appspot.com/github.com/PeteWay/Consulting/blob/master/Citations.py?slice=109:111&footer=minimal"></script>
<pre>
20815
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
</style>
<table class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>index</th>
<th>Citation Year</th>
<th>Citation Number</th>
<th>Offense Description</th>
<th>Offense Code</th>
<th>Race of Offender</th>
<th>Sex of Offender</th>
<th>Violation Date</th>
<th>Address</th>
<th>Longitude</th>
<th>Latitude</th>
<th>Location WKT</th>
<th>Citation_Charge_Link</th>
<th>Agency</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>3</td>
<td>2019</td>
<td>Y029161</td>
<td>SPEEDING</td>
<td>55-8-152</td>
<td>White</td>
<td>Male</td>
<td>1/5/19 21:15</td>
<td>5300 HW153 SB</td>
<td>-85.242198</td>
<td>35.127374</td>
<td>POINT (-85.242198247004 35.127374384516)</td>
<td>1221039</td>
<td>CHATANOOGA PD</td>
</tr>
<tr>
<th>1</th>
<td>7</td>
<td>2019</td>
<td>Y066034</td>
<td>SPEEDING</td>
<td>55-8-152</td>
<td>African American</td>
<td>Male</td>
<td>1/6/19 23:14</td>
<td>300 MANUFACTURERS RD</td>
<td>-85.313603</td>
<td>35.062448</td>
<td>POINT (-85.313603119038 35.062448419579)</td>
<td>1221114</td>
<td>CHATANOOGA PD</td>
</tr>
<tr>
<th>2</th>
<td>30</td>
<td>2019</td>
<td>Y029169</td>
<td>SPEEDING</td>
<td>55-8-152</td>
<td>White</td>
<td>Male</td>
<td>1/16/19 23:44</td>
<td>5400 HW153</td>
<td>-85.246361</td>
<td>35.136575</td>
<td>POINT (-85.246360986015 35.136574900541)</td>
<td>1222242</td>
<td>CHATANOOGA PD</td>
</tr>
<tr>
<th>3</th>
<td>37</td>
<td>2019</td>
<td>Y003716</td>
<td>SPEEDING</td>
<td>55-8-152</td>
<td>Asian</td>
<td>Male</td>
<td>1/9/19 10:12</td>
<td>5300 HW153 SB</td>
<td>-85.242198</td>
<td>35.127374</td>
<td>POINT (-85.242198247004 35.127374384516)</td>
<td>1221499</td>
<td>CHATANOOGA PD</td>
</tr>
<tr>
<th>4</th>
<td>40</td>
<td>2019</td>
<td>Y029167</td>
<td>SPEEDING</td>
<td>55-8-152</td>
<td>NaN</td>
<td>Male</td>
<td>1/16/19 21:53</td>
<td>5400 HW153</td>
<td>-85.246361</td>
<td>35.136575</td>
<td>POINT (-85.246360986015 35.136574900541)</td>
<td>1222238</td>
<td>CHATANOOGA PD</td>
</tr>
</tbody>
</table>
</div>
</pre>
<p>We'll need to adjust how our time and date are displayed before splitting the data up, just for simplicity's sake for later usage. Here, we're using a lambda statement to avoid utilizing for loops. For loops are great for assigning variables but they can get slowed down if the dataset becomes too large. While that's not a problem with this smaller dataset, it's good to familarize yourself with time and computation saving code whenever possible.</p>
<script charset="UTF-8" src="http://gist-it.appspot.com/github.com/PeteWay/Consulting/blob/master/Citations.py?slice=118:124&footer=minimal"></script>
<p>With the time and date split, let's now get some extra variables to help our model understand those values. Neural network models can't parse string variables, so we'll need to pull out the month, day of the week, and hour in order for the model to understand.</p>
<p>The weekday function finds the day of the week of a given date, and assigns it a value between 0 and 6, where Monday is zero, and Sunday is six.</p>
<p>Since the dataset already included a year column, we don't have to find that manually. We're using lambdas here as well.</p>
<p>Finally, we'll take a look at the head of the dataset again to make sure all of our commands worked correctly.</p>
<script charset="UTF-8" src="http://gist-it.appspot.com/github.com/PeteWay/Consulting/blob/master/Citations.py?slice=137:143&footer=minimal"></script>
<pre>
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
</style>
<table class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>index</th>
<th>Year</th>
<th>Citation Number</th>
<th>Offense Description</th>
<th>Offense Code</th>
<th>Race of Offender</th>
<th>Sex of Offender</th>
<th>Violation Date</th>
<th>Address</th>
<th>Longitude</th>
<th>Latitude</th>
<th>Location WKT</th>
<th>Citation_Charge_Link</th>
<th>Agency</th>
<th>Time</th>
<th>Date</th>
<th>Month</th>
<th>WeekDay</th>
<th>Hour</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>3</td>
<td>2019</td>
<td>Y029161</td>
<td>SPEEDING</td>
<td>55-8-152</td>
<td>White</td>
<td>Male</td>
<td>2019-01-05 21:15:00</td>
<td>5300 HW153 SB</td>
<td>-85.242198</td>
<td>35.127374</td>
<td>POINT (-85.242198247004 35.127374384516)</td>
<td>1221039</td>
<td>CHATANOOGA PD</td>
<td>21:15:00</td>
<td>2019-01-05</td>
<td>1</td>
<td>5</td>
<td>21</td>
</tr>
<tr>
<th>1</th>
<td>7</td>
<td>2019</td>
<td>Y066034</td>
<td>SPEEDING</td>
<td>55-8-152</td>
<td>African American</td>
<td>Male</td>
<td>2019-01-06 23:14:00</td>
<td>300 MANUFACTURERS RD</td>
<td>-85.313603</td>
<td>35.062448</td>
<td>POINT (-85.313603119038 35.062448419579)</td>
<td>1221114</td>
<td>CHATANOOGA PD</td>
<td>23:14:00</td>
<td>2019-01-06</td>
<td>1</td>
<td>6</td>
<td>23</td>
</tr>
<tr>
<th>2</th>
<td>30</td>
<td>2019</td>
<td>Y029169</td>
<td>SPEEDING</td>
<td>55-8-152</td>
<td>White</td>
<td>Male</td>
<td>2019-01-16 23:44:00</td>
<td>5400 HW153</td>
<td>-85.246361</td>
<td>35.136575</td>
<td>POINT (-85.246360986015 35.136574900541)</td>
<td>1222242</td>
<td>CHATANOOGA PD</td>
<td>23:44:00</td>
<td>2019-01-16</td>
<td>1</td>
<td>2</td>
<td>23</td>
</tr>
<tr>
<th>3</th>
<td>37</td>
<td>2019</td>
<td>Y003716</td>
<td>SPEEDING</td>
<td>55-8-152</td>
<td>Asian</td>
<td>Male</td>
<td>2019-01-09 10:12:00</td>
<td>5300 HW153 SB</td>
<td>-85.242198</td>
<td>35.127374</td>
<td>POINT (-85.242198247004 35.127374384516)</td>
<td>1221499</td>
<td>CHATANOOGA PD</td>
<td>10:12:00</td>
<td>2019-01-09</td>
<td>1</td>
<td>2</td>
<td>10</td>
</tr>
<tr>
<th>4</th>
<td>40</td>
<td>2019</td>
<td>Y029167</td>
<td>SPEEDING</td>
<td>55-8-152</td>
<td>NaN</td>
<td>Male</td>
<td>2019-01-16 21:53:00</td>
<td>5400 HW153</td>
<td>-85.246361</td>
<td>35.136575</td>
<td>POINT (-85.246360986015 35.136574900541)</td>
<td>1222238</td>
<td>CHATANOOGA PD</td>
<td>21:53:00</td>
<td>2019-01-16</td>
<td>1</td>
<td>2</td>
<td>21</td>
</tr>
</tbody>
</table>
</pre>
<h3>Combining Roadway Data with Citation reports.</h3>
<p>The next section combines our roadway information with the citation information to give each entry a set roadway name.</p>
<p>First, we'll be creating the geometry column for each of the GPS coordinates in the dataset.</p>
<p>Then, we'll be setting a coordinate reference system, so that the roadway data and the citation records can be properly matched.</p>
<p>Finally, we'll take a look at the head of the data again. Notice that the Location WKT column is almost exactly the same as the newly created geometry column. While we could have simply assigned that column as the 'geometry', it is simply a measure of caution to create the geometry column ourselves.</p>
<script charset="UTF-8" src="http://gist-it.appspot.com/github.com/PeteWay/Consulting/blob/master/Citations.py?slice=158:166&footer=minimal"></script>
<pre>
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
</style>
<table class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>index</th>
<th>Year</th>
<th>Citation Number</th>
<th>Offense Description</th>
<th>Offense Code</th>
<th>Race of Offender</th>
<th>Sex of Offender</th>
<th>Violation Date</th>
<th>Address</th>
<th>Longitude</th>
<th>Latitude</th>
<th>Location WKT</th>
<th>Citation_Charge_Link</th>
<th>Agency</th>
<th>Time</th>
<th>Date</th>
<th>Month</th>
<th>WeekDay</th>
<th>Hour</th>
<th>geometry</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>3</td>
<td>2019</td>
<td>Y029161</td>
<td>SPEEDING</td>
<td>55-8-152</td>
<td>White</td>
<td>Male</td>
<td>2019-01-05 21:15:00</td>
<td>5300 HW153 SB</td>
<td>-85.242198</td>
<td>35.127374</td>
<td>POINT (-85.242198247004 35.127374384516)</td>
<td>1221039</td>
<td>CHATANOOGA PD</td>
<td>21:15:00</td>
<td>2019-01-05</td>
<td>1</td>
<td>5</td>
<td>21</td>
<td>POINT (-85.24220 35.12737)</td>
</tr>
<tr>
<th>1</th>
<td>7</td>
<td>2019</td>
<td>Y066034</td>
<td>SPEEDING</td>
<td>55-8-152</td>
<td>African American</td>
<td>Male</td>
<td>2019-01-06 23:14:00</td>
<td>300 MANUFACTURERS RD</td>
<td>-85.313603</td>
<td>35.062448</td>
<td>POINT (-85.313603119038 35.062448419579)</td>
<td>1221114</td>
<td>CHATANOOGA PD</td>
<td>23:14:00</td>
<td>2019-01-06</td>
<td>1</td>
<td>6</td>
<td>23</td>
<td>POINT (-85.31360 35.06245)</td>
</tr>
<tr>
<th>2</th>
<td>30</td>
<td>2019</td>
<td>Y029169</td>
<td>SPEEDING</td>
<td>55-8-152</td>
<td>White</td>
<td>Male</td>
<td>2019-01-16 23:44:00</td>
<td>5400 HW153</td>
<td>-85.246361</td>
<td>35.136575</td>
<td>POINT (-85.246360986015 35.136574900541)</td>
<td>1222242</td>
<td>CHATANOOGA PD</td>
<td>23:44:00</td>
<td>2019-01-16</td>
<td>1</td>
<td>2</td>
<td>23</td>
<td>POINT (-85.24636 35.13657)</td>
</tr>
<tr>
<th>3</th>
<td>37</td>
<td>2019</td>
<td>Y003716</td>
<td>SPEEDING</td>
<td>55-8-152</td>
<td>Asian</td>
<td>Male</td>
<td>2019-01-09 10:12:00</td>
<td>5300 HW153 SB</td>
<td>-85.242198</td>
<td>35.127374</td>
<td>POINT (-85.242198247004 35.127374384516)</td>
<td>1221499</td>
<td>CHATANOOGA PD</td>
<td>10:12:00</td>
<td>2019-01-09</td>
<td>1</td>
<td>2</td>
<td>10</td>
<td>POINT (-85.24220 35.12737)</td>
</tr>
<tr>
<th>4</th>
<td>40</td>
<td>2019</td>
<td>Y029167</td>
<td>SPEEDING</td>
<td>55-8-152</td>
<td>NaN</td>
<td>Male</td>
<td>2019-01-16 21:53:00</td>
<td>5400 HW153</td>
<td>-85.246361</td>
<td>35.136575</td>
<td>POINT (-85.246360986015 35.136574900541)</td>
<td>1222238</td>
<td>CHATANOOGA PD</td>
<td>21:53:00</td>
<td>2019-01-16</td>
<td>1</td>
<td>2</td>
<td>21</td>
<td>POINT (-85.24636 35.13657)</td>
</tr>
</tbody>
</table>
</pre>
<p>Now, we'll be importing our roadway data, double-checking the type of our roadways, and limiting the entries to only that type. Note that sometimes datasets will have corrupted or incomplete data. This was the case with this dataset, where one entry of the roadways was incomplete. We can see that in the totals printed before and after the selection line.</p>
<p>Next, we join the two datasets together with a geopandas command called sjoin. Sjoin determines spatial matching between datasets of any type (Point, Polygon, Multi-line, etc) and one can select how they would like the new merged set to be set up by selecting 'left', 'right', or 'inner' for the how variable. We are looking to select citations within the roadway data, so we will select 'left', since our citations set is the left variable.</p>
<p>Something interesting happens when we merge our data, though. notice that the number of records actually increases. This is related to the records that do not fall within a given roadway polygon. The true number of citations retained is shown in our last print line, where we can see the number falling to 18,576.</p>
<p>Once again, we'll print the head of the data to verify our changes.</p>
<script charset="UTF-8" src="http://gist-it.appspot.com/github.com/PeteWay/Consulting/blob/master/Citations.py?slice=179:196&footer=minimal"></script>
<pre>
0 Polygon
1 Polygon
2 Polygon
3 Polygon
4 Polygon
...
11504 Polygon
11505 Polygon
11506 Polygon
11507 Polygon
11508 Polygon
Length: 11509, dtype: object
Roads: 11509
Roads: 11473
Citation records before merge: 20815
Records after merge 54517
Citations before dropping duplicates: 54517
Citations after dropping duplicates: 18576
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
</style>