在用R语言做关联规则分析之前,我们先了解下关联规则的相关定义和解释。

关联规则的用途是从数据背后发现事物之间可能存在的关联或者联系,是无监督的机器学习方法,用于知识发现,而非预测。

关联规则挖掘过程主要包含两个阶段:第一阶段从资料集合中找出所有的高频项目组,第二阶段再由这些高频项目组中产生关联规则。

接下来,我们了解下关联规则的两个主要参数:支持度和置信度。

用简化的方式来理解这两个指标,支持度是两个关联物品同时出现的概率,而置信度是当一物品出现,则另一个物品也出现的概率。

假如有一条规则:牛肉—>鸡肉,那么同时购买牛肉和鸡肉的顾客比例是3/7,而购买牛肉的顾客当中也购买了鸡肉的顾客比例是3/4。这两个比例参数是很重要的衡量指标,它们在关联规则中称作支持度(support)和置信度(confidence)。对于规则:牛肉—>鸡肉,它的支持度为3/7,表示在所有顾客当中有3/7同时购买牛肉和鸡肉,其反应了同时购买牛肉和鸡肉的顾客在所有顾客当中的覆盖范围;它的置信度为3/4,表示在买了牛肉的顾客当中有3/4的人买了鸡肉,其反应了可预测的程度,即顾客买了牛肉的话有多大可能性买鸡肉。

关联规则算法中最常用是Apriori算法。

下面我们用R来做个关联规则的算法实例。在R中有一个arules包,我们可以用数据集Groceries作为实例。

library(arules)
data(Groceries) #加载数据集
inspect(Groceries) #查看数据内容

做完基础动作后,我们就需要求频繁项集,即满足最小支持度的关联关系数据子集数量。

freq=eclat(Groceries,parameter = list(support=0.05,maxlen=10))
inspect(freq) #查看频繁项集情况

' items support

[1] {whole milk,yogurt} 0.05602440

[2] {whole milk,rolls/buns} 0.05663447

[3] {other vegetables,whole milk} 0.07483477

[4] {whole milk} 0.25551601

[5] {other vegetables} 0.19349263

[6] {rolls/buns} 0.18393493

[7] {yogurt} 0.13950178

[8] {soda} 0.17437722

[9] {root vegetables} 0.10899847

[10] {tropical fruit} 0.10493137

[11] {bottled water} 0.11052364

[12] {sausage} 0.09395018

[13] {shopping bags} 0.09852567

[14] {citrus fruit} 0.08276563

[15] {pastry} 0.08896797

[16] {pip fruit} 0.07564820

[17] {whipped/sour cream} 0.07168277

[18] {fruit/vegetable juice} 0.07229283

[19] {domestic eggs} 0.06344687

[20] {newspapers} 0.07981698

[21] {butter} 0.05541434

[22] {margarine} 0.05856634

[23] {brown bread} 0.06487036

[24] {bottled beer} 0.08052872

[25] {frankfurter} 0.05897306

[26] {pork} 0.05765125

[27] {napkins} 0.05236401

[28] {curd} 0.05327911

[29] {beef} 0.05246568

[30] {coffee} 0.05805796

[31] {canned beer} 0.07768175'

从结果来看,总共有31个频繁项集,其中有很多只有一个条目的内容,最小支持度可能太大了。

接下来我们选择小一点的支持度,利用Apriori函数建立模型

model<-apriori(Groceries,parameter=list(support=0.01,confidence=0.5))
summary(model)

set of 15 rules

rule length distribution (lhs + rhs):sizes

3

15

Min. 1st Qu. Median Mean 3rd Qu. Max.

3 3 3 3 3 3

summary of quality measures:

support confidence lift

Min. :0.01007 Min. :0.5000 Min. :1.984

1st Qu.:0.01174 1st Qu.:0.5151 1st Qu.:2.036

Median :0.01230 Median :0.5245 Median :2.203

Mean :0.01316 Mean :0.5411 Mean :2.299

3rd Qu.:0.01403 3rd Qu.:0.5718 3rd Qu.:2.432

Max. :0.02227 Max. :0.5862 Max. :3.030

mining info:

data ntransactions support confidence

Groceries 9835 0.01 0.5

接下来查看,具体的规则内容

inspect(model)

< lhs rhs support

[1] {curd,yogurt} => {whole milk} 0.01006609

[2] {other vegetables,butter} => {whole milk} 0.01148958

