R語言關(guān)聯(lián)規(guī)則深入詳解
在用R語言做關(guān)聯(lián)規(guī)則分析之前,我們先了解下關(guān)聯(lián)規(guī)則的相關(guān)定義和解釋。
關(guān)聯(lián)規(guī)則的用途是從數(shù)據(jù)背后發(fā)現(xiàn)事物之間可能存在的關(guān)聯(lián)或者聯(lián)系,是無監(jiān)督的機(jī)器學(xué)習(xí)方法,用于知識(shí)發(fā)現(xiàn),而非預(yù)測。
關(guān)聯(lián)規(guī)則挖掘過程主要包含兩個(gè)階段:第一階段從資料集合中找出所有的高頻項(xiàng)目組,第二階段再由這些高頻項(xiàng)目組中產(chǎn)生關(guān)聯(lián)規(guī)則。
接下來,我們了解下關(guān)聯(lián)規(guī)則的兩個(gè)主要參數(shù):支持度和置信度。
用簡化的方式來理解這兩個(gè)指標(biāo),支持度是兩個(gè)關(guān)聯(lián)物品同時(shí)出現(xiàn)的概率,而置信度是當(dāng)一物品出現(xiàn),則另一個(gè)物品也出現(xiàn)的概率。
假如有一條規(guī)則:牛肉—>雞肉,那么同時(shí)購買牛肉和雞肉的顧客比例是3/7,而購買牛肉的顧客當(dāng)中也購買了雞肉的顧客比例是3/4。這兩個(gè)比例參數(shù)是很重要的衡量指標(biāo),它們?cè)陉P(guān)聯(lián)規(guī)則中稱作支持度(support)和置信度(confidence)。對(duì)于規(guī)則:牛肉—>雞肉,它的支持度為3/7,表示在所有顧客當(dāng)中有3/7同時(shí)購買牛肉和雞肉,其反應(yīng)了同時(shí)購買牛肉和雞肉的顧客在所有顧客當(dāng)中的覆蓋范圍;它的置信度為3/4,表示在買了牛肉的顧客當(dāng)中有3/4的人買了雞肉,其反應(yīng)了可預(yù)測的程度,即顧客買了牛肉的話有多大可能性買雞肉。
關(guān)聯(lián)規(guī)則算法中最常用是Apriori算法。
下面我們用R來做個(gè)關(guān)聯(lián)規(guī)則的算法實(shí)例。在R中有一個(gè)arules包,我們可以用數(shù)據(jù)集Groceries作為實(shí)例。
library(arules) data(Groceries) #加載數(shù)據(jù)集 inspect(Groceries) #查看數(shù)據(jù)內(nèi)容
做完基礎(chǔ)動(dòng)作后,我們就需要求頻繁項(xiàng)集,即滿足最小支持度的關(guān)聯(lián)關(guān)系數(shù)據(jù)子集數(shù)量。
freq=eclat(Groceries,parameter = list(support=0.05,maxlen=10)) inspect(freq) #查看頻繁項(xiàng)集情況
' 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'
從結(jié)果來看,總共有31個(gè)頻繁項(xiàng)集,其中有很多只有一個(gè)條目的內(nèi)容,最小支持度可能太大了。
接下來我們選擇小一點(diǎn)的支持度,利用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ī)則內(nèi)容
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>
我們可以按照支持度對(duì)各關(guān)聯(lián)規(guī)則進(jìn)行排名后進(jìn)行查看
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>
可以看到結(jié)果中,當(dāng)購物籃中有other vegetables和yogurt兩個(gè)物品時(shí),也有whole milk的支持度最好,達(dá)到0.02。
具體的關(guān)聯(lián)規(guī)則情況我們還要根據(jù)業(yè)務(wù)的實(shí)際情況進(jìn)行篩選,也可以在建立關(guān)聯(lián)規(guī)則模型的過程中去掉那些明顯無用的規(guī)則。
比如我們要求結(jié)果中,被關(guān)聯(lián)項(xiàng)是whole mile 同時(shí)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>
再看結(jié)果中,只剩下4個(gè)lift值較高的關(guān)聯(lián)規(guī)則。
lift=P(L,R)/(P(L)P(R)) 是一個(gè)類似相關(guān)系數(shù)的指標(biāo)。lift=1時(shí)表示L和R獨(dú)立。這個(gè)數(shù)越大,越表明L和R存在在一個(gè)購物籃中不是偶然現(xiàn)象。
相關(guān)的篩選規(guī)則的符合解釋:
%in%
是精確匹配
%pin%
是部分匹配,也就是說只要item like '%A%' or item like '%B%'
%ain%
是完全匹配,也就是說itemset has 'A' and itemset has ‘B'
同時(shí)可以通過 條件運(yùn)算符(&, |, !) 添加 support, confidence, lift的過濾條件。
到此這篇關(guān)于R語言關(guān)聯(lián)規(guī)則深入詳解的文章就介紹到這了,更多相關(guān)R語言關(guān)聯(lián)規(guī)則內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
在R語言中實(shí)現(xiàn)Logistic邏輯回歸的操作
這篇文章主要介紹了在R語言中實(shí)現(xiàn)Logistic邏輯回歸的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-04-04R語言關(guān)于生存分析知識(shí)點(diǎn)總結(jié)
在本篇文章里,小編給大家整理的是一篇關(guān)于R語言生存分析的相關(guān)知識(shí)點(diǎn)及實(shí)例內(nèi)容,有興趣的朋友們跟著學(xué)習(xí)下吧。2021-05-05R語言科學(xué)計(jì)數(shù)法介紹:digits和scipen設(shè)置方式
這篇文章主要介紹了R語言科學(xué)計(jì)數(shù)法介紹:digits和scipen設(shè)置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-04-04R語言繪制小提琴圖violin plot實(shí)現(xiàn)示例
這篇文章主要為大家介紹了R語言繪制小提琴圖violin plot的實(shí)現(xiàn)方法示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2022-02-02R語言ggplot2邊框背景去除的實(shí)現(xiàn)
這篇文章主要介紹了R語言ggplot2邊框背景去除的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03R語言繪圖公式與變量對(duì)象混合拼接實(shí)現(xiàn)方法
這篇文章主要為大家介紹了R語言繪圖中的公式如何與變量對(duì)象混合拼接的實(shí)現(xiàn)方法,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2021-11-11基于R語言時(shí)間序列的平穩(wěn)時(shí)間序列模型預(yù)測圖文詳解
時(shí)間序列是將統(tǒng)一統(tǒng)計(jì)值按照時(shí)間發(fā)生的先后順序來進(jìn)行排列,時(shí)間序列分析的主要目的是根據(jù)已有數(shù)據(jù)對(duì)未來進(jìn)行預(yù)測,下面這篇文章主要給大家介紹了基于R語言時(shí)間序列的平穩(wěn)時(shí)間序列模型預(yù)測的相關(guān)資料,需要的朋友可以參考下2022-12-12