R語言關聯(lián)規(guī)則深入詳解
在用R語言做關聯(lián)規(guī)則分析之前,我們先了解下關聯(lián)規(guī)則的相關定義和解釋。
關聯(lián)規(guī)則的用途是從數(shù)據(jù)背后發(fā)現(xiàn)事物之間可能存在的關聯(lián)或者聯(lián)系,是無監(jiān)督的機器學習方法,用于知識發(fā)現(xiàn),而非預測。
關聯(lián)規(guī)則挖掘過程主要包含兩個階段:第一階段從資料集合中找出所有的高頻項目組,第二階段再由這些高頻項目組中產生關聯(lián)規(guī)則。
接下來,我們了解下關聯(lián)規(guī)則的兩個主要參數(shù):支持度和置信度。
用簡化的方式來理解這兩個指標,支持度是兩個關聯(lián)物品同時出現(xiàn)的概率,而置信度是當一物品出現(xiàn),則另一個物品也出現(xiàn)的概率。
假如有一條規(guī)則:牛肉—>雞肉,那么同時購買牛肉和雞肉的顧客比例是3/7,而購買牛肉的顧客當中也購買了雞肉的顧客比例是3/4。這兩個比例參數(shù)是很重要的衡量指標,它們在關聯(lián)規(guī)則中稱作支持度(support)和置信度(confidence)。對于規(guī)則:牛肉—>雞肉,它的支持度為3/7,表示在所有顧客當中有3/7同時購買牛肉和雞肉,其反應了同時購買牛肉和雞肉的顧客在所有顧客當中的覆蓋范圍;它的置信度為3/4,表示在買了牛肉的顧客當中有3/4的人買了雞肉,其反應了可預測的程度,即顧客買了牛肉的話有多大可能性買雞肉。
關聯(lián)規(guī)則算法中最常用是Apriori算法。
下面我們用R來做個關聯(lián)規(guī)則的算法實例。在R中有一個arules包,我們可以用數(shù)據(jù)集Groceries作為實例。
library(arules) data(Groceries) #加載數(shù)據(jù)集 inspect(Groceries) #查看數(shù)據(jù)內容
做完基礎動作后,我們就需要求頻繁項集,即滿足最小支持度的關聯(lián)關系數(shù)據(jù)子集數(shù)量。
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函數(shù)建立模型
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
接下來查看,具體的規(guī)則內容
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>
我們可以按照支持度對各關聯(lián)規(guī)則進行排名后進行查看
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。
具體的關聯(lián)規(guī)則情況我們還要根據(jù)業(yè)務的實際情況進行篩選,也可以在建立關聯(lián)規(guī)則模型的過程中去掉那些明顯無用的規(guī)則。
比如我們要求結果中,被關聯(lián)項是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值較高的關聯(lián)規(guī)則。
lift=P(L,R)/(P(L)P(R)) 是一個類似相關系數(shù)的指標。lift=1時表示L和R獨立。這個數(shù)越大,越表明L和R存在在一個購物籃中不是偶然現(xiàn)象。
相關的篩選規(guī)則的符合解釋:
%in%
是精確匹配
%pin%
是部分匹配,也就是說只要item like '%A%' or item like '%B%'
%ain%
是完全匹配,也就是說itemset has 'A' and itemset has ‘B'
同時可以通過 條件運算符(&, |, !) 添加 support, confidence, lift的過濾條件。
到此這篇關于R語言關聯(lián)規(guī)則深入詳解的文章就介紹到這了,更多相關R語言關聯(lián)規(guī)則內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
R語言科學計數(shù)法介紹:digits和scipen設置方式
這篇文章主要介紹了R語言科學計數(shù)法介紹:digits和scipen設置方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-04-04R語言繪制小提琴圖violin plot實現(xiàn)示例
這篇文章主要為大家介紹了R語言繪制小提琴圖violin plot的實現(xiàn)方法示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步2022-02-02