-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathQuantium - Module 1.py
612 lines (333 loc) · 18.7 KB
/
Quantium - Module 1.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
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
#!/usr/bin/env python
# coding: utf-8
# # Module 1
# We need to present a strategic recommendation to Julia that is supported by data which she can then use for the upcoming category review however to do so we need to analyse the data to understand the current purchasing trends and behaviours. The client is particularly interested in customer segments and their chip purchasing behaviour. Consider what metrics would help describe the customers’ purchasing behaviour.
# In[362]:
import pandas as pd
import matplotlib.pyplot as plt
get_ipython().run_line_magic('matplotlib', 'inline')
import numpy as np
# In[363]:
pur_bhvr = pd.read_csv("QVI_purchase_behaviour.csv")
print(pur_bhvr.head())
# In[364]:
tran_data = pd.read_excel("QVI_transaction_data.xlsx")
print(tran_data.head())
# In[365]:
merged_data = pd.merge(pur_bhvr, tran_data, on="LYLTY_CARD_NBR", how="right")
print(merged_data.head())
# In[366]:
print(len(merged_data))
print(len(tran_data))
# In[367]:
merged_data.info()
# Date column should be in datetime format
# In[368]:
from datetime import date, timedelta
start = date(1899,12,30)
new_date_format = []
for date in merged_data["DATE"]:
delta = timedelta(date)
new_date_format.append(start + delta)
# In[369]:
merged_data["DATE"] = pd.to_datetime(pd.Series(new_date_format))
print(merged_data["DATE"].dtype)
# Next, checking the prod_name column to make sure all items are chips
# In[370]:
merged_data["PROD_NAME"].unique()
# In[371]:
split_prods = merged_data["PROD_NAME"].str.replace(r'([0-9]+[gG])','').str.replace(r'[^\w]', ' ').str.split()
# In[372]:
word_counts = {}
def count_words(line):
for word in line:
if word not in word_counts:
word_counts[word] = 1
else:
word_counts[word] += 1
split_prods.apply(lambda line: count_words(line))
print(pd.Series(word_counts).sort_values(ascending=False))
# Removing Salsa products
# In[373]:
merged_data = merged_data[~merged_data["PROD_NAME"].str.contains(r"[Ss]alsa")]
# In[374]:
print(merged_data.describe(), '\n')
print(merged_data.info())
# In[375]:
merged_data["PROD_QTY"].value_counts(bins=4).sort_index()
# From the binning above we see that PROD_QTY values above 50.75
# In[376]:
merged_data.sort_values(by="PROD_QTY", ascending=False).head()
# 2 outliers (value = 200) in PROD_QTY will be removed. Both entries are by the same customer, we will examine this customer's other transactions.
# In[377]:
merged_data = merged_data[merged_data["PROD_QTY"] < 6]
# In[378]:
len(merged_data[merged_data["LYLTY_CARD_NBR"] == 226000])
# In[379]:
merged_data["DATE"].describe()
# Instead of 365, the DATE column only has 364 unique values. 1 is missing.
# In[380]:
pd.date_range(start=merged_data["DATE"].min(), end=merged_data["DATE"].max()).difference(merged_data["DATE"])
# Using the difference method we see that 2018-12-25 was the missing date.
# In[381]:
check_null_date = pd.merge(pd.Series(pd.date_range(start=merged_data["DATE"].min(), end=merged_data["DATE"].max()), name="DATE"), merged_data, on="DATE", how="left")
# In[469]:
trans_by_date = check_null_date["DATE"].value_counts()
dec = trans_by_date[(trans_by_date.index >= pd.datetime(2018,12,1)) & (trans_by_date.index < pd.datetime(2019,1,1))].sort_index()
dec.index = dec.index.strftime('%d')
ax = dec.plot(figsize=(15,3))
ax.set_xticks(np.arange(len(dec)))
ax.set_xticklabels(dec.index)
plt.title("2018 December Sales")
plt.savefig("2018 December Sales.png", bbox_inches="tight")
plt.show()
# In[383]:
check_null_date["DATE"].value_counts().sort_values().head()
# The date with the no transaction falls on Christmas day, the day when the store is closed. Knowing there's no anomaly in this, we leave it be.
# Next, we'll explore product pack sizes
# In[384]:
merged_data["PROD_NAME"] = merged_data["PROD_NAME"].str.replace(r'[0-9]+(G)','g')
pack_sizes = merged_data["PROD_NAME"].str.extract(r'([0-9]+[gG])')[0].str.replace("g","").astype("float")
print(pack_sizes.describe())
pack_sizes.plot.hist()
# Product pack size looks reasonable with highest transaction frequency in mid-sized pack. Smallest size is 70g, and biggest size is 380g.
# Next, we will explore the product brand names
# In[385]:
merged_data["PROD_NAME"].str.split().str[0].value_counts().sort_index()
# As we look further than the first word in product name, we can see that some product brands are written in more than 1 way. Dorito and Doritos. Grain and GrnWves. Infuzions and Infzns. Natural and NCC. Red and RRD. Smith and Smiths. Snbts and Sunbites. WW and Woolworths.
# In[386]:
merged_data["PROD_NAME"].str.split()[merged_data["PROD_NAME"].str.split().str[0] == "Grain"].value_counts()
# In[387]:
merged_data["PROD_NAME"].str.split()[merged_data["PROD_NAME"].str.split().str[0] == "Natural"].value_counts()
# In[388]:
merged_data["PROD_NAME"].str.split()[merged_data["PROD_NAME"].str.split().str[0] == "Red"].value_counts()
# In[389]:
merged_data["Cleaned_Brand_Names"] = merged_data["PROD_NAME"].str.split().str[0]
# In[390]:
def clean_brand_names(line):
brand = line["Cleaned_Brand_Names"]
if brand == "Dorito":
return "Doritos"
elif brand == "GrnWves" or brand == "Grain":
return "Grain Waves"
elif brand == "Infzns":
return "Infuzions"
elif brand == "Natural" or brand == "NCC":
return "Natural Chip Co"
elif brand == "Red":
return "RRD"
elif brand == "Smith":
return "Smiths"
elif brand == "Snbts":
return "Sunbites"
elif brand == "WW":
return "Woolworths"
else:
return brand
# In[391]:
merged_data["Cleaned_Brand_Names"] = merged_data.apply(lambda line: clean_brand_names(line), axis=1)
# In[392]:
merged_data["Cleaned_Brand_Names"].value_counts(ascending=True).plot.barh(figsize=(10,5))
# In[393]:
merged_data.isnull().sum()
# - Who spends the most on chips (total sales), describing customers by lifestage and
# how premium their general purchasing behaviour is
# - How many customers are in each segment
# - How many chips are bought per customer by segment
# - What's the average chip price by customer segment
# In[395]:
grouped_sales = pd.DataFrame(merged_data.groupby(["LIFESTAGE", "PREMIUM_CUSTOMER"])["TOT_SALES"].agg(["sum", "mean"]))
grouped_sales.sort_values(ascending=False, by="sum")
# In[396]:
grouped_sales["sum"].sum()
# In[397]:
grouped_sales["sum"].sort_values().plot.barh(figsize=(12,7))
# In[398]:
# Values of each group
bars1 = grouped_sales[grouped_sales.index.get_level_values("PREMIUM_CUSTOMER") == "Budget"]["sum"]
bars2 = grouped_sales[grouped_sales.index.get_level_values("PREMIUM_CUSTOMER") == "Mainstream"]["sum"]
bars3 = grouped_sales[grouped_sales.index.get_level_values("PREMIUM_CUSTOMER") == "Premium"]["sum"]
bars1_text = (bars1 / sum(grouped_sales["sum"])).apply("{:.1%}".format)
bars2_text = (bars2 / sum(grouped_sales["sum"])).apply("{:.1%}".format)
bars3_text = (bars3 / sum(grouped_sales["sum"])).apply("{:.1%}".format)
# Names of group and bar width
names = grouped_sales.index.get_level_values("LIFESTAGE").unique()
# The position of the bars on the x-axis
r = np.arange(len(names))
plt.figure(figsize=(13,5))
# Create brown bars
budget_bar = plt.barh(r, bars1, edgecolor='grey', height=1, label="Budget")
# Create green bars (middle), on top of the firs ones
mains_bar = plt.barh(r, bars2, left=bars1, edgecolor='grey', height=1, label="Mainstream")
# Create green bars (top)
prem_bar = plt.barh(r, bars3, left=np.add(bars1, bars2), edgecolor='grey', height=1, label="Premium")
for i in range(7):
budget_width = budget_bar[i].get_width()
budget_main_width = budget_width + mains_bar[i].get_width()
plt.text(budget_width/2, i, bars1_text[i], va='center', ha='center', size=8)
plt.text(budget_width + mains_bar[i].get_width()/2, i, bars2_text[i], va='center', ha='center', size=8)
plt.text(budget_main_width + prem_bar[i].get_width()/2, i, bars3_text[i], va='center', ha='center', size=8)
# Custom X axis
plt.yticks(r, names)
plt.ylabel("LIFESTAGE")
plt.xlabel("TOTAL SALES")
plt.legend(loc='center left', bbox_to_anchor=(1.0, 0.5))
plt.title("Total Sales per Lifestage")
plt.savefig("lifestage_sales.png", bbox_inches="tight")
# Show graphic
plt.show()
# In[399]:
stage_agg_prem = merged_data.groupby("LIFESTAGE")["PREMIUM_CUSTOMER"].agg(pd.Series.mode).sort_values()
print("Top contributor per LIFESTAGE by PREMIUM category")
print(stage_agg_prem)
# The top 3 total sales contributor segment are (in order):
# - Older families (Budget) \$156,864
# - Young Singles/Couples (Mainstream) \$147,582
# - Retirees (Mainstream) \$145,169
# In[400]:
unique_cust = merged_data.groupby(["LIFESTAGE", "PREMIUM_CUSTOMER"])["LYLTY_CARD_NBR"].nunique().sort_values(ascending=False)
pd.DataFrame(unique_cust)
# In[401]:
unique_cust.sort_values().plot.barh(figsize=(12,7))
# In[402]:
# Values of each group
ncust_bars1 = unique_cust[unique_cust.index.get_level_values("PREMIUM_CUSTOMER") == "Budget"]
ncust_bars2 = unique_cust[unique_cust.index.get_level_values("PREMIUM_CUSTOMER") == "Mainstream"]
ncust_bars3 = unique_cust[unique_cust.index.get_level_values("PREMIUM_CUSTOMER") == "Premium"]
ncust_bars1_text = (ncust_bars1 / sum(unique_cust)).apply("{:.1%}".format)
ncust_bars2_text = (ncust_bars2 / sum(unique_cust)).apply("{:.1%}".format)
ncust_bars3_text = (ncust_bars3 / sum(unique_cust)).apply("{:.1%}".format)
# # Names of group and bar width
#names = unique_cust.index.get_level_values("LIFESTAGE").unique()
# # The position of the bars on the x-axis
#r = np.arange(len(names))
plt.figure(figsize=(13,5))
# # Create brown bars
budget_bar = plt.barh(r, ncust_bars1, edgecolor='grey', height=1, label="Budget")
# # Create green bars (middle), on top of the firs ones
mains_bar = plt.barh(r, ncust_bars2, left=ncust_bars1, edgecolor='grey', height=1, label="Mainstream")
# # Create green bars (top)
prem_bar = plt.barh(r, ncust_bars3, left=np.add(ncust_bars1, ncust_bars2), edgecolor='grey', height=1, label="Premium")
for i in range(7):
budget_width = budget_bar[i].get_width()
budget_main_width = budget_width + mains_bar[i].get_width()
plt.text(budget_width/2, i, ncust_bars1_text[i], va='center', ha='center', size=8)
plt.text(budget_width + mains_bar[i].get_width()/2, i, ncust_bars2_text[i], va='center', ha='center', size=8)
plt.text(budget_main_width + prem_bar[i].get_width()/2, i, ncust_bars3_text[i], va='center', ha='center', size=8)
# Custom X axis
plt.yticks(r, names)
plt.ylabel("LIFESTAGE")
plt.xlabel("UNIQUE CUSTOMERS")
plt.legend(loc='center left', bbox_to_anchor=(1.0, 0.5))
plt.title("Unique Customers per Lifestage")
plt.savefig("lifestage_customers.png", bbox_inches="tight")
# # Show graphic
plt.show()
# The high sales amount by segment "Young Singles/Couples - Mainstream" and "Retirees - Mainstream" are due to their large number of unique customers, but not for the "Older - Budget" segment. Next we'll explore if the "Older - Budget" segment has:
# - High Frequency of Purchase and,
# - Average Sales per Customer compared to the other segment.
# In[403]:
freq_per_cust = merged_data.groupby(["LYLTY_CARD_NBR", "LIFESTAGE", "PREMIUM_CUSTOMER"]).count()["DATE"]
freq_per_cust.groupby(["LIFESTAGE", "PREMIUM_CUSTOMER"]).agg(["mean", "count"]).sort_values(ascending=False, by="mean")
# The above table describes the "Average frequency of Purchase per segment" and "Unique customer per segment". The top three most frequent purchase is contributed by the "Older Families" lifestage segment. We can see now that the "Older - Budget" segment contributes to high sales partly because of the combination of:
# - High Frequency of Purchase and,
# - Fairly high unique number of customer in the segment
# In[404]:
grouped_sales.sort_values(ascending=False, by="mean")
# Highest average spending per purchase are contributed by the Midage and Young "Singles/Couples". The difference between their Mainstream and Non-Mainstream group might seem insignificant (7.6 vs 6.6), but we'll find out by examining if the difference is statistically significant.
# In[405]:
from scipy.stats import ttest_ind
# In[406]:
mainstream = merged_data["PREMIUM_CUSTOMER"] == "Mainstream"
young_midage = (merged_data["LIFESTAGE"] == "MIDAGE SINGLES/COUPLES") | (merged_data["LIFESTAGE"] == "YOUNG SINGLES/COUPLES")
budget_premium = (merged_data["PREMIUM_CUSTOMER"] == "Budget") | (merged_data["PREMIUM_CUSTOMER"] == "Premium")
a = merged_data[young_midage & mainstream]["TOT_SALES"]
b = merged_data[young_midage & budget_premium]["TOT_SALES"]
stat, pval = ttest_ind(a.values, b.values, equal_var=False)
print(pval)
pval < 0.0000001
# P-Value is close to 0. There is a statistically significant difference to the Total Sales between the "Mainstream Young Midage" segment to the "Budget and Premium Young Midage" segment.
# Next, let's look examine what brand of chips the top 3 segments contributing to Total Sales are buying.
# In[407]:
merged_data.groupby(["LIFESTAGE", "PREMIUM_CUSTOMER"])["Cleaned_Brand_Names"].agg(pd.Series.mode).sort_values()
# In[408]:
for stage in merged_data["LIFESTAGE"].unique():
for prem in merged_data["PREMIUM_CUSTOMER"].unique():
print('==========',stage, '-', prem,'==========')
summary = merged_data[(merged_data["LIFESTAGE"] == stage) & (merged_data["PREMIUM_CUSTOMER"] == prem)]["Cleaned_Brand_Names"].value_counts().head(3)
print(summary)
plt.figure()
summary.plot.barh(figsize=(5,1))
plt.show()
# Every segment had Kettle as the most purchased brand. Every segment except "YOUNG SINGLES/COUPLES Mainstream" had Smiths as their second most purchased brand. "YOUNG SINGLES/COUPLES Mainstream" had Doritos as their second most purchased brand.
# In[409]:
from mlxtend.frequent_patterns import apriori
from mlxtend.frequent_patterns import association_rules
# In[410]:
temp = merged_data.reset_index().rename(columns = {"index": "transaction"})
temp["Segment"] = temp["LIFESTAGE"] + ' - ' + temp['PREMIUM_CUSTOMER']
segment_brand_encode = pd.concat([pd.get_dummies(temp["Segment"]), pd.get_dummies(temp["Cleaned_Brand_Names"])], axis=1)
# In[411]:
frequent_sets = apriori(segment_brand_encode, min_support=0.01, use_colnames=True)
rules = association_rules(frequent_sets, metric="lift", min_threshold=1)
# In[412]:
set_temp = temp["Segment"].unique()
rules[rules["antecedents"].apply(lambda x: list(x)).apply(lambda x: x in set_temp)]
# By looking at our a-priori analysis, we can conclude that Kettle is the brand of choice for most segment.
# Next, we'll find out the pack size preferences of different segments
# In[413]:
merged_pack = pd.concat([merged_data, pack_sizes.rename("Pack_Size")], axis=1)
for stage in merged_data["LIFESTAGE"].unique():
for prem in merged_data["PREMIUM_CUSTOMER"].unique():
print('==========',stage, '-', prem,'==========')
summary = merged_pack[(merged_pack["LIFESTAGE"] == stage) & (merged_pack["PREMIUM_CUSTOMER"] == prem)]["Pack_Size"].value_counts().head(3).sort_index()
print(summary)
plt.figure()
summary.plot.barh(figsize=(5,1))
plt.show()
# All of the segments prefer the 175gr pack size chips, followed by the 150gr size.
# Next, let's find out average amount of chips bought per customer segment.
# In[449]:
(temp.groupby(["LIFESTAGE", "PREMIUM_CUSTOMER"])["PROD_QTY"].sum() / temp.groupby(["LIFESTAGE", "PREMIUM_CUSTOMER"])["LYLTY_CARD_NBR"].nunique()).sort_values(ascending=False)
# In[464]:
(temp.groupby(["LIFESTAGE", "PREMIUM_CUSTOMER"])["PROD_QTY"].sum() / temp.groupby(["LIFESTAGE", "PREMIUM_CUSTOMER"])["LYLTY_CARD_NBR"].nunique()).unstack().plot.bar(figsize=(15,4), rot=0)
plt.legend(loc="center left", bbox_to_anchor=(1.0, 0.5))
plt.savefig("Average purchase quantity per segment.png", bbox_inches="tight")
# In[451]:
#Average chips price per transaction by segments
temp["Unit_Price"] = temp["TOT_SALES"] / temp["PROD_QTY"]
temp.groupby(["Segment"]).mean()["Unit_Price"].sort_values(ascending=False)
# In[463]:
temp.groupby(["LIFESTAGE", "PREMIUM_CUSTOMER"]).mean()["Unit_Price"].unstack().plot.bar(figsize=(15,4), rot=0)
plt.legend(loc="center left", bbox_to_anchor=(1,0.5))
# # Insights:
# - Top 3 total sales contributor segment are
# - Older families (Budget) \$156,864
# - Young Singles/Couples (Mainstream) \$147,582
# - Retirees (Mainstream) \$145,169
#
# - Young Singles/Couples (Mainstream) has the highest population, followed by Retirees (Mainstream). Which explains their high total sales.
# - Despite Older Families not having the highest population, they have the highest frequency of purchase, which contributes to their high total sales.
# - Older Families followed by Young Families has the highest average quantity of chips bought per purchase.
# - The Mainstream category of the "Young and Midage Singles/Couples" have the highest spending of chips per purchase. And the difference to the non-Mainstream "Young and Midage Singles/Couples" are statistically significant.
# - Chips brand Kettle is dominating every segment as the most purchased brand.
# - Observing the 2nd most purchased brand, "Young and Midage Singles/Couples" is the only segment with a different preference (Doritos) as compared to others' (Smiths).
# - Most frequent chip size purchased is 175gr followed by the 150gr chip size for all segments.
# # Reccomendations:
# - Older Families:
# - Focus on the Budget segment.
# - Strength: Frequent purchase. We can give promotions that encourages more frequency of purchase.
# - Strength: High quantity of chips purchased per visit. We can give promotions that encourage them to buy more quantity of chips per purchase.
# - Young Singles/Couples:
# - Focus on the Mainstream segment.
# - This segment is the only segment that had Doritos as their 2nd most purchased brand (after Kettle). To specifically target this segment it might be a good idea to collaborate with Doritos merchant to do some branding promotion catered to "Young Singles/Couples - Mainstream" segment.
# - Strength: Population quantity. We can spend more effort on making sure our promotions reach them, and it reaches them frequently.
# - Retirees:
# - Focus on the Mainstream segment.
# - Strength: Population quantity. Again, since their population quantity is the contributor to the high total sales, we should spend more effort on making sure our promotions reaches as many of them as possible and frequent.
# - General:
# - All segments has Kettle as the most frequently purchased brand, and 175gr (regardless of brand) followed by 150gr as the preferred chip size.
# - When promoting chips in general to all segments it is good to take advantage of these two points.
# In[125]:
z = temp.groupby(["Segment", "Cleaned_Brand_Names"]).sum()["TOT_SALES"].sort_values(ascending=False).reset_index()
z[z["Segment"] == "YOUNG SINGLES/COUPLES - Mainstream"]