[연관규칙] 기초

Author : tmlab / Date : 2016. 10. 6. 16:38 / Category : Text Mining/R

1) 패키지설치

#install.packages("arules")
library(arules)
## Warning: package 'arules' was built under R version 3.2.2
## Loading required package: Matrix
## 
## Attaching package: 'arules'
## 
## The following objects are masked from 'package:base':
## 
##     %in%, abbreviate, write

2) 데이터생성

사과<-c(1,1,0,1,1)
우유<-c(1,1,1,1,1)<-c(0,0,1,1,1)
버터<-c(0,1,1,1,0)
계란<-c(0,0,1,0,1)
콜라<-c(0,0,1,0,1)

3) 데이터 행렬로 변환

asso<-data.frame(사과, 우유, 빵, 버터, 계란, 콜라)
trans<-as.matrix(asso,"Transaction")
trans
##      사과 우유 빵 버터 계란 콜라
## [1,]    1    1  0    0    0    0
## [2,]    1    1  0    1    0    0
## [3,]    0    1  1    1    1    1
## [4,]    1    1  1    1    0    0
## [5,]    1    1  1    0    1    1

4)apriori알고리즘 : 최소 지지도 0.4, 최소신뢰도 0.6 이상인 규칙산출

rule1<-apriori(trans, parameter=list(supp=0.4, conf=0.6, target="rules"))
## 
## Parameter specification:
##  confidence minval smax arem  aval originalSupport support minlen maxlen
##         0.6    0.1    1 none FALSE            TRUE     0.4      1     10
##  target   ext
##   rules FALSE
## 
## Algorithmic control:
##  filter tree heap memopt load sort verbose
##     0.1 TRUE TRUE  FALSE TRUE    2    TRUE
## 
## apriori - find association rules with the apriori algorithm
## version 4.21 (2004.05.09)        (c) 1996-2004   Christian Borgelt
## set item appearances ...[0 item(s)] done [0.00s].
## set transactions ...[6 item(s), 5 transaction(s)] done [0.00s].
## sorting and recoding items ... [6 item(s)] done [0.00s].
## creating transaction tree ... done [0.00s].
## checking subsets of size 1 2 3 4 done [0.00s].
## writing ... [45 rule(s)] done [0.00s].
## creating S4 object  ... done [0.00s].

5)inpect()함수로 생성된 규칙확인

inspect(sort(rule1))
##    lhs                 rhs    support confidence lift     
## 4  {}               => {우유} 1.0     1.0000000  1.0000000
## 3  {}               => {사과} 0.8     0.8000000  1.0000000
## 21 {사과}           => {우유} 0.8     1.0000000  1.0000000
## 22 {우유}           => {사과} 0.8     0.8000000  1.0000000
## 1  {}               => {버터} 0.6     0.6000000  1.0000000
## 2  {}               => {빵}   0.6     0.6000000  1.0000000
## 16 {버터}           => {우유} 0.6     1.0000000  1.0000000
## 17 {우유}           => {버터} 0.6     0.6000000  1.0000000
## 19 {빵}             => {우유} 0.6     1.0000000  1.0000000
## 20 {우유}           => {빵}   0.6     0.6000000  1.0000000
## 5  {계란}           => {콜라} 0.4     1.0000000  2.5000000
## 6  {콜라}           => {계란} 0.4     1.0000000  2.5000000
## 7  {계란}           => {빵}   0.4     1.0000000  1.6666667
## 8  {빵}             => {계란} 0.4     0.6666667  1.6666667
## 9  {계란}           => {우유} 0.4     1.0000000  1.0000000
## 10 {콜라}           => {빵}   0.4     1.0000000  1.6666667
## 11 {빵}             => {콜라} 0.4     0.6666667  1.6666667
## 12 {콜라}           => {우유} 0.4     1.0000000  1.0000000
## 13 {버터}           => {빵}   0.4     0.6666667  1.1111111
## 14 {빵}             => {버터} 0.4     0.6666667  1.1111111
## 15 {버터}           => {사과} 0.4     0.6666667  0.8333333
## 18 {빵}             => {사과} 0.4     0.6666667  0.8333333
## 23 {계란,콜라}      => {빵}   0.4     1.0000000  1.6666667
## 24 {빵,계란}        => {콜라} 0.4     1.0000000  2.5000000
## 25 {빵,콜라}        => {계란} 0.4     1.0000000  2.5000000
## 26 {계란,콜라}      => {우유} 0.4     1.0000000  1.0000000
## 27 {우유,계란}      => {콜라} 0.4     1.0000000  2.5000000
## 28 {우유,콜라}      => {계란} 0.4     1.0000000  2.5000000
## 29 {빵,계란}        => {우유} 0.4     1.0000000  1.0000000
## 30 {우유,계란}      => {빵}   0.4     1.0000000  1.6666667
## 31 {우유,빵}        => {계란} 0.4     0.6666667  1.6666667
## 32 {빵,콜라}        => {우유} 0.4     1.0000000  1.0000000
## 33 {우유,콜라}      => {빵}   0.4     1.0000000  1.6666667
## 34 {우유,빵}        => {콜라} 0.4     0.6666667  1.6666667
## 35 {빵,버터}        => {우유} 0.4     1.0000000  1.0000000
## 36 {우유,버터}      => {빵}   0.4     0.6666667  1.1111111
## 37 {우유,빵}        => {버터} 0.4     0.6666667  1.1111111
## 38 {사과,버터}      => {우유} 0.4     1.0000000  1.0000000
## 39 {우유,버터}      => {사과} 0.4     0.6666667  0.8333333
## 40 {사과,빵}        => {우유} 0.4     1.0000000  1.0000000
## 41 {우유,빵}        => {사과} 0.4     0.6666667  0.8333333
## 42 {빵,계란,콜라}   => {우유} 0.4     1.0000000  1.0000000
## 43 {우유,계란,콜라} => {빵}   0.4     1.0000000  1.6666667
## 44 {우유,빵,계란}   => {콜라} 0.4     1.0000000  2.5000000
## 45 {우유,빵,콜라}   => {계란} 0.4     1.0000000  2.5000000

