在用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的過濾條件。