[3] {other vegetables,domestic eggs} => {whole milk} 0.01230300

[4] {yogurt,whipped/sour cream} => {whole milk} 0.01087951

[5] {other vegetables,whipped/sour cream} => {whole milk} 0.01464159

[6] {pip fruit,other vegetables} => {whole milk} 0.01352313

[7] {citrus fruit,root vegetables} => {other vegetables} 0.01037112

[8] {tropical fruit,root vegetables} => {other vegetables} 0.01230300

[9] {tropical fruit,root vegetables} => {whole milk} 0.01199797

[10] {tropical fruit,yogurt} => {whole milk} 0.01514997

[11] {root vegetables,yogurt} => {other vegetables} 0.01291307

[12] {root vegetables,yogurt} => {whole milk} 0.01453991

[13] {root vegetables,rolls/buns} => {other vegetables} 0.01220132

[14] {root vegetables,rolls/buns} => {whole milk} 0.01270971

[15] {other vegetables,yogurt} => {whole milk} 0.02226741

confidence lift

[1] 0.5823529 2.279125

[2] 0.5736041 2.244885

[3] 0.5525114 2.162336

[4] 0.5245098 2.052747

[5] 0.5070423 1.984385

[6] 0.5175097 2.025351

[7] 0.5862069 3.029608

[8] 0.5845411 3.020999

[9] 0.5700483 2.230969

[10] 0.5173611 2.024770

[11] 0.5000000 2.584078

[12] 0.5629921 2.203354

[13] 0.5020921 2.594890

[14] 0.5230126 2.046888

[15] 0.5128806 2.007235>

我们可以按照支持度对各关联规则进行排名后进行查看

inspect(sort(model,by="support")[1:10])

< lhs rhs support

[1] {other vegetables,yogurt} => {whole milk} 0.02226741

[2] {tropical fruit,yogurt} => {whole milk} 0.01514997

[3] {other vegetables,whipped/sour cream} => {whole milk} 0.01464159

[4] {root vegetables,yogurt} => {whole milk} 0.01453991

[5] {pip fruit,other vegetables} => {whole milk} 0.01352313

[6] {root vegetables,yogurt} => {other vegetables} 0.01291307

[7] {root vegetables,rolls/buns} => {whole milk} 0.01270971

[8] {other vegetables,domestic eggs} => {whole milk} 0.01230300

[9] {tropical fruit,root vegetables} => {other vegetables} 0.01230300

[10] {root vegetables,rolls/buns} => {other vegetables} 0.01220132

confidence lift

[1] 0.5128806 2.007235

[2] 0.5173611 2.024770

[3] 0.5070423 1.984385

[4] 0.5629921 2.203354

[5] 0.5175097 2.025351

[6] 0.5000000 2.584078

[7] 0.5230126 2.046888

[8] 0.5525114 2.162336

[9] 0.5845411 3.020999

[10] 0.5020921 2.594890>

可以看到结果中,当购物篮中有other vegetables和yogurt两个物品时,也有whole milk的支持度最好,达到0.02。

具体的关联规则情况我们还要根据业务的实际情况进行筛选,也可以在建立关联规则模型的过程中去掉那些明显无用的规则。

比如我们要求结果中,被关联项是whole mile 同时lift值要大于2.2

inspect(subset(model,subset=rhs%in%"whole milk"&lift>=2.2))

< lhs rhs support confidence lift

[1] {curd,yogurt} => {whole milk} 0.01006609 0.5823529 2.279125

[2] {other vegetables,butter} => {whole milk} 0.01148958 0.5736041 2.244885

[3] {tropical fruit,root vegetables} => {whole milk} 0.01199797 0.5700483 2.230969

[4] {root vegetables,yogurt} => {whole milk} 0.01453991 0.5629921 2.203354>

再看结果中,只剩下4个lift值较高的关联规则。

lift=P(L,R)/(P(L)P(R)) 是一个类似相关系数的指标。lift=1时表示L和R独立。这个数越大,越表明L和R存在在一个购物篮中不是偶然现象。

相关的筛选规则的符合解释:

%in%是精确匹配

%pin%是部分匹配,也就是说只要item like '%A%' or item like '%B%'

%ain%是完全匹配,也就是说itemset has ’A' and itemset has ‘B'

同时可以通过 条件运算符(&, |, !) 添加 support, confidence, lift的过滤条件。

05-11 13:03