최소 지지도 0.6인 규칙만 추출

rule2<-apriori(trans, parameter=list(conf=0.6))
## 
## Parameter specification:
##  confidence minval smax arem  aval originalSupport support minlen maxlen
##         0.6    0.1    1 none FALSE            TRUE     0.1      1     10
##  target   ext
##   rules FALSE
## 
## Algorithmic control:
##  filter tree heap memopt load sort verbose
##     0.1 TRUE TRUE  FALSE TRUE    2    TRUE
## Warning in apriori(trans, parameter = list(conf = 0.6)): You chose a very low absolute support count of 0. You might run out of memory! Increase minimum support.
## apriori - find association rules with the apriori algorithm
## version 4.21 (2004.05.09)        (c) 1996-2004   Christian Borgelt
## set item appearances ...[0 item(s)] done [0.00s].
## set transactions ...[6 item(s), 5 transaction(s)] done [0.00s].
## sorting and recoding items ... [6 item(s)] done [0.00s].
## creating transaction tree ... done [0.00s].
## checking subsets of size 1 2 3 4 5 done [0.00s].
## writing ... [86 rule(s)] done [0.00s].
## creating S4 object  ... done [0.00s].
inspect(sort(rule2))
##    lhs                      rhs    support confidence lift     
## 4  {}                    => {우유} 1.0     1.0000000  1.0000000
## 3  {}                    => {사과} 0.8     0.8000000  1.0000000
## 21 {사과}                => {우유} 0.8     1.0000000  1.0000000
## 22 {우유}                => {사과} 0.8     0.8000000  1.0000000
## 1  {}                    => {버터} 0.6     0.6000000  1.0000000
## 2  {}                    => {빵}   0.6     0.6000000  1.0000000
## 16 {버터}                => {우유} 0.6     1.0000000  1.0000000
## 17 {우유}                => {버터} 0.6     0.6000000  1.0000000
## 19 {빵}                  => {우유} 0.6     1.0000000  1.0000000
## 20 {우유}                => {빵}   0.6     0.6000000  1.0000000
## 5  {계란}                => {콜라} 0.4     1.0000000  2.5000000
## 6  {콜라}                => {계란} 0.4     1.0000000  2.5000000
## 7  {계란}                => {빵}   0.4     1.0000000  1.6666667
## 8  {빵}                  => {계란} 0.4     0.6666667  1.6666667
## 9  {계란}                => {우유} 0.4     1.0000000  1.0000000
## 10 {콜라}                => {빵}   0.4     1.0000000  1.6666667
## 11 {빵}                  => {콜라} 0.4     0.6666667  1.6666667
## 12 {콜라}                => {우유} 0.4     1.0000000  1.0000000
## 13 {버터}                => {빵}   0.4     0.6666667  1.1111111
## 14 {빵}                  => {버터} 0.4     0.6666667  1.1111111
## 15 {버터}                => {사과} 0.4     0.6666667  0.8333333
## 18 {빵}                  => {사과} 0.4     0.6666667  0.8333333
## 25 {계란,콜라}           => {빵}   0.4     1.0000000  1.6666667
## 26 {빵,계란}             => {콜라} 0.4     1.0000000  2.5000000
## 27 {빵,콜라}             => {계란} 0.4     1.0000000  2.5000000
## 30 {계란,콜라}           => {우유} 0.4     1.0000000  1.0000000
## 31 {우유,계란}           => {콜라} 0.4     1.0000000  2.5000000
## 32 {우유,콜라}           => {계란} 0.4     1.0000000  2.5000000
## 36 {빵,계란}             => {우유} 0.4     1.0000000  1.0000000
## 37 {우유,계란}           => {빵}   0.4     1.0000000  1.6666667
## 38 {우유,빵}             => {계란} 0.4     0.6666667  1.6666667
## 43 {빵,콜라}             => {우유} 0.4     1.0000000  1.0000000
## 44 {우유,콜라}           => {빵}   0.4     1.0000000  1.6666667
## 45 {우유,빵}             => {콜라} 0.4     0.6666667  1.6666667
## 47 {빵,버터}             => {우유} 0.4     1.0000000  1.0000000
## 48 {우유,버터}           => {빵}   0.4     0.6666667  1.1111111
## 49 {우유,빵}             => {버터} 0.4     0.6666667  1.1111111
## 50 {사과,버터}           => {우유} 0.4     1.0000000  1.0000000
## 51 {우유,버터}           => {사과} 0.4     0.6666667  0.8333333
## 52 {사과,빵}             => {우유} 0.4     1.0000000  1.0000000
## 53 {우유,빵}             => {사과} 0.4     0.6666667  0.8333333
## 63 {빵,계란,콜라}        => {우유} 0.4     1.0000000  1.0000000
## 64 {우유,계란,콜라}      => {빵}   0.4     1.0000000  1.6666667
## 65 {우유,빵,계란}        => {콜라} 0.4     1.0000000  2.5000000
## 66 {우유,빵,콜라}        => {계란} 0.4     1.0000000  2.5000000
## 23 {버터,계란}           => {콜라} 0.2     1.0000000  2.5000000
## 24 {버터,콜라}           => {계란} 0.2     1.0000000  2.5000000
## 28 {사과,계란}           => {콜라} 0.2     1.0000000  2.5000000
## 29 {사과,콜라}           => {계란} 0.2     1.0000000  2.5000000
## 33 {버터,계란}           => {빵}   0.2     1.0000000  1.6666667
## 34 {버터,계란}           => {우유} 0.2     1.0000000  1.0000000
## 35 {사과,계란}           => {빵}   0.2     1.0000000  1.6666667
## 39 {사과,계란}           => {우유} 0.2     1.0000000  1.0000000
## 40 {버터,콜라}           => {빵}   0.2     1.0000000  1.6666667
## 41 {버터,콜라}           => {우유} 0.2     1.0000000  1.0000000
## 42 {사과,콜라}           => {빵}   0.2     1.0000000  1.6666667
## 46 {사과,콜라}           => {우유} 0.2     1.0000000  1.0000000
## 54 {버터,계란,콜라}      => {빵}   0.2     1.0000000  1.6666667
## 55 {빵,버터,계란}        => {콜라} 0.2     1.0000000  2.5000000
## 56 {빵,버터,콜라}        => {계란} 0.2     1.0000000  2.5000000
## 57 {버터,계란,콜라}      => {우유} 0.2     1.0000000  1.0000000
## 58 {우유,버터,계란}      => {콜라} 0.2     1.0000000  2.5000000
## 59 {우유,버터,콜라}      => {계란} 0.2     1.0000000  2.5000000
## 60 {사과,계란,콜라}      => {빵}   0.2     1.0000000  1.6666667
## 61 {사과,빵,계란}        => {콜라} 0.2     1.0000000  2.5000000
## 62 {사과,빵,콜라}        => {계란} 0.2     1.0000000  2.5000000
## 67 {사과,계란,콜라}      => {우유} 0.2     1.0000000  1.0000000
## 68 {사과,우유,계란}      => {콜라} 0.2     1.0000000  2.5000000
## 69 {사과,우유,콜라}      => {계란} 0.2     1.0000000  2.5000000
## 70 {빵,버터,계란}        => {우유} 0.2     1.0000000  1.0000000
## 71 {우유,버터,계란}      => {빵}   0.2     1.0000000  1.6666667
## 72 {사과,빵,계란}        => {우유} 0.2     1.0000000  1.0000000
## 73 {사과,우유,계란}      => {빵}   0.2     1.0000000  1.6666667
## 74 {빵,버터,콜라}        => {우유} 0.2     1.0000000  1.0000000
## 75 {우유,버터,콜라}      => {빵}   0.2     1.0000000  1.6666667
## 76 {사과,빵,콜라}        => {우유} 0.2     1.0000000  1.0000000
## 77 {사과,우유,콜라}      => {빵}   0.2     1.0000000  1.6666667
## 78 {사과,빵,버터}        => {우유} 0.2     1.0000000  1.0000000
## 79 {빵,버터,계란,콜라}   => {우유} 0.2     1.0000000  1.0000000
## 80 {우유,버터,계란,콜라} => {빵}   0.2     1.0000000  1.6666667
## 81 {우유,빵,버터,계란}   => {콜라} 0.2     1.0000000  2.5000000
## 82 {우유,빵,버터,콜라}   => {계란} 0.2     1.0000000  2.5000000
## 83 {사과,빵,계란,콜라}   => {우유} 0.2     1.0000000  1.0000000
## 84 {사과,우유,계란,콜라} => {빵}   0.2     1.0000000  1.6666667
## 85 {사과,우유,빵,계란}   => {콜라} 0.2     1.0000000  2.5000000
## 86 {사과,우유,빵,콜라}   => {계란} 0.2     1.0000000  2.5000000

