In [1]:
import pandas as pd
import numpy as np
from datetime import datetime, timedelta
cat = 'noise'
housing = pd.read_csv('{}-complaints.csv'.format(cat))
/usr/lib/python2.7/site-packages/IPython/core/interactiveshell.py:2717: DtypeWarning: Columns (8,18,19,21,39) have mixed types. Specify dtype option on import or set low_memory=False.
interactivity=interactivity, compiler=compiler, result=result)
In [2]:
# created_index = pd.DatetimeIndex(housing['Created Date'])
# closed_index = pd.DatetimeIndex(housing['Closed Date'])
housing['Created Datetime'] = pd.to_datetime(housing['Created Date'])
In [3]:
housing
Out[3]:
Unnamed: 0
Unique Key
Created Date
Closed Date
Agency
Agency Name
Complaint Type
Descriptor
Location Type
Incident Zip
...
Bridge Highway Direction
Road Ramp
Bridge Highway Segment
Garage Lot Name
Ferry Direction
Ferry Terminal Name
Latitude
Longitude
Location
Created Datetime
0
0
30734982
05/30/2015 10:20:44 PM
05/30/2015 10:27:04 PM
NYPD
New York City Police Department
Noise - Vehicle
Car/Truck Music
Street/Sidewalk
10472.0
...
NaN
NaN
NaN
NaN
NaN
NaN
40.831875
-73.866355
(40.831875080919744, -73.86635456121994)
2015-05-30 22:20:44
1
1
30898443
06/20/2015 12:43:36 PM
06/20/2015 03:24:10 PM
NYPD
New York City Police Department
Noise - Vehicle
Engine Idling
Street/Sidewalk
11220.0
...
NaN
NaN
NaN
NaN
NaN
NaN
40.633223
-74.015146
(40.63322316540144, -74.01514635324975)
2015-06-20 12:43:36
2
2
31251174
08/06/2015 11:13:26 PM
08/07/2015 01:10:01 AM
NYPD
New York City Police Department
Noise - Vehicle
Car/Truck Music
Street/Sidewalk
10032.0
...
NaN
NaN
NaN
NaN
NaN
NaN
40.838823
-73.945635
(40.83882344002816, -73.94563493239124)
2015-08-06 23:13:26
3
3
31578191
09/20/2015 04:39:28 AM
09/20/2015 06:43:21 AM
NYPD
New York City Police Department
Noise - Vehicle
Car/Truck Music
Street/Sidewalk
11211.0
...
NaN
NaN
NaN
NaN
NaN
NaN
40.717341
-73.939457
(40.71734107285568, -73.9394571631376)
2015-09-20 04:39:28
4
4
31789663
10/19/2015 06:25:15 AM
10/19/2015 06:58:37 AM
NYPD
New York City Police Department
Noise - Vehicle
Engine Idling
Street/Sidewalk
11211.0
...
NaN
NaN
NaN
NaN
NaN
NaN
40.717707
-73.936766
(40.71770742395871, -73.93676570169862)
2015-10-19 06:25:15
5
5
31494828
09/09/2015 11:34:02 PM
09/10/2015 01:48:56 AM
NYPD
New York City Police Department
Noise - Vehicle
Car/Truck Music
Street/Sidewalk
10459.0
...
NaN
NaN
NaN
NaN
NaN
NaN
40.828956
-73.893852
(40.828955843057265, -73.89385151414002)
2015-09-09 23:34:02
6
6
30999974
07/03/2015 09:35:32 PM
07/03/2015 10:25:45 PM
NYPD
New York City Police Department
Noise - Vehicle
Car/Truck Music
Street/Sidewalk
10472.0
...
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
2015-07-03 21:35:32
7
7
30999975
07/03/2015 10:17:26 PM
07/03/2015 11:06:34 PM
NYPD
New York City Police Department
Noise - Vehicle
Car/Truck Music
Street/Sidewalk
10035.0
...
NaN
NaN
NaN
NaN
NaN
NaN
40.801342
-73.945969
(40.80134172109166, -73.94596894766171)
2015-07-03 22:17:26
8
8
31612897
09/26/2015 02:09:45 AM
09/26/2015 02:38:30 AM
NYPD
New York City Police Department
Noise - Vehicle
Car/Truck Music
Street/Sidewalk
10037.0
...
NaN
NaN
NaN
NaN
NaN
NaN
40.810869
-73.941063
(40.81086900186397, -73.94106271701641)
2015-09-26 02:09:45
9
9
31611501
09/25/2015 10:35:24 PM
09/26/2015 03:28:59 AM
NYPD
New York City Police Department
Noise - Vehicle
Car/Truck Horn
Street/Sidewalk
11207.0
...
NaN
NaN
NaN
NaN
NaN
NaN
40.674777
-73.887132
(40.67477694475006, -73.88713187162713)
2015-09-25 22:35:24
10
10
31613680
09/25/2015 08:46:19 AM
09/25/2015 09:57:40 AM
NYPD
New York City Police Department
Noise - Vehicle
Car/Truck Music
Street/Sidewalk
10024.0
...
NaN
NaN
NaN
NaN
NaN
NaN
40.787659
-73.967611
(40.787659122774215, -73.96761114390758)
2015-09-25 08:46:19
11
11
30069077
03/01/2015 10:55:58 PM
03/01/2015 11:31:37 PM
NYPD
New York City Police Department
Noise - Vehicle
Engine Idling
Street/Sidewalk
11206.0
...
NaN
NaN
NaN
NaN
NaN
NaN
40.696557
-73.949433
(40.69655704185142, -73.94943293854924)
2015-03-01 22:55:58
12
12
31900899
11/03/2015 08:51:42 PM
11/04/2015 01:17:59 AM
NYPD
New York City Police Department
Noise - Vehicle
Car/Truck Music
Street/Sidewalk
10014.0
...
NaN
NaN
NaN
NaN
NaN
NaN
40.740297
-74.007939
(40.74029738732747, -74.00793899318337)
2015-11-03 20:51:42
13
13
30842127
06/13/2015 04:33:24 AM
06/13/2015 06:44:58 AM
NYPD
New York City Police Department
Noise - Vehicle
Engine Idling
Street/Sidewalk
11233.0
...
NaN
NaN
NaN
NaN
NaN
NaN
40.669921
-73.918888
(40.66992075690915, -73.9188878079478)
2015-06-13 04:33:24
14
14
30485997
04/26/2015 10:52:26 PM
04/26/2015 11:09:17 PM
NYPD
New York City Police Department
Noise - Vehicle
Car/Truck Music
Street/Sidewalk
10026.0
...
NaN
NaN
NaN
NaN
NaN
NaN
40.799459
-73.951529
(40.799458565558155, -73.9515290994865)
2015-04-26 22:52:26
15
15
30585931
05/10/2015 04:53:38 PM
05/10/2015 09:01:50 PM
NYPD
New York City Police Department
Noise - Vehicle
Car/Truck Music
Street/Sidewalk
10026.0
...
NaN
NaN
NaN
NaN
NaN
NaN
40.802253
-73.953243
(40.80225340545579, -73.95324276081412)
2015-05-10 16:53:38
16
16
30828559
06/11/2015 09:10:27 PM
06/11/2015 09:57:33 PM
NYPD
New York City Police Department
Noise - Vehicle
Car/Truck Music
Street/Sidewalk
11207.0
...
NaN
NaN
NaN
NaN
NaN
NaN
40.691301
-73.906216
(40.69130115261603, -73.90621561520808)
2015-06-11 21:10:27
17
17
31577104
09/20/2015 04:35:59 AM
09/20/2015 07:17:46 AM
NYPD
New York City Police Department
Noise - Vehicle
Car/Truck Music
Street/Sidewalk
10457.0
...
NaN
NaN
NaN
NaN
NaN
NaN
40.847241
-73.890866
(40.84724107129996, -73.89086572534552)
2015-09-20 04:35:59
18
18
31514055
09/11/2015 11:32:33 PM
09/12/2015 04:25:55 AM
NYPD
New York City Police Department
Noise - Vehicle
Car/Truck Music
Street/Sidewalk
10308.0
...
NaN
NaN
NaN
NaN
NaN
NaN
40.556737
-74.159582
(40.55673653113164, -74.15958167130559)
2015-09-11 23:32:33
19
19
30879912
06/19/2015 12:15:10 AM
06/19/2015 03:26:01 AM
NYPD
New York City Police Department
Noise - Vehicle
Engine Idling
Street/Sidewalk
11692.0
...
NaN
NaN
NaN
NaN
NaN
NaN
40.586674
-73.801715
(40.58667427843524, -73.80171514636476)
2015-06-19 00:15:10
20
20
31580238
09/21/2015 05:24:01 PM
09/21/2015 07:29:56 PM
NYPD
New York City Police Department
Noise - Vehicle
Car/Truck Horn
Street/Sidewalk
11377.0
...
NaN
NaN
NaN
NaN
NaN
NaN
40.753724
-73.911047
(40.75372356377427, -73.91104729292951)
2015-09-21 17:24:01
21
21
31616922
09/25/2015 04:13:13 PM
09/26/2015 03:33:18 AM
NYPD
New York City Police Department
Noise - Vehicle
Car/Truck Horn
Street/Sidewalk
10032.0
...
NaN
NaN
NaN
NaN
NaN
NaN
40.834734
-73.945432
(40.834733725102346, -73.94543229012842)
2015-09-25 16:13:13
22
22
31584128
09/21/2015 06:00:00 PM
09/22/2015 02:52:44 PM
NYPD
New York City Police Department
Noise - Vehicle
Car/Truck Horn
Street/Sidewalk
11235.0
...
NaN
NaN
NaN
NaN
NaN
NaN
40.586547
-73.951176
(40.58654679193705, -73.95117598848994)
2015-09-21 18:00:00
23
23
30572512
05/08/2015 07:29:12 PM
05/08/2015 10:50:38 PM
NYPD
New York City Police Department
Noise - Vehicle
Car/Truck Music
Street/Sidewalk
11207.0
...
NaN
NaN
NaN
NaN
NaN
NaN
40.656176
-73.882575
(40.65617649702889, -73.8825753397958)
2015-05-08 19:29:12
24
24
31611705
09/25/2015 08:02:55 AM
09/26/2015 10:19:33 AM
NYPD
New York City Police Department
Noise - Vehicle
Engine Idling
Street/Sidewalk
11101.0
...
NaN
NaN
NaN
NaN
NaN
NaN
40.744217
-73.949426
(40.74421704008074, -73.94942563540398)
2015-09-25 08:02:55
25
25
30900829
06/21/2015 12:35:26 PM
06/21/2015 02:37:17 PM
NYPD
New York City Police Department
Noise - Vehicle
Car/Truck Horn
Street/Sidewalk
10462.0
...
NaN
NaN
NaN
NaN
NaN
NaN
40.836112
-73.845729
(40.83611187894238, -73.84572912093441)
2015-06-21 12:35:26
26
26
31221781
08/02/2015 06:49:18 PM
08/02/2015 07:00:36 PM
NYPD
New York City Police Department
Noise - Vehicle
Engine Idling
Street/Sidewalk
10002.0
...
NaN
NaN
NaN
NaN
NaN
NaN
40.714821
-73.992630
(40.714820597241975, -73.9926303802237)
2015-08-02 18:49:18
27
27
30604575
05/13/2015 12:07:00 PM
05/13/2015 12:13:00 PM
NYPD
New York City Police Department
Noise - Vehicle
Car/Truck Music
Street/Sidewalk
11201.0
...
NaN
NaN
NaN
NaN
NaN
NaN
40.690617
-73.990581
(40.69061710685595, -73.99058127292989)
2015-05-13 12:07:00
28
28
30459815
04/22/2015 05:37:43 PM
04/22/2015 06:13:06 PM
NYPD
New York City Police Department
Noise - Vehicle
Car/Truck Horn
Street/Sidewalk
10013.0
...
NaN
NaN
NaN
NaN
NaN
NaN
40.721323
-74.008344
(40.721322866775374, -74.00834439237221)
2015-04-22 17:37:43
29
29
31259316
08/07/2015 03:55:52 PM
08/07/2015 10:39:45 PM
NYPD
New York City Police Department
Noise - Vehicle
Car/Truck Music
Street/Sidewalk
10472.0
...
NaN
NaN
NaN
NaN
NaN
NaN
40.830450
-73.866021
(40.83045018662338, -73.86602136528577)
2015-08-07 15:55:52
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
379610
379610
32082493
11/27/2015 06:53:34 PM
11/27/2015 07:19:32 PM
NYPD
New York City Police Department
Noise - Commercial
Loud Music/Party
Store/Commercial
11213.0
...
NaN
NaN
NaN
NaN
NaN
NaN
40.669981
-73.931000
(40.669980809733275, -73.93099996423969)
2015-11-27 18:53:34
379611
379611
32084497
11/29/2015 01:48:58 AM
11/29/2015 02:25:59 AM
NYPD
New York City Police Department
Noise - Commercial
Loud Music/Party
Club/Bar/Restaurant
10461.0
...
NaN
NaN
NaN
NaN
NaN
NaN
40.852054
-73.829039
(40.85205433772875, -73.82903920932816)
2015-11-29 01:48:58
379612
379612
32084767
11/28/2015 02:42:15 AM
11/28/2015 03:52:18 AM
NYPD
New York City Police Department
Noise - Commercial
Loud Music/Party
Club/Bar/Restaurant
11249.0
...
NaN
NaN
NaN
NaN
NaN
NaN
40.712999
-73.966331
(40.712998877490364, -73.96633081126946)
2015-11-28 02:42:15
379613
379613
32084902
11/29/2015 12:16:03 AM
11/29/2015 02:49:09 AM
NYPD
New York City Police Department
Noise - Commercial
Loud Music/Party
Club/Bar/Restaurant
11377.0
...
NaN
NaN
NaN
NaN
NaN
NaN
40.743101
-73.897413
(40.743101024032335, -73.89741304204851)
2015-11-29 00:16:03
379614
379614
32085663
11/28/2015 06:40:43 PM
11/28/2015 07:43:30 PM
NYPD
New York City Police Department
Noise - Commercial
Banging/Pounding
Store/Commercial
10025.0
...
NaN
NaN
NaN
NaN
NaN
NaN
40.796753
-73.970727
(40.7967532483937, -73.9707272227454)
2015-11-28 18:40:43
379615
379615
32088358
11/29/2015 01:12:20 AM
11/29/2015 02:24:52 AM
NYPD
New York City Police Department
Noise - Commercial
Loud Music/Party
Club/Bar/Restaurant
10462.0
...
NaN
NaN
NaN
NaN
NaN
NaN
40.834331
-73.850485
(40.834331333190896, -73.85048531630817)
2015-11-29 01:12:20
379616
379616
32088590
11/28/2015 04:51:44 PM
11/28/2015 09:34:13 PM
NYPD
New York City Police Department
Noise - Commercial
Loud Talking
Store/Commercial
10036.0
...
NaN
NaN
NaN
NaN
NaN
NaN
40.761396
-73.995668
(40.76139645225346, -73.99566826833663)
2015-11-28 16:51:44
379617
379617
32090099
11/29/2015 09:31:25 AM
11/29/2015 04:37:30 PM
NYPD
New York City Police Department
Noise - Commercial
Loud Music/Party
Club/Bar/Restaurant
10002.0
...
NaN
NaN
NaN
NaN
NaN
NaN
40.714406
-73.990221
(40.714405958847486, -73.99022079542576)
2015-11-29 09:31:25
379618
379618
32090667
11/29/2015 04:27:22 PM
11/29/2015 05:51:14 PM
NYPD
New York City Police Department
Noise - Commercial
Loud Talking
Store/Commercial
10036.0
...
NaN
NaN
NaN
NaN
NaN
NaN
40.761396
-73.995668
(40.76139645225346, -73.99566826833663)
2015-11-29 16:27:22
379619
379619
32091325
11/29/2015 05:09:23 PM
11/29/2015 05:38:32 PM
NYPD
New York City Police Department
Noise - Commercial
Loud Music/Party
Store/Commercial
11203.0
...
NaN
NaN
NaN
NaN
NaN
NaN
40.663191
-73.937264
(40.66319106996423, -73.93726433523922)
2015-11-29 17:09:23
379620
379620
32091333
11/29/2015 05:16:42 PM
11/29/2015 06:08:06 PM
NYPD
New York City Police Department
Noise - Commercial
Loud Talking
Store/Commercial
10036.0
...
NaN
NaN
NaN
NaN
NaN
NaN
40.761396
-73.995668
(40.76139645225346, -73.99566826833663)
2015-11-29 17:16:42
379621
379621
32092452
11/29/2015 11:13:33 PM
11/30/2015 05:54:38 PM
NYPD
New York City Police Department
Noise - Commercial
Loud Music/Party
Club/Bar/Restaurant
10453.0
...
NaN
NaN
NaN
NaN
NaN
NaN
40.858337
-73.902654
(40.85833748317012, -73.90265423901148)
2015-11-29 23:13:33
379622
379622
32092697
11/29/2015 02:38:50 AM
11/29/2015 06:52:32 AM
NYPD
New York City Police Department
Noise - Commercial
Loud Music/Party
Club/Bar/Restaurant
10461.0
...
NaN
NaN
NaN
NaN
NaN
NaN
40.852054
-73.829039
(40.85205433772875, -73.82903920932816)
2015-11-29 02:38:50
379623
379623
32092760
11/29/2015 05:21:39 PM
11/29/2015 08:14:09 PM
NYPD
New York City Police Department
Noise - Commercial
Loud Talking
Store/Commercial
10036.0
...
NaN
NaN
NaN
NaN
NaN
NaN
40.761396
-73.995668
(40.76139645225346, -73.99566826833663)
2015-11-29 17:21:39
379624
379624
32099379
11/30/2015 04:43:53 PM
12/01/2015 12:31:09 AM
NYPD
New York City Police Department
Noise - Commercial
Loud Music/Party
Store/Commercial
10033.0
...
NaN
NaN
NaN
NaN
NaN
NaN
40.848978
-73.932936
(40.84897766372195, -73.93293601807679)
2015-11-30 16:43:53
379625
379625
32100541
11/30/2015 04:44:30 PM
12/01/2015 12:01:19 AM
NYPD
New York City Police Department
Noise - Commercial
Loud Talking
Store/Commercial
10036.0
...
NaN
NaN
NaN
NaN
NaN
NaN
40.761396
-73.995668
(40.76139645225346, -73.99566826833663)
2015-11-30 16:44:30
379626
379626
32102786
11/30/2015 04:25:34 PM
12/01/2015 12:01:20 AM
NYPD
New York City Police Department
Noise - Commercial
Loud Talking
Store/Commercial
10036.0
...
NaN
NaN
NaN
NaN
NaN
NaN
40.761396
-73.995668
(40.76139645225346, -73.99566826833663)
2015-11-30 16:25:34
379627
379627
32105933
12/01/2015 10:59:50 PM
12/02/2015 12:07:15 AM
NYPD
New York City Police Department
Noise - Commercial
Loud Music/Party
Store/Commercial
10018.0
...
NaN
NaN
NaN
NaN
NaN
NaN
40.756305
-73.993467
(40.75630484801116, -73.9934668041605)
2015-12-01 22:59:50
379628
379628
32106889
12/01/2015 09:47:24 PM
12/01/2015 10:50:19 PM
NYPD
New York City Police Department
Noise - Commercial
Loud Music/Party
Store/Commercial
10018.0
...
NaN
NaN
NaN
NaN
NaN
NaN
40.756305
-73.993467
(40.75630484801116, -73.9934668041605)
2015-12-01 21:47:24
379629
379629
32111969
12/02/2015 10:11:10 PM
12/03/2015 01:24:00 AM
NYPD
New York City Police Department
Noise - Commercial
Banging/Pounding
Club/Bar/Restaurant
10075.0
...
NaN
NaN
NaN
NaN
NaN
NaN
40.773958
-73.959613
(40.77395762310706, -73.95961342342461)
2015-12-02 22:11:10
379630
379630
32111972
12/02/2015 10:28:59 AM
12/02/2015 03:00:55 PM
NYPD
New York City Police Department
Noise - Commercial
Loud Music/Party
Store/Commercial
10013.0
...
NaN
NaN
NaN
NaN
NaN
NaN
40.717684
-74.000234
(40.717683618083214, -74.00023448157707)
2015-12-02 10:28:59
379631
379631
32111983
12/02/2015 09:44:51 PM
12/03/2015 01:22:53 AM
NYPD
New York City Police Department
Noise - Commercial
Banging/Pounding
Club/Bar/Restaurant
10075.0
...
NaN
NaN
NaN
NaN
NaN
NaN
40.773958
-73.959613
(40.77395762310706, -73.95961342342461)
2015-12-02 21:44:51
379632
379632
32115826
12/02/2015 05:22:40 PM
12/02/2015 05:59:27 PM
NYPD
New York City Police Department
Noise - Commercial
Loud Talking
Store/Commercial
10036.0
...
NaN
NaN
NaN
NaN
NaN
NaN
40.761396
-73.995668
(40.76139645225346, -73.99566826833663)
2015-12-02 17:22:40
379633
379633
32115912
12/02/2015 04:21:46 PM
12/02/2015 05:22:19 PM
NYPD
New York City Police Department
Noise - Commercial
Loud Talking
Store/Commercial
10036.0
...
NaN
NaN
NaN
NaN
NaN
NaN
40.761396
-73.995668
(40.76139645225346, -73.99566826833663)
2015-12-02 16:21:46
379634
379634
32117739
12/02/2015 05:13:03 PM
12/02/2015 05:58:23 PM
NYPD
New York City Police Department
Noise - Commercial
Loud Talking
Store/Commercial
10036.0
...
NaN
NaN
NaN
NaN
NaN
NaN
40.761396
-73.995668
(40.76139645225346, -73.99566826833663)
2015-12-02 17:13:03
379635
379635
32118859
12/02/2015 06:48:59 PM
12/02/2015 08:46:12 PM
NYPD
New York City Police Department
Noise - Commercial
Loud Music/Party
Club/Bar/Restaurant
11213.0
...
NaN
NaN
NaN
NaN
NaN
NaN
40.669981
-73.931000
(40.669980809733275, -73.93099996423969)
2015-12-02 18:48:59
379636
379636
32120904
12/03/2015 04:57:08 PM
12/03/2015 07:23:55 PM
NYPD
New York City Police Department
Noise - Commercial
Loud Talking
Store/Commercial
10036.0
...
NaN
NaN
NaN
NaN
NaN
NaN
40.761396
-73.995668
(40.76139645225346, -73.99566826833663)
2015-12-03 16:57:08
379637
379637
32121903
12/03/2015 05:22:06 PM
12/03/2015 07:22:49 PM
NYPD
New York City Police Department
Noise - Commercial
Loud Talking
Store/Commercial
10036.0
...
NaN
NaN
NaN
NaN
NaN
NaN
40.761396
-73.995668
(40.76139645225346, -73.99566826833663)
2015-12-03 17:22:06
379638
379638
32124016
12/03/2015 07:10:55 PM
12/03/2015 08:13:21 PM
NYPD
New York City Police Department
Noise - Commercial
Loud Music/Party
Store/Commercial
11213.0
...
NaN
NaN
NaN
NaN
NaN
NaN
40.669981
-73.931000
(40.669980809733275, -73.93099996423969)
2015-12-03 19:10:55
379639
379639
32125084
12/03/2015 04:56:01 PM
12/03/2015 07:23:55 PM
NYPD
New York City Police Department
Noise - Commercial
Loud Talking
Store/Commercial
10036.0
...
NaN
NaN
NaN
NaN
NaN
NaN
40.761396
-73.995668
(40.76139645225346, -73.99566826833663)
2015-12-03 16:56:01
379640 rows × 55 columns
In [4]:
def between_dates(start_date, end_date):
mask = (housing['Created Datetime'] > start_date) & (housing['Created Datetime'] <= end_date)
return housing.loc[mask]
def between_hours(start_date, end_date):
start_time = str(start_date.time())
end_time = str(end_date.time())
return housing.iloc[created_index.indexer_between_time(start_time,end_time)]
# between_dates(datetime(2015,1,1), datetime(2015,1,2))
# between_hours(datetime(2015,1,1,0), datetime(2015,1,1,1))
In [5]:
start = datetime(2015,1,1)
one_hour = timedelta(hours=1)
one_day = timedelta(days=1)
In [6]:
def daily_complaints(year=2015):
start = datetime(year, 1, 1)
# dc = {i:[] for i in range(1,13)}
dc = []
next_day = lambda starttime : len(between_dates(start, start+one_day))
if start.year % 4 == 0:
days_in_year = 366
else:
days_in_year = 365
for i in range(days_in_year):
dc.append(next_day(start))
# dc[start.month].append(next_day(start))
start = start + one_day
return dc
# len(between_dates(start, start+one_hour))
def hourly_complaints(hours=24):
start = datetime(2015, 1, 1)
if hours > 24:
hours = 24
hc = []
next_hour = lambda starttime : len(between_hours(start, start + one_hour))
for i in range(hours):
hc.append(next_hour(start))
start = start + one_hour
return hc
In [7]:
start.year % 4
Out[7]:
3
In [8]:
dc = daily_complaints(year=2015)
In [ ]:
# combine = reduce(lambda x,y : x.extend(y), dc.values())
y = np.array(dc)
x = np.arange(len(dc))
x = x.reshape(-1,1)
X = np.zeros([365,2])
X[:,:-1] = x
for i in range(len(X)):
X[i,1] = i % 7
In [ ]:
import matplotlib.pyplot as plt
from sklearn.svm import SVR
from sklearn.tree import DecisionTreeRegressor
svr_lin = SVR(kernel='linear', C=1e3)
svr_poly = SVR(kernel='poly', C=1e3, degree=3)
svr_rbf = SVR(kernel='rbf', C=200, gamma=0.05)
y_rbf = svr_rbf.fit(X[:250], y[:250]).predict(X[250:])
y_lin = svr_lin.fit(X[:250], y[:250]).predict(X[250:])
y_poly = svr_poly.fit(X[:250], y[:250]).predict(X[250:])
In [ ]:
lw = 2
plt.scatter(x, y, color='darkorange', label='data')
plt.plot(range(250, 365), y_rbf, color='navy', lw=lw, label='RBF model')
plt.plot(range(250, 365), y_lin, color='c', lw=lw, label='Linear model')
plt.plot(range(250, 365), y_poly, color='teal', lw=lw, label='Polynomial model')
plt.xlabel('Day of year')
plt.ylabel('Number of complaints')
plt.title('Support Vector Regression')
plt.legend()
plt.show()
In [ ]:
clf = linear_model.LogisticRegression(C=1e3,intercept_scaling=30)
y_clf = clf.fit(x, y).predict(np.array(range(100,450)).reshape(-1,1))
plt.scatter(x, y, color='darkorange', label='data')
plt.plot(range(100,450), y_clf, color='navy', lw=lw, label='RBF model')
In [ ]:
import json
with open('{}-complaints.json'.format(cat), 'w') as fp:
json.dump(dc, fp)
In [ ]:
# hc = hourly_complaints()
Content source: arvind-iyer/socdata
Similar notebooks: