-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path04_transform_data.py
executable file
·222 lines (186 loc) · 6.14 KB
/
04_transform_data.py
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
#!/usr/bin/env python3
import haversine
import numpy as np
import pandas as pd
import seaborn as sns
import datetime as dt
import plotly.express as px
#%% Import data ----------------------------------------------------------------
bikes = pd.read_csv(
'data/divvy_bikes_2013-2017.csv',
parse_dates = ['datetime'],
infer_datetime_format = True
)
weather = pd.read_csv(
'data/chicago_weather_2013-2017.csv',
parse_dates = ['datetime'],
infer_datetime_format = True,
usecols = [
'datetime',
'temperature',
'humidity',
'cloudiness',
'wind_speed',
'rain_1h',
'snow_1h'
]
)
#%% Scale available bikes ------------------------------------------------------
bikes['available_bikes'] = (bikes['available_bikes'] / bikes['capacity']).round(6)
#%% Plot available bikes distribution ------------------------------------------
sns.stripplot(
data = bikes[['available_bikes']].sample(10000),
size = 0.5,
orient = 'h'
)
#%% Normalize station coordinates ----------------------------------------------
locations = bikes[
['proxy_latitude', 'proxy_longitude']
].drop_duplicates().reset_index(drop = True)
# Project latitudes and longitudes to Cartesian coordinates
locations['x_coordinate'] = (
np.cos(locations['proxy_latitude']) *
np.cos(locations['proxy_longitude'])
)
locations['y_coordinate'] = (
np.cos(locations['proxy_latitude']) *
np.sin(locations['proxy_longitude'])
)
locations['z_coordinate'] = - np.sin(locations['proxy_latitude'])
# Add distance to dowtown
madison_state_intersection = (41.882045, -87.627823)
locations['downtown_distance'] = haversine.haversine_vector(
[madison_state_intersection] * len(locations),
locations[['proxy_latitude','proxy_longitude']],
haversine.Unit.KILOMETERS
)
# Z-score normalize coordinates
locations['x_coordinate'] = (locations['x_coordinate'] + 0.47) / 0.05
locations['y_coordinate'] = (locations['y_coordinate'] + 0.15) / 0.03
locations['z_coordinate'] = (locations['z_coordinate'] - 0.87) / 0.03
# Scale distance
locations['downtown_distance'] /= 20
bikes = bikes.merge(
locations.round(6),
on = ['proxy_latitude', 'proxy_longitude'],
how = 'left'
)
#%% Plot station coordinates ---------------------------------------------------
px.scatter_3d(
locations,
x = 'x_coordinate',
y = 'y_coordinate',
z = 'z_coordinate',
color = 'downtown_distance',
).update_traces(marker_size = 2)
#%% Transform datetimes --------------------------------------------------------
datetimes = bikes[['datetime']].drop_duplicates().reset_index(drop = True)
# Transform hours and days
day_hour = datetimes['datetime'].dt.hour
datetimes['sin_day_hour'] = np.sin(2 * np.pi * day_hour / 24 - np.pi)
datetimes['cos_day_hour'] = np.cos(2 * np.pi * day_hour / 24 - np.pi)
year_day = datetimes['datetime'].dt.dayofyear
datetimes['sin_year_day'] = np.sin(2 * np.pi * year_day / 365 - np.pi)
datetimes['cos_year_day'] = np.cos(2 * np.pi * year_day / 365 - np.pi)
week_hour = 24 * datetimes['datetime'].dt.dayofweek + datetimes['datetime'].dt.hour
datetimes['sin_week_hour'] = np.sin(2 * np.pi * week_hour / (24 * 7) - np.pi)
datetimes['cos_week_hour'] = np.cos(2 * np.pi * week_hour / (24 * 7) - np.pi)
# Add a workday feature
holidays = [
(dt.datetime(year, 1, 1) + offset).date()
for year in range(
datetimes['datetime'].min().year,
datetimes['datetime'].max().year + 1
)
for offset in [
# New year on 1st of January
pd.tseries.offsets.DateOffset(months = 0, days = 0),
# MLK day on 3rd Monday of January
pd.tseries.offsets.DateOffset(months = 0, weeks = 2, weekday = 0),
# President day on 3rd Monday of February
pd.tseries.offsets.DateOffset(months = 1, weeks = 2, weekday = 0),
# Memorial day on last Monday of May
pd.tseries.offsets.LastWeekOfMonth(5, weekday = 0),
# Independence day on 4th of July
pd.tseries.offsets.DateOffset(months = 6, days = 3),
# Labor day on 1st Monday of September
pd.tseries.offsets.DateOffset(months = 8, weeks = 0, weekday = 0),
# Columbus day on 2nd Monday of October
pd.tseries.offsets.DateOffset(months = 9, weeks = 1, weekday = 0),
# Veterans day on 11th of November
pd.tseries.offsets.DateOffset(months = 10, days = 10),
# Thanksgiving on 4th Thursday of November
pd.tseries.offsets.DateOffset(months = 10, weeks = 3, weekday = 3),
# Christmas on 25th of December
pd.tseries.offsets.DateOffset(months = 11, days = 24)
]
]
datetimes['workday'] = np.where(
datetimes['datetime'].dt.date.isin(holidays) |
datetimes['datetime'].dt.dayofweek.isin([5, 6]),
0,
1
)
bikes = bikes.merge(datetimes.round(6), on = 'datetime', how = 'left')
#%% Plot time transformations --------------------------------------------------
x = np.arange(0, 24, 0.1)
sin_day_hour = np.sin(2 * np.pi * x / 24 - np.pi)
cos_day_hour = np.cos(2 * np.pi * x / 24 - np.pi)
sns.lineplot(
data = pd.DataFrame({
'hour': x,
'sin_day_hour': sin_day_hour,
'cos_day_hour': cos_day_hour,
'day_hour': (np.arctan2(sin_day_hour, cos_day_hour) + np.pi) / (2 * np.pi)
}).set_index('hour'),
)
#%% Scale weather --------------------------------------------------------------
weather['temperature'] += 25
weather['temperature'] /= 60
weather['humidity'] /= 100
weather['cloudiness'] /= 100
weather['wind_speed'] /= 20
weather['rain_1h'] /= 30
weather['snow_1h'] /= 10
bikes = bikes.merge(weather.round(6), on = 'datetime', how = 'left')
#%% Plot weather distributions -------------------------------------------------
sns.violinplot(
data = weather[[
'temperature',
'humidity',
'cloudiness',
'wind_speed',
'rain_1h',
'snow_1h'
]],
inner = 'quartile',
scale = 'width',
orient = 'h',
cut = 0
)
#%% Output transformed data ----------------------------------------------------
bikes[[
'datetime',
'available_bikes',
'x_coordinate',
'y_coordinate',
'z_coordinate',
'downtown_distance',
'sin_year_day',
'cos_year_day',
'sin_week_hour',
'cos_week_hour',
'sin_day_hour',
'cos_day_hour',
'workday',
'temperature',
'humidity',
'cloudiness',
'wind_speed',
'rain_1h',
'snow_1h'
]].to_csv(
'data/divvy_bikes_chicago_weather_2013-2017_transformed.csv',
index = False
)
print(bikes.info())