-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
378 lines (334 loc) · 10.9 KB
/
main.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
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
import numpy as np
import pandas as pd
import altair as alt
aisles = pd.read_csv('aisles.csv')
departments = pd.read_csv('departments.csv')
order_products_prior = pd.read_csv('order_products__prior.csv')
order_products_train = pd.read_csv('order_products__train.csv')
orders = pd.read_csv('orders.csv')
products = pd.read_csv('products.csv')
# Exploring data sets
aisles.head()
departments.head()
orders.head()
products.head()
order_products_prior.head()
order_products_train.head()
# Concating order prior and train
order_products = pd.concat([order_products_prior,order_products_train])
order_products.shape
# Creating test, trian, prior set
test = orders[orders['eval_set'] == 'test']
test.head()
train = orders[orders['eval_set'] == 'train']
train.head()
prior = orders[orders['eval_set'] == 'prior']
prior.head()
# Creating final data set
data = pd.merge(orders, order_products, on='order_id')
data.head()
data = pd.merge(data, products, on='product_id')
data.head()
data = pd.merge(data, aisles, on='aisle_id')
data = pd.merge(data, departments, on='department_id')
data.head()
# Explanatory data analysis
from altair import pipe, limit_rows, to_values
t = lambda data: pipe(data, limit_rows(max_rows=1000000000), to_values)
alt.data_transformers.register('custom', t)
alt.data_transformers.enable('custom')
#How many orders, items, and users are in each eval set?
count = pd.DataFrame(orders.groupby('eval_set')['order_number'].count())
count['i'] = count.index
count
n = alt.Chart(count, height = 350, width = 450,title = 'Orders as per Evaluation Set').mark_bar().encode(
x='i',
y='order_number',
color = alt.Color('i', legend=None)
).configure_axis(
labelFontSize=16,
titleFontSize=16,
grid = False
).configure_title(fontSize=24)
n.encoding.y.title = 'Number of Orders'
n.encoding.x.title = 'Evalution set'
n
count = pd.DataFrame(data.groupby('eval_set')['order_number'].count())
count['i'] = count.index
count
n = alt.Chart(count, height = 350, width = 450,title = 'Orders as per Evaluation Set').mark_bar().encode(
x='i',
y='order_number',
color = alt.Color('i', legend=None)
).configure_axis(
labelFontSize=16,
titleFontSize=16,
grid = False
).configure_title(fontSize=24)
n.encoding.y.title = 'Number of Orders'
n.encoding.x.title = 'Evalution set'
n
count = pd.DataFrame(orders.groupby('eval_set')['user_id'].nunique())
count['i'] = count.index
count
n = alt.Chart(count, height = 350, width = 450,title = 'Number of Users').mark_bar().encode(
x='i',
y='user_id',
color = alt.Color('i', legend=None)
).configure_axis(
labelFontSize=16,
titleFontSize=16,
grid = False
).configure_title(fontSize=24)
n.encoding.y.title = 'Users'
n.encoding.x.title = 'Evalution set'
n
#How many users in prior appear in train? How many users in prior or train appear in test?
len(pd.Series(list(set(prior['user_id']) & set(test['user_id']))))
len(pd.Series(list(set(prior['user_id']) & set(train['user_id']))))
len(pd.Series(list(set(train['user_id']) & set(test['user_id']))))
#When are orders made?
count = pd.DataFrame(orders.groupby('order_dow')['order_number'].count())
count['i'] = count.index + 1
count
n = alt.Chart(count, height = 350, width = 350,title = 'Number of Orders on Each day of Week').mark_bar(size=25).encode(
x='i',
y='order_number'
).configure_axis(
labelFontSize=16,
titleFontSize=16,
grid = False
).configure_title(fontSize=24)
n.encoding.y.title = 'Number of order'
n.encoding.x.title = 'Day of the Week'
n
count = pd.DataFrame(orders.groupby('order_hour_of_day')['order_number'].count())
count['i'] = count.index + 1
count.head()
n = alt.Chart(count, height = 350, width = 750,title = 'Number of Orders on Each day of Week').mark_bar(size=20).encode(
x='i',
y='order_number'
).configure_axis(
labelFontSize=16,
titleFontSize=16,
grid = False
).configure_title(fontSize=24)
n.encoding.y.title = 'Number of order'
n.encoding.x.title = 'Day of the Week'
n
count = pd.DataFrame(orders.groupby('days_since_prior_order')['order_number'].count())
count['i'] = count.index + 1
count.head()
alt.Chart(count, width = 750, height = 350).mark_bar(size = 20).encode(
alt.X("i", title = 'Date'),
alt.Y("order_number", title='Number of Order')
).configure_axis(
labelFontSize=16,
titleFontSize=16,
grid = False
).configure_title(fontSize = 24)
hourDow = orders.groupby(['order_dow', 'order_hour_of_day'])['order_number'].aggregate('count').reset_index()
hourDow
alt.Chart(hourDow, title='No of order day of week vs hour of day').mark_rect().encode(
x = alt.X('order_hour_of_day:O', title='Hour of the Day'),
y = alt.Y('order_dow:O', title='Day of Week'),
color = 'order_number:Q'
).configure_axis(
labelFontSize=16,
titleFontSize=16
).configure_title(
fontSize = 24
).properties(
height = 450,
width = 450
)
#Count of Users by Number of Prior Orders
count = pd.DataFrame(orders.groupby('user_id')['order_number'].aggregate('max').reset_index())
count
new = pd.DataFrame(count['order_number'].value_counts())
new['i'] = new.index
new.head()
alt.Chart(new, title='Count of users by number of prior orders').mark_bar().encode(
alt.X('i', title = 'Number of Prior Orders'),
alt.Y('order_number', title = 'Number of Users')
).configure_axis(
labelFontSize = 16,
titleFontSize = 16,
grid = False
).configure_title(
fontSize = 24
).properties(
height = 400,
width = 700
).configure_bar(
color = '#7AAD80'
)
# Distribution of basket size
OrderSize = data.reset_index().groupby(['user_id', 'order_number'])['add_to_cart_order'].aggregate('max')
new = pd.DataFrame(OrderSize.value_counts())
new['i'] = new.index
new
alt.Chart(new, title='Count of users by number of Items in Basket').mark_bar(size = 4).encode(
alt.X('i', title = 'Number of Items in Basket'),
alt.Y('add_to_cart_order', title = 'Number of Users')
).configure_axis(
labelFontSize = 16,
titleFontSize = 16,
grid = False
).configure_title(
fontSize = 24
).properties(
height = 350,
width = 750
).configure_bar(
color = '#FC5959'
)
# Popular Products
countProducts = pd.crosstab(data.product_name, data.eval_set)
countProducts['total'] = countProducts.prior + countProducts.train
count = countProducts.sort_values(by='total', ascending=False).head(25)
count['i'] = count.index
count.head(10)
m = alt.Chart(count, title="Count of Order of prior 25 Most Ordered Product").mark_bar(size = 7).encode(
alt.X('i', title = 'Product Name'),
alt.Y('prior', title = 'Count of Orders')
).properties(
height = 350,
width = 350
)
n = alt.Chart(count, title="Count of Order of train 25 Most Ordered Product").mark_bar(size = 7).encode(
alt.X('i', title = 'Product Name'),
alt.Y('train', title = 'Count of Orders')
).properties(
height = 350,
width = 350
)
(m|n).configure_axis(
labelFontSize = 14,
titleFontSize = 14,
grid = False
).configure_title(
fontSize = 16
)
countAisles = pd.crosstab(data.aisle, data.eval_set)
countAisles['total'] = countAisles.prior + countAisles.train
count = countAisles.sort_values(by='total', ascending=False).head(15)
count['i'] = count.index
count.head()
m = alt.Chart(count, title="Count of Order of prior 15 Most Ordered from Aisles").mark_bar(size = 10).encode(
alt.X('i', title = 'Product Name'),
alt.Y('prior', title = 'Count of Orders')
).properties(
height = 350,
width = 350
)
n = alt.Chart(count, title="Count of Order of train 15 Most Ordered from Aisles").mark_bar(size = 10).encode(
alt.X('i', title = 'Product Name'),
alt.Y('train', title = 'Count of Orders')
).properties(
height = 350,
width = 350
)
(m|n).configure_axis(
labelFontSize = 14,
titleFontSize = 14,
grid = False
).configure_title(
fontSize = 15
)
Ratio of Departments
countDepartment = pd.crosstab(data.department, data.eval_set)
countDepartment['total'] = countDepartment.prior + countDepartment.train
countDepartment['%'] = countDepartment.total / countDepartment.total.sum()
count = countDepartment.sort_values(by='total', ascending=False)
count['i'] = count.index
count.head()
p = alt.Chart(count, title='Proportion of Items Sold from Each Department').mark_point().encode(
x=alt.X('i', title='Department Name'),
y=alt.Y('%', title='Total Percentage')
)
t = p.mark_text(
align='left',
baseline='middle',
dy=7,
fontSize = 12
).encode(
text='i'
)
(p+t).configure_axis(
labelFontSize = 14,
titleFontSize = 14,
grid = False
).configure_title(
fontSize = 15
).properties(
height = 600,
width = 600
)
# Reorder ratio by departement
count = pd.DataFrame(data.groupby('department')['reordered'].mean())
count['i'] = count.index
count.head()
alt.Chart(count).mark_bar(size = 25).encode(
alt.X('i', title = 'Department'),
alt.Y('reordered', title = 'Ratio')
).configure_axis(
labelFontSize = 16,
titleFontSize = 16,
grid = False
).configure_title(
fontSize = 24
).properties(
height = 350,
width = 150
)
#Reorder ratio by the hour of the day or the day of the week?
count = data.groupby(['order_dow'])['reordered'].aggregate('mean').reset_index()
count
alt.Chart(count, title='Reorder proportion on Each day of Week').mark_bar(size = 25).encode(
alt.X('order_dow', title = 'Day of week'),
alt.Y('reordered', title = 'Reorder Proportion')
).configure_axis(
labelFontSize = 16,
titleFontSize = 16,
grid = False
).configure_title(
fontSize = 24
).properties(
height = 350,
width = 300
).configure_bar(
color = '#9CABFF'
)
count = data.groupby(['order_hour_of_day'])['reordered'].aggregate('mean').reset_index()
count.head()
alt.Chart(count, title='Reorder proportion on Each Hour of day').mark_bar(size = 20).encode(
alt.X('order_hour_of_day', title = 'Order of day'),
alt.Y('reordered', title = 'Reorder Proportion')
).configure_axis(
labelFontSize = 16,
titleFontSize = 16,
grid = False
).configure_title(
fontSize = 24
).properties(
height = 350,
width = 650
).configure_bar(
color = '#E2A4FF'
)
reorderHourDow = data.groupby(['order_dow', 'order_hour_of_day'])['reordered'].aggregate('mean').reset_index()
reorderHourDow
alt.Chart(reorderHourDow, title='Reorder Proportion by Day of Week vs Hour of Day').mark_rect().encode(
x = alt.X('order_hour_of_day:O', title='Hour of the Day'),
y = alt.Y('order_dow:O', title='Day of Week'),
color=alt.Color('reordered:Q',scale=alt.Scale(scheme='greenblue'))
).configure_axis(
labelFontSize=16,
titleFontSize=16
).configure_title(
fontSize = 24
).properties(
height = 450,
width=150
)