數(shù)據(jù)描述
本數(shù)據(jù)共有兩個(gè)表格念链,GoodsOrder.csv和GoodsTypes.csv,分別是商品購買信息和商品類型信息赘方。
分析目標(biāo)
分析商品的銷售信息缰泡,了解商品銷量刀荒。然后通過分析商品之間的關(guān)聯(lián)度,找到達(dá)到所制定標(biāo)準(zhǔn)的商品進(jìn)行推薦棘钞。
分析方法
對(duì)數(shù)據(jù)進(jìn)行預(yù)處理缠借,使其符合Apriori關(guān)聯(lián)規(guī)則的算法要求,然后找出數(shù)據(jù)值中頻繁出現(xiàn)的數(shù)據(jù)集合宜猜,導(dǎo)入python自帶算法包泼返,設(shè)置參數(shù),找出達(dá)到置信度姨拥、支持度绅喉、提升度的商品組合。
數(shù)據(jù)處理與分析
數(shù)據(jù)預(yù)處理
1叫乌、首先導(dǎo)入包柴罐,讀取數(shù)據(jù)并查看數(shù)據(jù)前10行
In:
# 導(dǎo)入pandas、numpy憨奸、matplotlib包
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
# 讀取購買數(shù)據(jù)并查看
GoodsOrder = pd.read_csv('C:/data/Apriori/GoodsOrder.csv',encoding = 'gbk')
GoodsOrder.head(10)
Out:
id Goods
0 1 柑橘類水果
1 1 人造黃油
2 1 即食湯
3 1 半成品面包
4 2 咖啡
5 2 熱帶水果
6 2 酸奶
7 3 全脂牛奶
8 4 奶油乳酪
9 4 肉泥
2革屠、查看數(shù)據(jù)基本信息
In:
GoodsOrder.info()
Out:
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 43367 entries, 0 to 43366
Data columns (total 2 columns):
id 43367 non-null int64
Goods 43367 non-null object
dtypes: int64(1), object(1)
memory usage: 677.7+ KB
3、查看是否存在重復(fù)數(shù)據(jù)
In:
GoodsOrder.duplicated().sum()
Out:
0
從數(shù)據(jù)信息看出不存在空值排宰,上述結(jié)果看出不存在重復(fù)值似芝,因此無需進(jìn)行處理。
商品銷量查看與分析
銷量前十的商品分析
1板甘、查看銷量前十商品
In:
sale_hot = GoodsOrder.groupby(['Goods']).count().sort_values('id',ascending = False).reset_index()
sale_hot.head(10)
Out:
Goods id
0 全脂牛奶 2513
1 其他蔬菜 1903
2 面包卷 1809
3 蘇打 1715
4 酸奶 1372
5 瓶裝水 1087
6 根莖類蔬菜 1072
7 熱帶水果 1032
8 購物袋 969
9 香腸 924
2党瓮、繪制銷量前十條形圖
In:
x = sale_hot[:10]['Goods']
y = sale_hot[:10]['id']
plt.figure(figsize=(10,5))
plt.title('銷量TOP10商品')
plt.xlabel('銷量')
plt.ylabel('商品')
plt.yticks(range(len(x)),x)
plt.barh(range(len(x)),y)
plt.show()
Out:
3、計(jì)算銷量前10的商品銷量在總銷量中的占比
In:
sale_total = sale_hot['id'].sum()
sale_acc = sale_hot[:10]['id'] / sale_total
sale_hot_acc = sale_hot[:10]
sale_hot_acc['percent'] = sale_acc
sale_hot_acc
Out:
Goods id percent
0 全脂牛奶 2513 0.057947
1 其他蔬菜 1903 0.043881
2 面包卷 1809 0.041714
3 蘇打 1715 0.039546
4 酸奶 1372 0.031637
5 瓶裝水 1087 0.025065
6 根莖類蔬菜 1072 0.024719
7 熱帶水果 1032 0.023797
8 購物袋 969 0.022344
9 香腸 924 0.021307
商品類型銷量分析
1虾啦、讀取商品數(shù)據(jù)并查看
In:
GoodsTypes = pd.read_csv('C:/data/Apriori/GoodsTypes.csv',encoding = 'gbk')
GoodsTypes.head(10)
Out:
Goods Types
0 白飯 熟食
1 白酒 酒精飲料
2 白蘭地 酒精飲料
3 白面包 西點(diǎn)
4 半成品面包 西點(diǎn)
5 包裝袋 百貨
6 包裝水果/蔬菜 果蔬
7 保鮮膜/袋 百貨
8 報(bào)紙 百貨
9 爆米花 零食
2麻诀、將兩張表合并,并計(jì)算各商品的銷售量
In:
Goods_merge = pd.merge(sale_hot,GoodsTypes)
Goods_merge.head(10)
Out:
Goods id Types
0 全脂牛奶 2513 非酒精飲料
1 其他蔬菜 1903 果蔬
2 面包卷 1809 西點(diǎn)
3 蘇打 1715 非酒精飲料
4 酸奶 1372 食品類
5 瓶裝水 1087 非酒精飲料
6 根莖類蔬菜 1072 果蔬
7 熱帶水果 1032 果蔬
8 購物袋 969 百貨
9 香腸 924 肉類
3傲醉、計(jì)算各商品類別的總銷量
In:
sale_type = Goods_merge.groupby(['Types']).sum().sort_values('id',ascending = False).reset_index()
sale_type_p =sale_type['id'] / sale_total
sale_type['percent'] = sale_type_p
sale_type
Out:
Types id percent
0 非酒精飲料 7594 0.175110
1 西點(diǎn) 7192 0.165840
2 果蔬 7146 0.164780
3 米糧調(diào)料 5185 0.119561
4 百貨 5141 0.118546
5 肉類 4870 0.112297
6 酒精飲料 2287 0.052736
7 食品類 1870 0.043120
8 零食 1459 0.033643
9 熟食 541 0.012475
4、繪制餅圖查看各類商品銷量占比
In:
data = sale_type['percent']
plt.figure(figsize=(7,7))
plt.title('商品銷量占比')
plt.pie(data,labels = sale_type['Types'],autopct = '%1.2f%%') # 設(shè)置數(shù)據(jù)標(biāo)簽保留2位小數(shù)
plt.show()
Out:
上圖可以看出呻率,非酒精飲料硬毕、西點(diǎn)、果蔬是銷量最高的三個(gè)類別礼仗,占比超過總銷量的50%吐咳,接下來可以將非酒精飲料進(jìn)一步分析逻悠,查看內(nèi)部哪些商品銷量更高。
非酒精飲料內(nèi)部商品銷量分析
1韭脊、計(jì)算非酒精飲料內(nèi)部商品銷量占比
In:
sale_nowine = Goods_merge.loc[Goods_merge['Types'] == '非酒精飲料']
sale_nowine_total = sale_nowine['id'].sum()
sale_nowine_p =sale_nowine['id'] / sale_nowine_total
sale_nowine['percent'] = sale_nowine_p
sale_nowine
Out:
Goods id Types percent
0 全脂牛奶 2513 非酒精飲料 0.330919
3 蘇打 1715 非酒精飲料 0.225836
5 瓶裝水 1087 非酒精飲料 0.143139
16 水果/蔬菜汁 711 非酒精飲料 0.093627
22 咖啡 571 非酒精飲料 0.075191
38 超高溫殺菌的牛奶 329 非酒精飲料 0.043324
45 其他飲料 279 非酒精飲料 0.036740
51 一般飲料 256 非酒精飲料 0.033711
101 速溶咖啡 73 非酒精飲料 0.009613
125 茶 38 非酒精飲料 0.005004
144 可可飲料 22 非酒精飲料 0.002897
2童谒、繪制餅圖查看各類商品銷量占比
In:
data = sale_nowine['percent']
plt.figure(figsize=(7,7))
plt.title('非酒精飲料內(nèi)部商品銷量占比')
explode = (0.02,0.03,0.04,0.05,0.06,0.07,0.08,0.08,0.3,0.1,0.3)
plt.pie(data,explode = explode,labels = sale_nowine['Goods'],autopct = '%1.2f%%') # 設(shè)置數(shù)據(jù)標(biāo)簽保留2位小數(shù)
plt.show()
Out:
上圖可以看出,非酒精飲料中沪羔,銷量最高的是全脂牛奶饥伊,占非酒精類飲料銷量的33%,其次是蘇打水蔫饰,和全脂牛奶一起占據(jù)50%以上比例琅豆。
構(gòu)建模型進(jìn)行推薦
1、構(gòu)建用戶——產(chǎn)品矩陣
In:
GoodsOrder_new = (GoodsOrder.groupby(['id', 'Goods']).size().unstack().reset_index().fillna(0).set_index('id'))
2篓吁、生成頻繁項(xiàng)集:取支持度最小為0.02
In:
from mlxtend.frequent_patterns import association_rules
from mlxtend.frequent_patterns import apriori
frequent = apriori(GoodsOrder_new, min_support=0.02, use_colnames=True, max_len=3)
frequent
Out:
support itemsets
0 0.025826 [一般肉類]
1 0.026029 [一般飲料]
2 0.058566 [人造黃油]
3 0.075648 [仁果類水果]
4 0.255516 [全脂牛奶]
5 0.193493 [其他蔬菜]
6 0.028368 [其他飲料]
7 0.025013 [冰淇淋]
8 0.058973 [冷凍熱狗腸]
9 0.048094 [冷凍蔬菜]
10 0.028368 [冷凍餐飯]
11 0.053279 [凝乳]
12 0.024504 [切片奶酪]
13 0.032944 [衛(wèi)生用品]
14 0.021047 [口香糖]
15 0.058058 [咖啡]
16 0.037824 [咸點(diǎn)心]
17 0.039654 [奶油乳酪]
18 0.038434 [威化餅]
19 0.049619 [巧克力]
20 0.079817 [報(bào)紙]
21 0.063447 [本地蛋類]
22 0.082766 [柑橘類水果]
23 0.108998 [根莖類蔬菜]
24 0.072293 [水果/蔬菜汁]
25 0.033249 [漢堡肉]
26 0.028063 [油]
27 0.031012 [洋蔥]
28 0.026029 [火腿]
29 0.104931 [熱帶水果]
... ... ...
92 0.024809 [其他蔬菜, 瓶裝水]
93 0.022572 [其他蔬菜, 糕點(diǎn)]
94 0.032740 [其他蔬菜, 蘇打]
95 0.023183 [其他蔬菜, 購物袋]
96 0.043416 [其他蔬菜, 酸奶]
97 0.028876 [其他蔬菜, 酸奶油]
98 0.042603 [其他蔬菜, 面包卷]
99 0.026945 [其他蔬菜, 香腸]
100 0.020031 [其他蔬菜, 黃油]
101 0.021657 [柑橘類水果, 酸奶]
102 0.021047 [根莖類蔬菜, 熱帶水果]
103 0.025826 [根莖類蔬菜, 酸奶]
104 0.024301 [根莖類蔬菜, 面包卷]
105 0.020844 [熱帶水果, 蘇打]
106 0.029283 [熱帶水果, 酸奶]
107 0.024606 [熱帶水果, 面包卷]
108 0.028978 [瓶裝水, 蘇打]
109 0.022979 [瓶裝水, 酸奶]
110 0.024199 [瓶裝水, 面包卷]
111 0.021047 [糕點(diǎn), 蘇打]
112 0.020946 [糕點(diǎn), 面包卷]
113 0.024606 [蘇打, 購物袋]
114 0.027351 [蘇打, 酸奶]
115 0.038332 [蘇打, 面包卷]
116 0.024301 [蘇打, 香腸]
117 0.020742 [酸奶, 酸奶油]
118 0.034367 [酸奶, 面包卷]
119 0.030605 [面包卷, 香腸]
120 0.023183 [全脂牛奶, 其他蔬菜, 根莖類蔬菜]
121 0.022267 [全脂牛奶, 其他蔬菜, 酸奶]
122 rows × 2 columns
3茫因、生成規(guī)則:以提升度為標(biāo)準(zhǔn)
In:
rules = association_rules(frequent, metric="lift", min_threshold=0.8)
rules.head(10)
Out:
antecedants consequents antecedent support consequent support support confidence lift leverage conviction
0 (人造黃油) (全脂牛奶) 0.058566 0.255516 0.024199 0.413194 1.617098 0.009235 1.268706
1 (全脂牛奶) (人造黃油) 0.255516 0.058566 0.024199 0.094708 1.617098 0.009235 1.039922
2 (全脂牛奶) (仁果類水果) 0.255516 0.075648 0.030097 0.117788 1.557043 0.010767 1.047765
3 (仁果類水果) (全脂牛奶) 0.075648 0.255516 0.030097 0.397849 1.557043 0.010767 1.236375
4 (其他蔬菜) (仁果類水果) 0.193493 0.075648 0.026131 0.135050 1.785237 0.011494 1.068676
5 (仁果類水果) (其他蔬菜) 0.075648 0.193493 0.026131 0.345430 1.785237 0.011494 1.232118
6 (熱帶水果) (仁果類水果) 0.104931 0.075648 0.020437 0.194767 2.574648 0.012499 1.147931
7 (仁果類水果) (熱帶水果) 0.075648 0.104931 0.020437 0.270161 2.574648 0.012499 1.226392
8 (其他蔬菜) (全脂牛奶) 0.193493 0.255516 0.074835 0.386758 1.513634 0.025394 1.214013
9 (全脂牛奶) (其他蔬菜) 0.255516 0.193493 0.074835 0.292877 1.513634 0.025394 1.140548
4、推薦結(jié)果:提升度最小值為1杖剪,置信度最小值為0.35
In:
R_result = rules[(rules['lift'] >= 1) & (rules['confidence'] >= 0.35)]
R_result.sort_values(by=['lift'], axis=0, ascending=False)
Out:
antecedants consequents antecedent support consequent support support confidence lift leverage conviction
124 (全脂牛奶, 根莖類蔬菜) (其他蔬菜) 0.048907 0.193493 0.023183 0.474012 2.449770 0.013719 1.533320
59 (根莖類蔬菜) (其他蔬菜) 0.108998 0.193493 0.047382 0.434701 2.246605 0.026291 1.426693
77 (酸奶油) (其他蔬菜) 0.071683 0.193493 0.028876 0.402837 2.081924 0.015006 1.350565
130 (酸奶, 全脂牛奶) (其他蔬菜) 0.056024 0.193493 0.022267 0.397459 2.054131 0.011427 1.338511
128 (其他蔬菜, 酸奶) (全脂牛奶) 0.043416 0.255516 0.022267 0.512881 2.007235 0.011174 1.528340
50 (黃油) (全脂牛奶) 0.055414 0.255516 0.027555 0.497248 1.946053 0.013395 1.480817
65 (豬肉) (其他蔬菜) 0.057651 0.193493 0.021657 0.375661 1.941476 0.010502 1.291779
14 (凝乳) (全脂牛奶) 0.053279 0.255516 0.026131 0.490458 1.919481 0.012517 1.461085
122 (其他蔬菜, 根莖類蔬菜) (全脂牛奶) 0.047382 0.255516 0.023183 0.489270 1.914833 0.011076 1.457687
83 (黃油) (其他蔬菜) 0.055414 0.193493 0.020031 0.361468 1.868122 0.009308 1.263065
19 (本地蛋類) (全脂牛奶) 0.063447 0.255516 0.029995 0.472756 1.850203 0.013783 1.412030
55 (本地蛋類) (其他蔬菜) 0.063447 0.193493 0.022267 0.350962 1.813824 0.009991 1.242619
44 (酸奶油) (全脂牛奶) 0.071683 0.255516 0.032232 0.449645 1.759754 0.013916 1.352735
22 (根莖類蔬菜) (全脂牛奶) 0.108998 0.255516 0.048907 0.448694 1.756031 0.021056 1.350401
12 (冷凍蔬菜) (全脂牛奶) 0.048094 0.255516 0.020437 0.424947 1.663094 0.008149 1.294636
0 (人造黃油) (全脂牛奶) 0.058566 0.255516 0.024199 0.413194 1.617098 0.009235 1.268706
28 (牛肉) (全脂牛奶) 0.052466 0.255516 0.021251 0.405039 1.585180 0.007845 1.251315
27 (熱帶水果) (全脂牛奶) 0.104931 0.255516 0.042298 0.403101 1.577595 0.015486 1.247252
42 (酸奶) (全脂牛奶) 0.139502 0.255516 0.056024 0.401603 1.571735 0.020379 1.244132
3 (仁果類水果) (全脂牛奶) 0.075648 0.255516 0.030097 0.397849 1.557043 0.010767 1.236375
52 (黑面包) (全脂牛奶) 0.064870 0.255516 0.025216 0.388715 1.521293 0.008641 1.217899
8 (其他蔬菜) (全脂牛奶) 0.193493 0.255516 0.074835 0.386758 1.513634 0.025394 1.214013
31 (豬肉) (全脂牛奶) 0.057651 0.255516 0.022166 0.384480 1.504719 0.007435 1.209520
36 (糕點(diǎn)) (全脂牛奶) 0.088968 0.255516 0.033249 0.373714 1.462587 0.010516 1.188729
20 (柑橘類水果) (全脂牛奶) 0.082766 0.255516 0.030503 0.368550 1.442377 0.009355 1.179008
24 (水果/蔬菜汁) (全脂牛奶) 0.072293 0.255516 0.026640 0.368495 1.442160 0.008168 1.178904