좌측항목에 버터가 있고, 규칙의 리프트가 0.6 이상인 규칙추출

rule.sub_1<-subset(rule1, subset=lhs %pin% "버터" & lift>0.6)
inspect(sort(rule.sub_1))
##    lhs            rhs    support confidence lift     
## 16 {버터}      => {우유} 0.6     1.0000000  1.0000000
## 13 {버터}      => {빵}   0.4     0.6666667  1.1111111
## 15 {버터}      => {사과} 0.4     0.6666667  0.8333333
## 35 {빵,버터}   => {우유} 0.4     1.0000000  1.0000000
## 36 {우유,버터} => {빵}   0.4     0.6666667  1.1111111
## 38 {사과,버터} => {우유} 0.4     1.0000000  1.0000000
## 39 {우유,버터} => {사과} 0.4     0.6666667  0.8333333

우측항목에 버터가 있고, 규칙의 리프트가 0.6 이상인 규칙추출

rule.sub_2<-subset(rule1, subset=rhs %pin% "빵" & lift>0.6)
inspect(sort(rule.sub_2))
##    lhs                 rhs  support confidence lift    
## 2  {}               => {빵} 0.6     0.6000000  1.000000
## 20 {우유}           => {빵} 0.6     0.6000000  1.000000
## 7  {계란}           => {빵} 0.4     1.0000000  1.666667
## 10 {콜라}           => {빵} 0.4     1.0000000  1.666667
## 13 {버터}           => {빵} 0.4     0.6666667  1.111111
## 23 {계란,콜라}      => {빵} 0.4     1.0000000  1.666667
## 30 {우유,계란}      => {빵} 0.4     1.0000000  1.666667
## 33 {우유,콜라}      => {빵} 0.4     1.0000000  1.666667
## 36 {우유,버터}      => {빵} 0.4     0.6666667  1.111111
## 43 {우유,계란,콜라} => {빵} 0.4     1.0000000  1.666667


Archives

05-16 02:44

Contact Us

Address
경기도 수원시 영통구 원천동 산5번지 아주대학교 다산관 429호

E-mail
textminings@gmail.com

Phone
031-219-2910

Tags

Calendar

«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Copyright © All Rights Reserved
Designed by CMSFactory.NET