什么是关联规则
引例:啤酒与尿布
发现了买尿布的顾客经常也会买啤酒后,超市把两者摆在一起,从而提高了两者的销量。具体原因是:年轻的父亲下班回家的路上需要给孩子买尿布,但他们又没空去酒吧,所以就通常也给自己买一些啤酒。
关联规则的基本概念
关联规则的主要目的是找出数据集中的频繁模式,即多次重复出现的项之间的“关联”。
关联规则与聚类一样,都是无监督的学习。它们都不用来预测。
应用关联规则最经典的案例就是购物篮分析。通过分析顾客购物篮中的商品之间的关联,可以得到顾客购物的习惯。
关联规则在传统零售业的应用
购物篮分析对于传统零售商非常有用,因为他们可以通过把顾客经常同时购买的商品摆在一起,从而方便顾客选取。这也叫做交叉销售。
找到关联规则后,超市可以:
- 把两三种商品摆在一起;
- 把两种商品打包销售;
- 把两种商品分开放置,迫使顾客必须走过通道寻找所要购买的东西,这时顾客可能会看到并购买其他东西。
关联规则在电商中的应用
不仅是传统零售业,在电商中同样有交叉销售,比如根据用户已经购买的商品,进行商品推荐,或者把两种商品捆绑销售。
关联规则有效性的两个指标
判断关联规则是否有效,最常用的两个指标是:
- 置信度
- 支持度
为了筛选出具有较高置信度和支持度的规则,需要给这两个指标分别设置一个阈值。达到了阈值的规则才是有效规则。
支持度:
定义:设W中有s%的事务同时支持物品集A和B,s%称为关联规则A→B 的支持度。支持度描述了A 和B这两个物品集的并集C在所有的事务中出现的概率有多大。
通俗解释:简单地说,A=>B的支持度就是指物品集A和物品集B同时出现的概率。
概率描述:物品集A对物品集B的支持度support(A==>B)=P(AnB)
置信度:
定义:设W中支持物品集A的事务中,有c%的事务同时也支持物品集B,c%称为关联规则A→B 的可信度。
通俗解释:简单地说,置信度就是指在出现了物品集A的事务T中,物品集B也同时出现的概率有多大。
概率描述:物品集A对物品集B的置信度confidence(A==>B)=P(A|B)
提升度:
定义:表示“使用A的用户中同时使用B的比例”与“使用B的用户比例”的比值。
通俗解释:提升度反映了“物品集A的出现”对物品集B的出现概率发生了多大的变化。
概率描述:lift(A==>B)=confidence(A==>B)/support(B)=p(B|A)/p(B)
例如:在所分析的10000个事务中,6000个事务包含计算机游戏,7500个包含游戏机游戏,4000个事务同时包含两者。分析关联规则(计算机游戏->游戏机游戏)
支持度为0.4,看似很高,但其实这个关联规则是一个误导。
在用户购买了计算机游戏后有 (4000÷6000)=0.667 的概率的去购买游戏机游戏,而在没有任何前提条件时,用户反而有(7500÷10000)=0.75的概率去购买游戏机游戏。
也就是说设置了购买计算机游戏这样的条件反而会降低用户去购买游戏机游戏的概率,所以计算机游戏和游戏机游戏是相斥的。
提升度表示含有X的条件下,同时含有Y的概率,与不含X的条件下却含Y的概率之比。
当提升度大于1时,意味着X的出现对Y的出现有促进作用,当它小于1时,表明X的出现降低了Y出现的可能性。
如果Lift(X→Y)>1,则规则“X→Y”是有效的强关联规则。
如果Lift(X→Y) <=1,则规则“X→Y”是无效的强关联规则。
特别地,如果Lift(X→Y) =1,则表示X与Y相互独立。
常用的关联规则算法——Apriori算法
步骤:
1.生成频繁项目集
2.生成所有可信关联规则。规则可以有多个前项和后项。当然,后项通常只有一个。
R语言与关联规则
R语言中与关联规则有关的包:
- 频繁项集、关联规则挖掘包:arules
- 关联规则可视化包:arulesViz
> data("Groceries")
> inspect(Groceries[1:10])
items
[1] {citrus fruit,
semi-finished bread,
margarine,
ready soups}
[2] {tropical fruit,
yogurt,
coffee}
[3] {whole milk}
[4] {pip fruit,
yogurt,
cream cheese ,
meat spreads}
[5] {other vegetables,
whole milk,
condensed milk,
long life bakery product}
[6] {whole milk,
butter,
yogurt,
rice,
abrasive cleaner}
[7] {rolls/buns}
[8] {other vegetables,
UHT-milk,
rolls/buns,
bottled beer,
liquor (appetizer)}
[9] {pot plants}
[10] {whole milk,
cereals}
生成关联规则:
> rules0=apriori(Groceries,parameter=list(support=0.001,confidence=0.5))
Apriori
Parameter specification:
confidence minval smax arem aval originalSupport maxtime support
0.5 0.1 1 none FALSE TRUE 5 0.001
minlen maxlen target ext
1 10 rules FALSE
Algorithmic control:
filter tree heap memopt load sort verbose
0.1 TRUE TRUE FALSE TRUE 2 TRUE
Absolute minimum support count: 9
set item appearances ...[0 item(s)] done [0.00s].
set transactions ...[169 item(s), 9835 transaction(s)] done [0.00s].
sorting and recoding items ... [157 item(s)] done [0.00s].
creating transaction tree ... done [0.00s].
checking subsets of size 1 2 3 4 5 6 done [0.02s].
writing ... [5668 rule(s)] done [0.01s].
creating S4 object ... done [0.01s].
> inspect(rules0[1:10])
lhs rhs support confidence lift count
[1] {honey} => {whole milk} 0.001118454 0.7333333 2.870009 11
[2] {tidbits} => {rolls/buns} 0.001220132 0.5217391 2.836542 12
[3] {cocoa drinks} => {whole milk} 0.001321810 0.5909091 2.312611 13
[4] {pudding powder} => {whole milk} 0.001321810 0.5652174 2.212062 13
[5] {cooking chocolate} => {whole milk} 0.001321810 0.5200000 2.035097 13
[6] {cereals} => {whole milk} 0.003660397 0.6428571 2.515917 36
[7] {jam} => {whole milk} 0.002948653 0.5471698 2.141431 29
[8] {specialty cheese} => {other vegetables} 0.004270463 0.5000000 2.584078 42
[9] {rice} => {other vegetables} 0.003965430 0.5200000 2.687441 39
[10] {rice} => {whole milk} 0.004677173 0.6133333 2.400371 46
支持度按降序排序:
> rules.sorted_sup = sort ( rules0, by="support" )
> inspect ( rules.sorted_sup [1:5] )
lhs rhs support confidence lift count
[1] {other vegetables,yogurt} => {whole milk} 0.02226741 0.5128806 2.007235 219
[2] {tropical fruit,yogurt} => {whole milk} 0.01514997 0.5173611 2.024770 149
[3] {other vegetables,whipped/sour cream} => {whole milk} 0.01464159 0.5070423 1.984385 144
[4] {root vegetables,yogurt} => {whole milk} 0.01453991 0.5629921 2.203354 143
[5] {pip fruit,other vegetables} => {whole milk} 0.01352313 0.5175097 2.025351 133
关联规则可视化:
> plot(rules0, method="graph")
如果商家想通过两件商品捆绑销售的办法来促销冷门商品芥末mustard,我们需要发现rhs仅包含芥末mustard 的关联规则,从而有效地找到mustard的强关联商品
> rules1<-apriori(Groceries,parameter=list(maxlen=2,supp=0.001,conf=0.1),appearance=list(rhs="mustard",default="lhs"))
Apriori
Parameter specification:
confidence minval smax arem aval originalSupport maxtime support minlen maxlen target ext
0.1 0.1 1 none FALSE TRUE 5 0.001 1 2 rules FALSE
Algorithmic control:
filter tree heap memopt load sort verbose
0.1 TRUE TRUE FALSE TRUE 2 TRUE
Absolute minimum support count: 9
set item appearances ...[1 item(s)] done [0.00s].
set transactions ...[169 item(s), 9835 transaction(s)] done [0.00s].
sorting and recoding items ... [157 item(s)] done [0.00s].
creating transaction tree ... done [0.00s].
checking subsets of size 1 2 done [0.00s].
writing ... [1 rule(s)] done [0.00s].
creating S4 object ... done [0.00s].
> inspect(rules1)
lhs rhs support confidence lift count
[1] {mayonnaise} => {mustard} 0.001423488 0.1555556 12.96516 14