This repository has been archived by the owner on Jan 9, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
gym-crowdedness.html
355 lines (293 loc) · 9.15 KB
/
gym-crowdedness.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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Gym Crowdedness - iodide</title>
<link rel="stylesheet" type="text/css" href="https://iodide.io/stable/iodide.stable.css">
</head>
<body>
<script id="jsmd" type="text/jsmd">
%% meta
{
"title": "Gym Crowdedness",
"lastSaved": "2018-03-27T03:08:12.605Z",
"lastExport": "2018-05-10T00:55:33.152Z"
}
%% md
# Exploring the Campus Gym Crowdedness Dataset <br/>
## Data
Each row in the dataset represents how many people were in the campus gym at a given time. Measurements were taken every 10 minutes at the campus gym over the course of a year. Other feature columns such as temperature and day of week have been added to the dataset.
Source:
<a href="https://www.kaggle.com/nsrose7224/crowdedness-at-the-campus-gym">Kaggle - Crowdedness at the Campus Gym</a>
## Purpose
The purpose of this notebook is to explore and analyze the Campus Gym Crowdedness Dataset for correlations between the number of people in the campus gym and other features such as temperature and time.
## Libraries Used
- D3.js
- plotly.js
- jStat
## Import and explore csv data using d3.js
Import d3
%% resource
https://cdnjs.cloudflare.com/ajax/libs/d3/4.10.2/d3.js
%% md
Import the data using D3
%% js
const URL = "https://gist.githubusercontent.com/jandrewtorres/0e40e6b3368af619a2287c1fa6508483/raw/a0661154081f625e6a74f672dd55db62a9dd8a5a/gym_crowdedness.csv"
// the columns in the dataset will be needed later for calculations
var columns = {}
var getData = function(url) {
var re = new XMLHttpRequest()
re.open('GET', url, false)
re.send(null)
return re.response
}
var data = d3.csvParse(getData(URL))
// configure columns
Object.keys(data[0]).forEach((key) =>{
columns[key] = new Array()
})
data.forEach((row) => {
Object.keys(columns).forEach((key) => {
columns[key].push(Number(row[key]))
})
})
%% md
Verify data imported successfully and preview dataset
%% js
data
%% md
Verify columns has been configured correctly
%% js
columns
%% md
## Wrangle the data and calculate correlation coefficients between features
%% md
We want to explore the correlations between the different columns. To do so, we will need to calculate the correlation coefficients using jStat.corrcoeff() which accepts two feature columns as parameters and returns their Pearson Correlation Coefficient in the range [-1, 1]
%% resource
https://cdnjs.cloudflare.com/ajax/libs/jstat/1.7.1/jstat.min.js
%% md
Notice that the 'date' and 'timestamp' feature columns are continuous and will not give us meaningful correlations. Let's delete them.
%% js
delete columns['date']
delete columns['timestamp']
%% md
We are now ready to calculate the correlation coefficients for all column pairs
%% js
var correlationCoefficients = {}
Object.keys(columns).forEach(function(xColName) {
var xColumn = columns[xColName]
console.log(xColumn)
correlationCoefficients[xColName] = new Array()
Object.keys(columns).forEach(function(yColName) {
var yColumn = columns[yColName]
correlationCoefficients[xColName].push(jStat.corrcoeff(xColumn, yColumn))
})
})
%% js
correlationCoefficients
%% md
## Visualize correlations using plotly.js heatmap
%% md
<div id="plot"></div>
%% resource
https://cdn.plot.ly/plotly-latest.min.js
%% md
Plot the correlations in a heatmap
%% js
var zValues = new Array()
Object.keys(correlationCoefficients).forEach(function(ccKey) {
zValues.push(correlationCoefficients[ccKey])
})
var heatmapData = [
{
z: zValues,
x: Object.keys(correlationCoefficients),
y: Object.keys(correlationCoefficients),
colorscale: 'RdBu',
type: 'heatmap',
zmin: -1,
zmax: 1,
}
]
var layout = {
title: 'Gym Crowdedness Correlations',
margin: {
l: 200,
},
};
function plotHeatmap() {
Plotly.newPlot('plot', heatmapData, layout, { displayModeBar: false })
}
plotHeatmap()
%% md
## Heatmap / Correlation Coefficients Conclusions
There is a high positive correlation between number of people and:
- hour
- temperature
- is during semester
- is start of semester
There is a high negative correlation between number of people and:
- is weekend
- day of week
Let's create visualizations of these correlations to explore them further.
## Group by feature and find mean number of people for groups
%% js
// Hour
var byHour = d3.nest()
.key(function(d) { return d.hour })
.rollup(function(d) {
return d3.mean(d, function(g) { return g.number_people })
})
.entries(data)
.sort(function(a,b) {
return d3.descending(parseInt(a.key), parseInt(b.key))
})
// Temperature
function roundUp(value) {
return value + (2.5 - value % 2.5)
}
var byTemperature = d3.nest()
.key(function(d) {
var temperature = d.temperature
return roundUp(parseInt(d.temperature))
})
.rollup(function(d) {
return d3.mean(d, function(g) { return g.number_people })
})
.entries(data)
.sort(function(a,b) {
return d3.descending(parseInt(a.key),parseInt(b.key))
})
// is_start
var byStartOfSemester = d3.nest()
.key(function(d) { return d.is_start_of_semester })
.rollup(function(d) {
return d3.mean(d, function(g) { return g.number_people })
}).entries(data)
// is_during
var byDuringSemester = d3.nest()
.key(function(d) { return d.is_during_semester })
.rollup(function(d) {
return d3.mean(d, function(g) { return g.number_people })
}).entries(data)
// is_weekend
var byIsWeekend = d3.nest()
.key(function(d) { return d.is_weekend })
.rollup(function(d) {
return d3.mean(d, function(g) { return g.number_people })
}).entries(data)
// day of week
var byDayOfWeek = d3.nest()
.key(function(d) { return d.day_of_week })
.rollup(function(d) {
return d3.mean(d, function(g) { return g.number_people })
})
.entries(data)
.sort(function(a, b) {
return d3.ascending(parseInt(a.key), parseInt(b.key))
})
%% md
## Feature groups vs mean number of people graphs
<div id="subplot-wrapper">
<div id="byTemp"></div>
<div id="byDay"></div>
<div id="byHour"></div>
<div id="byStartOfSemester"></div>
<div id="byDuringSemester"></div>
<div id="byIsWeekend"></div>
</div>
%% md
CSS subplot wrapper div styling
%% css
#subplot-wrapper {
display: flex;
flex-flow: row wrap;
}
%% md
Plot the data in line and bar charts
%% js
var byTemperatureTrace = {
x: Object.keys(byTemperature).map(function(key, index) {
return byTemperature[key].key
}),
y: Object.keys(byTemperature).map(function(key, index) {
return byTemperature[key].value
}),
type: 'scatter'
}
var byDayOfWeekTrace = {
x: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
y: Object.keys(byDayOfWeek).map(function(key, index) {
return byDayOfWeek[key].value
}),
type: 'scatter'
}
var byHourTrace = {
x: Object.keys(byHour).map(function(key, index) {
return key
}),
y: Object.keys(byHour).map(function(key, index) {
return byHour[key].value
}),
type: 'scatter'
}
var byStartOfSemesterTrace = {
x: ["No", "Yes"],
y: Object.keys(byStartOfSemester).map(function(key, index) {
return byStartOfSemester[key].value
}),
type: 'bar'
}
var byDuringSemesterTrace = {
x: ["No", "Yes"],
y: Object.keys(byDuringSemester).map(function(key, index) {
return byDuringSemester[key].value
}),
type: 'bar'
}
var byIsWeekendTrace = {
x: ["No", "Yes"],
y: Object.keys(byIsWeekend).map(function(key, index) {
return byIsWeekend[key].value
}),
type: 'bar'
}
function plotSubPlot(divId, data, title, xaxisTitle, yaxisTitle) {
Plotly.newPlot(divId, data, {
title: title,
xaxis: {
title: xaxisTitle,
},
yaxis: {
title: yaxisTitle,
},
autosize: false,
width: 300,
height: 250,
margin: { l:50, r:0, b:50, t:50 },
}, { displayModeBar: false })
}
plotSubPlot('byTemp', [byTemperatureTrace], 'Temperature vs Num People', 'Temperature (F)', 'Num People')
plotSubPlot('byDay', [byDayOfWeekTrace], 'Day of Week vs Num People', 'Day of Week', 'Num People')
plotSubPlot('byHour', [byHourTrace], 'Hour of Day vs Num People', 'Hour', 'Num People')
plotSubPlot('byStartOfSemester', [byStartOfSemesterTrace], 'Is Start of Semester vs Num People', 'Is Start of Semester', 'Num People')
plotSubPlot('byDuringSemester', [byDuringSemesterTrace], 'Is During Semester vs Num People', 'Is During Semster', 'NumPeople')
plotSubPlot('byIsWeekend', [byIsWeekendTrace], 'Is Weekend vs Num People', 'Is Weekend', 'Num People')
%% md
## Analysis of feature groups vs mean number of people graphs
- More people go to the gym later in the day
- There are more people in the gym when it is mild temperature outside, BUT the time of day and temperature have a relationship that is likely affecting this relationship
- More people are in the gym at the start of the semester than the middle of the semester
- More people are in the gym during the semester than not during the semester
- More people are in the gym during the week than weekend
- The average number of people in the gym steadily declines from Monday to Sunday
%% md
__original author__: John A. Torres (jandrewtorres) <br/>
__contributors__: jandrewtorres <br/>
__maintainer__: jandrewtorres <br/>
__submission date__: 5/9/18 <br/>
</script>
<div id='page'></div>
<script src='https://iodide.io/stable/iodide.stable.js'></script>
</body>
</html>