'연관규칙 시각화'에 해당되는 글 1건

  1. 2016.05.23 [R 연관규칙 (Association Rule)] R arules package로 연관규칙 분석하기 38

지난번 포스팅에서는 대용량 데이터로 부터 효율적으로 연관규칙(association rules)을 도출할 수 있는 apriori algorithm 에 대하여 알아보았습니다.

 

이번 포스팅에서는 R의 arules package를 가지고 분석하는 방법을 예를 들어서 설명하도록 하겠습니다. 

(그동안 선형대수랑 기계학습 이론 내용만 포스팅하다보니 R 사용법 포스팅한지가 너무 오래된거 같아요. ^^;)

 

arules package는 Apriori algorithm으로 구현되었 있습니다.

 

 

 

1. arules package 설치 및 library(arules)로 로딩

 

aruels package는 base package가 아니므로 별도 설치 필요합니다.

 

 

 > install.packages("arules")
Installing package into ‘C:/Users/Owner/Documents/R/win-library/3.2’
(as ‘lib’ is unspecified)
trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.2/arules_1.4-1.zip'
Content type 'application/zip' length 1885514 bytes (1.8 MB)
downloaded 1.8 MB

package ‘arules’ successfully unpacked and MD5 sums checked

The downloaded binary packages are in
C:\Users\Owner\AppData\Local\Temp\Rtmp4sJVWg\downloaded_packages
> library(arules)
필요한 패키지를 로딩중입니다: Matrix

다음의 패키지를 부착합니다: ‘arules’

The following objects are masked from ‘package:base’:

    abbreviate, write

Warning message:
패키지 ‘arules’는 R 버전 3.2.5에서 작성되었습니다

 

 

2. 데이터 확보 및 탐색

 

분석에 사용할 데이터는 Epub 거래 데이터셋 입니다.  Help에 'Epub' 을 검색해보면 아래와 같은 설명이 나옵니다. Vienna University of Economics and Business Administration에서의 2003~2008년까지 기간 동안 전자책 다운로드 이력/거래 데이터입니다.

 

> help(Epub)

 

 

Epub {arules}

R Documentation

Epub Data Set

Description

The Epub data set contains the download history of documents from the electronic publication platform of the Vienna University of Economics and Business Administration. The data was recorded between Jan 2003 and Dec 2008.

Usage

data(Epub)

Format

Object of class transactions with 15729 transactions and 936 items. Item labels are document IDs of the from "doc\_11d". Session IDs and time stamps for transactions are also provided.

Author(s)

Michael Hahsler

Source

Provided by Michael Hahsler from ePub-WU at http://epub.wu-wien.ac.at.

 

 

 

data(Epub)로 로딩하고 summary(Epub)로 데이터셋 요약정보를 살펴보겠습니다. 

- sparse format 형식으로 저장된 itemMatrix의 거래(transactions) 데이터셋이며,

- 15,729개의 행(즉, 거래)과 936개의 열(items)으로 구성되어 있습니다.

- 밀도(density)가 0.1758755% 라고 되어 있는데요, 전체 15729*936개의 cell 중에서 0.1758% 의 cell에 거래가 발생해서 숫자가 차 있다는 뜻입니다. (일부 책벌레 애독자가 한꺼번에 다수의 책을 사고, 대부분의 독자는 item 1개나 2개 단품 위주로 샀기 때문에 이렇게 밀도가 낮겠지요?)

- 'most frequent items' 는 거래 빈도가 가장 많은 top 5의 품목명과 거래빈도를 제시해주고 있습니다.

  (doc_11d 가 356회 거래 빈도 발생으로 1위) 

- 'element (itemset/transaction) length distribution' 은 하나의 거래 장바구니(즉, row 1개별로)에 품목(item)의 개수별로 몇 번의 거래가 있었는지를 나타냅니다.

(장바구니에 item 1개 짜리 단품만 거래한 경우가 11,615건으로서 가장 많고, item 2개 거래는 2,189건이군요)

- 마지막에 item 정보의 label 형식과 transaction ID, TimeStamp 정보의 format 예시가 나옵니다.

 

> data(Epub)
> summary(Epub)
transactions as itemMatrix in sparse format with
 15729 rows (elements/itemsets/transactions) and
 936 columns (items) and a density of 0.001758755 

most frequent items:
doc_11d doc_813 doc_4c6 doc_955 doc_698 (Other) 
    356     329     288     282     245   24393 

element (itemset/transaction) length distribution:
sizes
    1     2     3     4     5     6     7     8     9    10    11    12    13    14 
11615  2189   854   409   198   121    93    50    42    34    26    12    10    10 
   15    16    17    18    19    20    21    22    23    24    25    26    27    28 
    6     8     6     5     8     2     2     3     2     3     4     5     1     1 
   30    34    36    38    41    43    52    58 
    1     2     1     2     1     1     1     1 

   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  1.000   1.000   1.000   1.646   2.000  58.000 

includes extended item information - examples:
   labels
1 doc_11d
2 doc_13d
3 doc_14c

includes extended transaction information - examples:
    transactionID           TimeStamp
10792  session_4795 2003-01-02 10:59:00
10793  session_4797 2003-01-02 21:46:01
10794  session_479a 2003-01-03 00:50:38 

 

 

참고로, R을 분석에 많이 사용해본 분이라면 dataframe 형식의 데이터셋을 많이 사용해보셨을 텐데요, 연관규칙분석에서 사용할 itemMatrix in sparse format 형식의 데이터셋과 비교해보면 아래와 같이 차이가 있습니다.  위에서 Epub 데이터셋의 density가 0.1758%로서 item별 matrix cell의 거의 대부분이 숭숭 비어있다고 했는데요, 비어있는 cell까지 모두 저장하려면 메모리 비효율이 발생하므로 cell의 차있는 부분(즉, 거래발생 항목, nonzero elements)에 대해서만 효율적으로 데이터를 저장해놓는 방식이 itemMatrix in sparse format 형식입니다. 저장 효율이 좋아지는 대신 반대급부로 access하는 것이나 저장구조는 좀 복잡해집니다.   

(행렬의 대부분의 cell이 '0'이면 sparse matrix 라고 하며, 그 반대는 dense matrix 라고 합니다.)

 

csv 파일을 dataframe 으로 업로드할 때 read.csv() 함수를 사용하는 것처럼, transaction 데이터를 연관규칙분석을 위해 sparse format의 itemMatrix로 업로드하기 위해서는 read.transactions("dataset.csv") 함수를 사용합니다.

 

 

 

 

 

 

이번에는 inspect() 함수를 사용해서 거래 데이터 10개만 뽑아서 살펴보겠습니다.

 

 > ## check itemsets in sparse matrix

> inspect(Epub[1:10])
      items                    transactionID TimeStamp          
10792 {doc_154}                session_4795  2003-01-02 10:59:00
10793 {doc_3d6}                session_4797  2003-01-02 21:46:01
10794 {doc_16f}                session_479a  2003-01-03 00:50:38
10795 {doc_11d,doc_1a7,doc_f4} session_47b7  2003-01-03 08:55:50
10796 {doc_83}                 session_47bb  2003-01-03 11:27:44
10797 {doc_11d}                session_47c2  2003-01-04 00:18:04
10798 {doc_368}                session_47cb  2003-01-04 04:40:57
10799 {doc_11d,doc_192}        session_47d8  2003-01-04 09:00:01
10800 {doc_364}                session_47e2  2003-01-05 02:48:36
10801 {doc_ec}                 session_47e7  2003-01-05 05:58:48

 

 

 

다음으로 itemFrequency() 함수를 이용해서 거래품목(item)별로 거래에서 차지하는 비율(support)를 살펴보겠습니다. Epub[ , 1:10] 으로 앞에 10개만 indexing 해왔습니다.

 

> ## support per item: itemFrequency()
> itemFrequency(Epub[ , 1:10])
     doc_11d      doc_13d      doc_14c      doc_14e      doc_150      doc_151 
0.0226333524 0.0009536525 0.0024794965 0.0017801513 0.0015894208 0.0007629220 
     doc_153      doc_154      doc_155      doc_156 
0.0006357683 0.0013351135 0.0010808062 0.0031152648

 

 

 

itemFrequencyPlot(dataset, support = xx) 함수를 이용해서 지지도 1% 이상의 item에 대해 막대그래프를 그려보겠습니다.

 

> ## item frequency plot : itemFrequentPlot()
> itemFrequencyPlot(Epub, support = 0.01, main = "item frequency plot above support 1%")

 

 

 

이번에는 itemFrequencyPlot(dataset, topN = xx) 함수를 사용해서 support 상위 30개의 막대그래프를 그려보겠습니다. support 1등 item이 'doc_11d'이고 2~2.5% 사이로군요. 30등 tiem은 'doc_3ec'이고 support가 약 1% 이네요.

 

> ## item frequency plot top 30 : itemFrequencyPlot(,topN)
> itemFrequencyPlot(Epub, topN = 30, main = "support top 30 items")

 

 

 

 

아래는 image()함수sample()함수를 이용해서 500개의 무작위 샘플을 가지고 matrix diagram을 그려본 것입니다.  그림의 점들은 거래가 발생한 item을 의미합니다. 이 그림만 봐서는 어떤 패턴이 있는지 알기가 어렵지요? ^^' 

 

> # matrix diagram : image()
> image(sample(Epub, 500, replace = FALSE), main = "matrix diagram")

 

 

 

 

 

3. 연관규칙 분석 (association rule analysis)

 

arules 패키지의 apriori() 함수를 이용해서 연관규칙을 분석해보겠습니다.

 

parameter 에 list로 minimum support, minimum confidence, minimum length 를 지정해주면 이를 충족시키지 못하는 superset에 대해서는 pruning 을 해서 빠르게 연관규칙을 찾아줍니다.

 

그런데, 아래 예시에서는 minumum support = 0.01 로 했더니 기준이 너무 높았던지 연관규칙이 '0'개 나왔네요.

 

 

> ## association rule analysis : apriori()
> Epub_rule <- apriori(data = Epub, 
+                      parameter = list(support = 0.01, 
+                                       confidence = 0.20, 
+                                       minlen = 2))
Apriori

Parameter specification:
 confidence minval smax arem  aval originalSupport support minlen maxlen target   ext
        0.2    0.1    1 none FALSE            TRUE    0.01      2     10  rules FALSE

Algorithmic control:
 filter tree heap memopt load sort verbose
    0.1 TRUE TRUE  FALSE TRUE    2    TRUE

Absolute minimum support count: 157 

set item appearances ...[0 item(s)] done [0.00s].
set transactions ...[936 item(s), 15729 transaction(s)] done [0.00s].
sorting and recoding items ... [19 item(s)] done [0.00s].
creating transaction tree ... done [0.00s].
checking subsets of size 1 2 done [0.00s].
writing ... [0 rule(s)] done [0.00s].
creating S4 object  ... done [0.00s]

 

 

minumum support 기준을 0.001 로 낮추어서 다시 한번 분석을 해보겠습니다. 

아래처럼 결과가 나왔습니다.  연관규칙 분석은 기본 개념과 결과를 해석할 줄 알면 분석툴로 분석하는 것은 이처럼 매우 쉽습니다. (컴퓨터는 연산해야할 일이 엄청 많지만요...)

 

> # re-setting minimum support from 0.01 to 0.001
> Epub_rule_2 <- apriori(data = Epub, 
+                      parameter = list(support = 0.001, 
+                                       confidence = 0.20, 
+                                       minlen = 2))
Apriori

Parameter specification:
 confidence minval smax arem  aval originalSupport support minlen maxlen target   ext
        0.2    0.1    1 none FALSE            TRUE   0.001      2     10  rules FALSE

Algorithmic control:
 filter tree heap memopt load sort verbose
    0.1 TRUE TRUE  FALSE TRUE    2    TRUE

Absolute minimum support count: 15 

set item appearances ...[0 item(s)] done [0.00s].
set transactions ...[936 item(s), 15729 transaction(s)] done [0.00s].
sorting and recoding items ... [481 item(s)] done [0.00s].
creating transaction tree ... done [0.02s].
checking subsets of size 1 2 3 done [0.00s].
writing ... [65 rule(s)] done [0.00s].
creating S4 object  ... done [0.00s]

 

> Epub_rule_2
set of 65 rules

 

 

 

4. 연관규칙 조회 및 평가

 

연관규칙을 Epub_rule 이라는 객체에 저장을 해두었으므로, summary() 함수를 써서 연관규칙에 대해 개략적으로 파악을 해보면 아래와 같습니다. 

 

62개 rule이 2개 item으로 이루어져 있고, 3개 rule은 3개 item으로 구성되있군요. 지지도(support), 신뢰도(confidence), 향상도(lift)에 대한 기초통계량도 같이 제시가 되었는데요, 향상도 최소값이 11.19로서 전반적으로 꽤 높군요.

 

> summary(Epub_rule_2)
set of 65 rules

rule length distribution (lhs + rhs):sizes
 2  3 
62  3 

   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  2.000   2.000   2.000   2.046   2.000   3.000 

summary of quality measures:
    support           confidence          lift       
 Min.   :0.001017   Min.   :0.2048   Min.   : 11.19  
 1st Qu.:0.001081   1st Qu.:0.2388   1st Qu.: 34.02  
 Median :0.001208   Median :0.2874   Median : 59.47  
 Mean   :0.001435   Mean   :0.3571   Mean   :105.16  
 3rd Qu.:0.001526   3rd Qu.:0.3696   3rd Qu.:100.71  
 Max.   :0.004069   Max.   :0.8947   Max.   :454.75  

mining info:
 data ntransactions support confidence
 Epub         15729   0.001        0.2

 

 

 

연관규칙을 평가하기 위해 개별 규칙(rule)을 inspect()함수를 사용해서 살펴보겠습니다.  아래 결과에 1~20개의 rule을 제시했는데요, lhs : Left-hand side, rhs : Right-hand side 를 의미합니다.

 

> # inspection of 1~20 association rules : inspect() > inspect(Epub_rule_2[1:20]) lhs rhs support confidence lift 1 {doc_506} => {doc_507} 0.001207960 0.6551724 303.09432 2 {doc_507} => {doc_506} 0.001207960 0.5588235 303.09432 3 {doc_470} => {doc_4c6} 0.001080806 0.2048193 11.18612 4 {doc_714} => {doc_574} 0.001080806 0.3695652 113.97826 5 {doc_574} => {doc_714} 0.001080806 0.3333333 113.97826 6 {doc_4bf} => {doc_4ac} 0.001080806 0.5000000 77.10294 7 {doc_6e9} => {doc_6e8} 0.001207960 0.6785714 333.53906 8 {doc_6e8} => {doc_6e9} 0.001207960 0.5937500 333.53906 9 {doc_6e9} => {doc_6e7} 0.001271537 0.7142857 321.00000 10 {doc_6e7} => {doc_6e9} 0.001271537 0.5714286 321.00000 11 {doc_749} => {doc_74a} 0.001017229 0.3555556 86.03897 12 {doc_74a} => {doc_749} 0.001017229 0.2461538 86.03897 13 {doc_6e8} => {doc_6e7} 0.001335113 0.6562500 294.91875 14 {doc_6e7} => {doc_6e8} 0.001335113 0.6000000 294.91875 15 {doc_3d6} => {doc_3c4} 0.001144383 0.2465753 79.15068 16 {doc_3c4} => {doc_3d6} 0.001144383 0.3673469 79.15068 17 {doc_3d6} => {doc_4b4} 0.001017229 0.2191781 51.45451 18 {doc_4b4} => {doc_3d6} 0.001017229 0.2388060 51.45451 19 {doc_3c4} => {doc_574} 0.001017229 0.3265306 100.70588 20 {doc_574} => {doc_3c4} 0.001017229 0.3137255 100.70588

 

 

 

위처럼 주욱 나열을 해놓으면 rule이 수백, 수천개가 되면 일일이 눈으로 보고 평가하기가 쉽지가 않습니다.  봐야할 rule이 많을 때는 sort() 함수를 써서 분석가가 보고자하는 기준에 맞게 by 매개변수로 정렬을 하여 우선순위를 뒤서 보면 유용합니다.  아래 예는 lift 를 기준으로 상위 20개 연관규칙을 정렬해보았습니다.  매우 유용하지요?!!

 

 

> # sorting association rules by lift : sort( , by = "lift") > inspect(sort(Epub_rule_2, by = "lift")[1:20]) lhs rhs support confidence lift 65 {doc_6e7,doc_6e8} => {doc_6e9} 0.001080806 0.8095238 454.75000 64 {doc_6e7,doc_6e9} => {doc_6e8} 0.001080806 0.8500000 417.80156 63 {doc_6e8,doc_6e9} => {doc_6e7} 0.001080806 0.8947368 402.09474 7 {doc_6e9} => {doc_6e8} 0.001207960 0.6785714 333.53906 8 {doc_6e8} => {doc_6e9} 0.001207960 0.5937500 333.53906 9 {doc_6e9} => {doc_6e7} 0.001271537 0.7142857 321.00000 10 {doc_6e7} => {doc_6e9} 0.001271537 0.5714286 321.00000 1 {doc_506} => {doc_507} 0.001207960 0.6551724 303.09432 2 {doc_507} => {doc_506} 0.001207960 0.5588235 303.09432 13 {doc_6e8} => {doc_6e7} 0.001335113 0.6562500 294.91875 14 {doc_6e7} => {doc_6e8} 0.001335113 0.6000000 294.91875 39 {doc_87c} => {doc_882} 0.001335113 0.6000000 171.58909 40 {doc_882} => {doc_87c} 0.001335113 0.3818182 171.58909 4 {doc_714} => {doc_574} 0.001080806 0.3695652 113.97826 5 {doc_574} => {doc_714} 0.001080806 0.3333333 113.97826 20 {doc_574} => {doc_3c4} 0.001017229 0.3137255 100.70588 19 {doc_3c4} => {doc_574} 0.001017229 0.3265306 100.70588 22 {doc_4b4} => {doc_3c4} 0.001207960 0.2835821 91.02985 21 {doc_3c4} => {doc_4b4} 0.001207960 0.3877551 91.02985 11 {doc_749} => {doc_74a} 0.001017229 0.3555556 86.03897

 

 

 

아래는 정렬 기준을 '지지도(support)'로 해서 top 20 연관규칙을 뽑아본 것입니다.  편하고 좋지요?!

 

> # sorting association rules by support : sort(, by = "support")
> inspect(sort(Epub_rule_2, by = "support")[1:20])
   lhs          rhs       support     confidence lift     
50 {doc_72f} => {doc_813} 0.004068917 0.3516484   16.81178
45 {doc_4ac} => {doc_16e} 0.002797381 0.4313725   53.42566
46 {doc_16e} => {doc_4ac} 0.002797381 0.3464567   53.42566
62 {doc_364} => {doc_71}  0.002733804 0.2336957   15.91255
60 {doc_60e} => {doc_6bf} 0.002670227 0.2745098   21.06227
61 {doc_6bf} => {doc_60e} 0.002670227 0.2048780   21.06227
49 {doc_972} => {doc_8f9} 0.002161612 0.2281879   18.69358
58 {doc_1a2} => {doc_4c7} 0.002098035 0.2391304   17.99657
56 {doc_424} => {doc_359} 0.001843728 0.3020833   44.40625
57 {doc_359} => {doc_424} 0.001843728 0.2710280   44.40625
47 {doc_4da} => {doc_84b} 0.001780151 0.2314050   34.01653
48 {doc_84b} => {doc_4da} 0.001780151 0.2616822   34.01653
52 {doc_8a8} => {doc_8af} 0.001716574 0.2903226   47.07715
53 {doc_8af} => {doc_8a8} 0.001716574 0.2783505   47.07715
27 {doc_803} => {doc_3fc} 0.001589421 0.3289474   59.47142
28 {doc_3fc} => {doc_803} 0.001589421 0.2873563   59.47142
55 {doc_466} => {doc_19f} 0.001525844 0.2264151   25.80640
59 {doc_359} => {doc_4c7} 0.001398690 0.2056075   15.47368
13 {doc_6e8} => {doc_6e7} 0.001335113 0.6562500  294.91875
14 {doc_6e7} => {doc_6e8} 0.001335113 0.6000000  294.91875

 

 

 

 

또 하나 유용한 tip이 있다면 subset() 함수를 써서 관심이 있는 item이 포함된 연관규칙만 선별해서 보는 방법입니다.  subset()함수를 이용해 연관규칙에서 "doc_72f"나 "doc_4ac"를 포함하는 규칙을 선별하는 방법은 아래와 같습니다.

 

 

> # subset of association rules : subset()
> rule_interest <- subset(Epub_rule_2, items %in% c("doc_72f", "doc_4ac"))
> inspect(rule_interest)
   lhs          rhs       support     confidence lift    
6  {doc_4bf} => {doc_4ac} 0.001080806 0.5000000  77.10294
45 {doc_4ac} => {doc_16e} 0.002797381 0.4313725  53.42566
46 {doc_16e} => {doc_4ac} 0.002797381 0.3464567  53.42566
50 {doc_72f} => {doc_813} 0.004068917 0.3516484  16.81178

 

 

 

연관규칙을 찾을 때 왼쪽(lhs : Left-hand side)이나 오른쪽(rhs: Right-hand side)을 기준으로 원하는 항목(item)이 포함된 규칙만 찾고 싶을 때는 아래와 같이 lhs 나 rhs 조건을 주면 됩니다.

 

> # subset with left-hand side item : subset(lhs %in% "item")
> rule_interest_lhs <- subset(Epub_rule_2, lhs %in% c("doc_72f", "doc_4ac"))
> inspect(rule_interest_lhs)
   lhs          rhs       support     confidence lift    
45 {doc_4ac} => {doc_16e} 0.002797381 0.4313725  53.42566
50 {doc_72f} => {doc_813} 0.004068917 0.3516484  16.81178 

 

 

 

위에서는 사용한 %in% (select itemsets matching any given item) 조건은 적어도 하나의 제품이라도 존재하면 연관규칙을 indexing해온다는 뜻입니다.  이에 반해 %pin% (partial matching) 는 부분 일치만 하더라도, %ain% (select only itemsets matching all given item) 는 완전한 일치를 할 때만 indexing을 하게 됩니다. 

 

아래에 item 이름에 부분적으로라도 "60e"라는 철자가 들어가 있는 item이 들어가 있는 연관규칙을 부분집합으로 indexing해오는 예를 들어보겠습니다.  이 기능도 꽤 유용하겠지요?

 

> # partial subset : %pin%
> rule_interest_pin <- subset(Epub_rule_2, items %pin% c("60e"))
> inspect(rule_interest_pin)
   lhs          rhs       support     confidence lift    
60 {doc_60e} => {doc_6bf} 0.002670227 0.2745098  21.06227
61 {doc_6bf} => {doc_60e} 0.002670227 0.2048780  21.06227

 

 

 

 

Rule의 왼쪽에 "doc_6e8"과 "doc_6e9" item을 동시에 정확히 가지고 있는 rule을 "ain" 을 사용해서 선별해보면 아래와 같습니다.

 

> rule_interest_lhs_ain <- subset(Epub_rule_2, lhs %ain% c("doc_6e8", "doc_6e9"))
> inspect(rule_interest_lhs_ain)
    lhs                  rhs       support     confidence lift    
[1] {doc_6e8,doc_6e9} => {doc_6e7} 0.001080806 0.8947368  402.094

 

 

 

 

support, confidence, lift 조건을 추가해서 부분집합(subset)을 취할 수도 있습니다.  아래 예는 신뢰도(confidence) 0.25 초과하는 rule 을 선별하라는 조건을 추가하였습니다.  참 편하지요?!

 

> # partial subset with confidence condition : %pin%, confidence
> rule_interest_pin_conf <- subset(Epub_rule_2, items %pin% c("60e") & confidence > 0.25)
> inspect(rule_interest_pin_conf)
   lhs          rhs       support     confidence lift    
60 {doc_60e} => {doc_6bf} 0.002670227 0.2745098  21.06227 

 

 

 

 

5. 연관규칙의 시각화 : arulesViz package

 

arulesViz package를 사용해서 연관규칙을 시각화해보겠습니다.

 

 

  • Scatter plot for association rules 

> install.packages("arulesViz")
> library(arulesViz)

>

> # scatter plot of association rules
> plot(Epub_rule_2)

 

 

 

 

  • Grouped matrix for assocation rules

> # grouped matrix for association rules
> plot(sort(Epub_rule_2, by = "support")[1:20], method = "grouped")

 

* 65개 rule을 모두 그리니깐 너무 작게 나와서 support 기준 상위 20개만 선별해서 그렸음

 

 

  • Network Graph for assocation rules

참고로 아래 그래프의 원은 item element가 아니라 {item} → {item} 연관규칙의 지지도(support)를 나타냅니다. 지지도에 따라 원의 크기가 비례합니다. 색깔은 향상도(Lift)를 나타냅니다. 색깔이 진할수록 향상도가 높습니다. 그런데 화살표(from lhs to rhs)가 그려지다 말아서 그래프가 영... 어색합니다. -_-???

 

 

> # Graph for association rules
> plot(Epub_rule_2, method = "graph", control = list(type="items"))

 

 

 


위의 65개 연관규칙 그래프의 라벨 글자 크기(label font size, default = 1)를 줄이고, 화살표 크기(arrow size, default = 1)도 조금 잘게 해보겠습니다.   네트워크 그래프 그릴 때 사용하는 igraph 패키지의 파라미터 설정 방법 사용하면 됩니다. 위의 그래프보다는 아주 조금 더 나은거 같기는 한데요, 많이 좋아보인다는 느낌은 안드네요. ^^;; (그래프 그릴 때마다 위치가 조금씩 달라지므로 제가 화면 캡쳐해놓은 거랑은 아마 다르게 그려질 거예요)



> # changing font size(vertex.label.cex), arrow 

> plot(Epub_rule_2, method = "graph", 

+      control = list(type="items"), 

+      vertex.label.cex = 0.7, 

+      edge.arrow.size = 0.3,

+      edge.arrow.width = 2)

 


Edge : 선 관련 파라미터

 Vertex : 점 관련 파라미터

- edge.color : 지정 (default = "darkgrey")

- edge.width :

- edge.arrow.size : 화살 크기

- edge.arrow.width : 화살

- edge.arrow.mode : 화살 머리 유형 (0 : 없음,  1 : 역방향,  2 : 순방향,   3 : 양방향)

(무방향 네트워크의 경우 default = 0)

 

- edge.lty : 유형 ("solid", "dashed", "dotted", "dotdash", "longdash", "twodash")

- edge.label : 레이블

- edge.label.family : 레이블 종류 ("serif", "sans", "mono" )

- edge.label.font : 레이블 글자형 (1 : plain text, 2 : bold, 3 : italic, 4 : bold italic)

- edge.label.cex : 레이블 크기 (default = 1)

- edge.label.color : 레이블 (default = "navy") 

 - vertex.size : 크기, vector 가능 (default = 15)

- vertex.color : (default = "SkyBlue2")

- vertex.frame.color : 윤곡의 (default = "black")

- vertex.shape : 형태 ("circle", "square", "rectangle", "none", default = "circle")

 

- vertex.label : 레이블 (vector)

- vertex.label.family : 레이블 종류 ("serif", "sans", "mono" )

- vertex.label.font : 레이블 글자형 (1 : plain text, 2 : bold, 3 : italic, 4 : bold italic)

- vertex.label.cex : 레이블 크기 (default = 1)

- vertex.label.dist : 중심과 레이블 거리 (default = 0)

- vertex.label.degree : 레이블 방향(radian) ( : 0, : pi, : -pi/2, : pi/2)

- vertex.label.color : 레이블 (default = "navy")

* igraph 참고 : http://rfriend.tistory.com/221





Network graph for association rules 그래프 해석을 좀더 쉽게 할 수 있도록 아래 그래프의 몇 개의 item 간 연관규칙 그래프 옆에다가 association rule(left-hand => right-hand rule, support, confidence, lift) 분석 결과를 매핑해서 표시를 해보았습니다.  item 과 item 의 연관규칙 관계를 화살표로 나타내구요, 원의 크기는 지지도, 원의 색깔은 향상도를 나타냅니다. 


  • 화살표 (arrow) : left-hand => right-hand rule 의 방향(direction from left to right)을 나타냄
  • 원의 크기 (circle size) : 지지도(support)에 비례해서 커짐
  • 원의 색깔 (circle color) : 향상도(lift)에 비례해서 색이 진해짐





연관규칙 그래프 시각화를 좀더 이쁘게 하려면 연관규칙 개수를 20개 이내로 줄여주면 됩니다. 아래는 연관규칙 55~65번째 (11개 규칙) 만 선별해서 type = "items" 로 그려본 것인데요, label 글자 크기나 화살표 등이 전혀 거슬리지 않고 자연스럽지요?


[ 그림 1]  Graph-based visualization with item and rules as vertices



> plot(Epub_rule_2[55:65], method = "graph", control = list(type="items"))



 




type = "itemsets" 으로 설정을 해주면 아래의 그래프 처럼 "item-set" (가령, {doc_6e8, doc_6e9}, {doc_6e7, doc_6e9} 처럼 여러개 item 들이 들어 있는 바구니) 끼리의 연관규칙 관계를 시각화해줍니다. 

  • 화살표 두께(width) : item-set 간 연관규칙 지지도(support)에 비례하여 커짐
  • 화살표 색깔(color) : item-set 간 연관규칙 향상도(lift)에 비례하여 진해짐

type = "items" (위의 [그림1]) 일 때와 type = "itemsets" (아래의 [그림 2]) 일때의 동그라미로 표시해 둔 부분을 유심히 비교해서 살펴보시면 서로 연관규칙을 어떻게 표현하는 것인지 이해할 수 있을 것입니다. 서로 장단점이 있지요? 



[ 그림 2]  Graph-based visualization with item-sets as vertices



> plot(Epub_rule_2[55:65], method="graph", control=list(type="itemsets"))

 






 

6. 연관규칙을 CSV 파일로 저장

 

마지막으로 write() 함수를 사용해서 분석한 연관규칙을 CSV 파일로 저장해보겠습니다.  나중에 엑셀로 불러다가 후속 분석/작업하는데 필요할 수도 있겠지요?  file = "경로" 지정할 때 경로구분표시가 '\'가 아니라 '/' 로 해주어야 하는 것에 주의하시기 바랍니다. Windows 탐색기의 경로 그대로 복사해다가 붙이면 경로구분표시가 '\' 되어 있어서 오류납니다.

 

> # saving in CSV format : write() > write(Epub_rule_2, + file = "C:/Users/Owner/Documents/R/Epub_rule.csv", + sep = ",", + quote = TRUE, + row.names = FALSE)

 

 

 

마지막으로 as() 함수를 이용하여 연관규칙을 데이터프레임(dataframe) 구조로 변환해서 저장해보겠습니다.  연관규칙이 데이터프레임으로 되어있으면 다른 분석할 때 가져다 쓰기에 편하겠지요?

 

> # transforming into dataframe
> Epub_rule_df <- as(Epub_rule_2, "data.frame")

 

 

> str(Epub_rule_df)
'data.frame':	65 obs. of  4 variables:
 $ rules     : Factor w/ 65 levels "{doc_16e} => {doc_4ac}",..: 22 23 14 44 26 20 43 41 42 38 ...
 $ support   : num  0.00121 0.00121 0.00108 0.00108 0.00108 ...
 $ confidence: num  0.655 0.559 0.205 0.37 0.333 ...
 $ lift      : num  303.1 303.1 11.2 114 114 ...

 

 

 

데이터프레임으로 만들었으니 이전 포스팅에서 배웠던 IS(Interest-Support) Measure 와 교차지지도(cross support) 흥미측도를 생성할 수 있습니다.  R arules 패키지에서 자동으로 생성해주는 것은 지지도(support), 신뢰도(confidence), 향상도(lift) 3개뿐이다 보니 IS측도나 교차지지도는 직접 코딩해서 계산해줘야 합니다. 

 

아래 예시는 IS 기준으로 내림차순한 다음에 IS측도 상위 10개만 indexing한 것입니다.  (교차지지도는 최소지지도, 최대지지도 구해서 나눠줘야 하는 복잡함이 있으므로 패쓰... ^^; )

 

> # IS(Interest-Support) measure = sqrt(lift(A,B)*support(A,B))
> Epub_rule_df <- transform(Epub_rule_df, IS = sqrt(lift*support))
> Epub_rule_df[order(-Epub_rule_df$IS), ][1:10, ]
                            rules     support confidence     lift        IS
65 {doc_6e7,doc_6e8} => {doc_6e9} 0.001080806  0.8095238 454.7500 0.7010682
64 {doc_6e7,doc_6e9} => {doc_6e8} 0.001080806  0.8500000 417.8016 0.6719840
63 {doc_6e8,doc_6e9} => {doc_6e7} 0.001080806  0.8947368 402.0947 0.6592317
9          {doc_6e9} => {doc_6e7} 0.001271537  0.7142857 321.0000 0.6388766
10         {doc_6e7} => {doc_6e9} 0.001271537  0.5714286 321.0000 0.6388766
7          {doc_6e9} => {doc_6e8} 0.001207960  0.6785714 333.5391 0.6347454
8          {doc_6e8} => {doc_6e9} 0.001207960  0.5937500 333.5391 0.6347454
13         {doc_6e8} => {doc_6e7} 0.001335113  0.6562500 294.9187 0.6274950
14         {doc_6e7} => {doc_6e8} 0.001335113  0.6000000 294.9187 0.6274950
1          {doc_506} => {doc_507} 0.001207960  0.6551724 303.0943 0.6050833

 

 

 

이상으로 R arules 패키지를 사용해서 연관규칙 분석하는 방법을 마치도록 하겠습니다.

 

다음번 포스팅에서는 범주형 데이터의 연관분석에 대하여 알아보도록 하겠습니다.

 

이번 포스팅이 도움이 되었다면 아래의 '공감 ~♡'를 꾸욱 눌러주세요.

 


 

 

연관규칙 분석을 위해 transactions format으로 데이터를 변환하는 방법에 대한 질문이 자주 있어서 아래와 같이 데이터 유형별로 예를 들어 정리하였습니다. 참고하세요.

 

 

##==== [참고] List를 transactons format 으로 변환하기 ====

 

 

> ##------------------------------------------
> ## List -> transactions 자료로 변환
> ##------------------------------------------
> # transaction list
> tr_list <- list(c("a", "b"), 
+                 c("a", "c"), 
+                 c("b", "c", "d"), 
+                 c("a", "e"), 
+                 c("c", "d", "e"))
> # set transaction names
> names(tr_list) <- paste("tr", c(1:5), sep = "_")
> tr_list
$tr_1
[1] "a" "b"

$tr_2
[1] "a" "c"

$tr_3
[1] "b" "c" "d"

$tr_4
[1] "a" "e"

$tr_5
[1] "c" "d" "e"

> tr <- as(tr_list, "transactions")
> tr
transactions in sparse format with
 5 transactions (rows) and
 5 items (columns)
> summary(tr)
transactions as itemMatrix in sparse format with
 5 rows (elements/itemsets/transactions) and
 5 columns (items) and a density of 0.48 

most frequent items:
      a       c       b       d       e (Other) 
      3       3       2       2       2       0 

element (itemset/transaction) length distribution:
sizes
2 3 
3 2 

   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
    2.0     2.0     2.0     2.4     3.0     3.0 

includes extended item information - examples:
  labels
1      a
2      b
3      c

includes extended transaction information - examples:
  transactionID
1          tr_1
2          tr_2
3          tr_3

 

 

 

 

##==== [참고] Matrix를 transactions format 으로 변환하기 ====

 

 

> ##-------------------------------------------
> ## Matrix -> transactions 자료로 변환
> ##-------------------------------------------
> tr_matrix <- matrix(c(1, 1, 0, 0, 0, 
+                       1, 0, 1, 0, 0, 
+                       0, 1 , 1, 1, 0, 
+                       1, 0, 0, 0, 1, 
+                       0, 0, 1, 1, 1), 
+                     ncol = 5)
> # set dim names
> dimnames(tr_matrix) <- list(c("a", "b", "c", "d", "e"),
+ paste("tr", c(1:5), sep = "_"))
> tr_matrix
  tr_1 tr_2 tr_3 tr_4 tr_5
a    1    1    0    1    0
b    1    0    1    0    0
c    0    1    1    0    1
d    0    0    1    0    1
e    0    0    0    1    1
> # coerce into transactions
> tr2 <- as(tr_matrix, "transactions")
> tr2
transactions in sparse format with
 5 transactions (rows) and
 5 items (columns)
> summary(tr2)
transactions as itemMatrix in sparse format with
 5 rows (elements/itemsets/transactions) and
 5 columns (items) and a density of 0.48 

most frequent items:
   tr_3    tr_5    tr_1    tr_2    tr_4 (Other) 
      3       3       2       2       2       0 

element (itemset/transaction) length distribution:
sizes
2 3 
3 2 

   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
    2.0     2.0     2.0     2.4     3.0     3.0 

includes extended item information - examples:
  labels
1   tr_1
2   tr_2
3   tr_3

includes extended transaction information - examples:
  transactionID
1             a
2             b
3             c

 

 

 

 

##==== [참고] DataFrame을 transactions format 으로 변환하기 ( 1 ) ====

 

 

> ##------------------------------------------------
> ## data.frame -> transactions 자료로 변환
> ##------------------------------------------------
> tr_dataframe <- data.frame(
+   age = as.factor(c("30대", "20대", "30대", "40대", "10대")), 
+   grade = as.factor(c("A", "B", "A", "A", "C")))
> 
> # coerce into transactions
> tr3 <- as(tr_dataframe, "transactions")
> 
> tr3
transactions in sparse format with
 5 transactions (rows) and
 7 items (columns)
> summary(tr3)
transactions as itemMatrix in sparse format with
 5 rows (elements/itemsets/transactions) and
 7 columns (items) and a density of 0.2857143 

most frequent items:
 grade=A age=30대 age=10대 age=20대 age=40대  (Other) 
       3        2        1        1        1        2 

element (itemset/transaction) length distribution:
sizes
2 
5 

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

includes extended item information - examples:
    labels variables levels
1 age=10대       age   10대
2 age=20대       age   20대
3 age=30대       age   30대

includes extended transaction information - examples:
  transactionID
1             1
2             2
3             3 

 

 

 

##==== [참고] DataFrame을 transactions format 으로 변환하기 ( 2 ) =====

 

 

> ## as(split([dadaframe[,"itemID"], dataframe[,"transactionID"]), "transactions") 함수
> ##-- making data.frame
> transactionID <- c(rep("tr1", 3), rep("tr2", 4))
> itemID <- c("item1", "item2", "item3", "item1", "item2", "item4", "item5")
> tr_df <- data.frame(transactionID, itemID)
> str(tr_df)
'data.frame': 7 obs. of  2 variables:
$ transactionID: Factor w/ 2 levels "tr1","tr2": 1 1 1 2 2 2 2
$ itemID       : Factor w/ 5 levels "item1","item2",..: 1 2 3 1 2 4 5
>
> ## converting data.frame to transactions format
> tr <- as(split(tr_df[,"itemID"], tr_df[,"transactionID"]), "transactions")
> inspect(tr)
    items                     transactionID
[1] {item1,item2,item3}       tr1         
[2] {item1,item2,item4,item5} tr2 

 


## ==== [참고] TimeStamp 가 있는 거래 데이터 text 파일을 읽어와서 Sequence analysis를 할 수 있는 transactions format 으로 변환하기 

* 예제 거래 데이터 text 파일: 

tr.txt


* 순차규칙분석(Sequence analysis)는 https://rfriend.tistory.com/195 를 참고하시기 바랍니다. 


아래와 같은 거래 데이터 text 파일이 있다고 하겠습니다. 변수 이름은 순서대로 "X", "sequenceID", "eventID", "SIZE", "item" 입니다. 



0 GSFS1480140045034589400 2019-02-13 1 "TV"

1 GSFS1480140045034589400 2019-03-19 1 "AC"

2 GSFS1480140045034589400 2019-08-01 1 "CA"

3 GSFS1480140045034589400 2019-11-13 1 "RF"

4 GSFS1480151310065913100 2019-04-18 1 "AC"

5 GSFS1480151310065913100 2019-06-15 1 "AC, TV"

 



위의 거래 데이터 text 파일을 나중에 순차분석(Sequence analysis)을 할 수 있는 transactions format 으로 불러오려면 "arulesSequences" 패키지의 read_baskets() 함수를 사용하면 되는데요, 이때 구분자(seperator)를 지정해주고 (이 예에서는 1 space " "), 라벨 정보(label information) 을 지정해주어야 합니다. (제일 마지막의 "item" 이름은 생략). 만약 read_baskets() 함수 안에서 info = c() 를 지정해주지 않으면 칼럼 하나에 전체 데이터가 모두 들어가게 되어 이후의 순차분석을 진행할 수 없습니다. 


eventID 가 요인형(factor)으로 불어와졌으므로 시간 순서를 부여하기 위해 as.Date() 함수를 사용하여 날짜형 변수로 변환해주었습니다. 


transactions format으로 준비가 되었으면 cspade() 함수를 사용하여 순차분석을 실시해 순차 규칙을 도출하고, summary() 와 as(seq_rule, "data.frame") 으로 순차 규칙을 보기 좋게 출력해보았습니다.  



# install and load arulesSequences package

install.packages("arulesSequences")

library("arulesSequences")


# read text file with label information

> tr <- read_baskets(con = "/Users/ihongdon/Downloads/tr.txt", 

+                    sep = " ", 

+                    info = c("X", "sequenceID", "eventID", "SIZE"))

Warning message:

In read_baskets(con = "/Users/ihongdon/Downloads/tr.txt", sep = " ",  :

  'eventID' is a factor

> str(tr)

Formal class 'transactions' [package "arules"] with 3 slots

  ..@ data       :Formal class 'ngCMatrix' [package "Matrix"] with 5 slots

  .. .. ..@ i       : int [1:7] 4 0 2 3 0 1 5

  .. .. ..@ p       : int [1:7] 0 1 2 3 4 5 7

  .. .. ..@ Dim     : int [1:2] 6 6

  .. .. ..@ Dimnames:List of 2

  .. .. .. ..$ : NULL

  .. .. .. ..$ : NULL

  .. .. ..@ factors : list()

  ..@ itemInfo   :'data.frame': 6 obs. of  1 variable:

  .. ..$ labels: chr [1:6] "\"AC\"" "\"AC," "\"CA\"" "\"RF\"" ...

  ..@ itemsetInfo:'data.frame': 6 obs. of  4 variables:

  .. ..$ X         : int [1:6] 0 1 2 3 4 5

  .. ..$ sequenceID: Factor w/ 2 levels "GSFS1480140045034589400",..: 1 1 1 1 2 2

  .. ..$ eventID   : Factor w/ 6 levels "2019-02-13","2019-03-19",..: 1 2 5 6 3 4

  .. ..$ SIZE      : int [1:6] 1 1 1 1 1 1

> as(tr, "data.frame")

       items X              sequenceID    eventID SIZE

1     {"TV"} 0 GSFS1480140045034589400 2019-02-13    1

2     {"AC"} 1 GSFS1480140045034589400 2019-03-19    1

3     {"CA"} 2 GSFS1480140045034589400 2019-08-01    1

4     {"RF"} 3 GSFS1480140045034589400 2019-11-13    1

5     {"AC"} 4 GSFS1480151310065913100 2019-04-18    1

6 {"AC,,TV"} 5 GSFS1480151310065913100 2019-06-15    1

> # transforming eventID from factor to Date type for sequence analysis

> transactionInfo(tr)$eventID <- as.Date(transactionInfo(tr)$eventID, origin="2015-04-01")

> str(tr)

Formal class 'transactions' [package "arules"] with 3 slots

  ..@ data       :Formal class 'ngCMatrix' [package "Matrix"] with 5 slots

  .. .. ..@ i       : int [1:7] 4 0 2 3 0 1 5

  .. .. ..@ p       : int [1:7] 0 1 2 3 4 5 7

  .. .. ..@ Dim     : int [1:2] 6 6

  .. .. ..@ Dimnames:List of 2

  .. .. .. ..$ : NULL

  .. .. .. ..$ : NULL

  .. .. ..@ factors : list()

  ..@ itemInfo   :'data.frame': 6 obs. of  1 variable:

  .. ..$ labels: chr [1:6] "\"AC\"" "\"AC," "\"CA\"" "\"RF\"" ...

  ..@ itemsetInfo:'data.frame': 6 obs. of  4 variables:

  .. ..$ X         : int [1:6] 0 1 2 3 4 5

  .. ..$ sequenceID: Factor w/ 2 levels "GSFS1480140045034589400",..: 1 1 1 1 2 2

  .. ..$ eventID   : Date[1:6], format: "2019-02-13" "2019-03-19" "2019-08-01" ...

  .. ..$ SIZE      : int [1:6] 1 1 1 1 1 1

>

>

> transactionInfo(tr)

  X              sequenceID    eventID SIZE

1 0 GSFS1480140045034589400 2019-02-13    1

2 1 GSFS1480140045034589400 2019-03-19    1

3 2 GSFS1480140045034589400 2019-08-01    1

4 3 GSFS1480140045034589400 2019-11-13    1

5 4 GSFS1480151310065913100 2019-04-18    1

6 5 GSFS1480151310065913100 2019-06-15    1

> # Sequence Rule analysis

> seq_rule <- cspade(tr, 

+                    parameter = list(support = 0.1, maxsize = 5, maxlen = 4), 

+                    control = list(verbose = TRUE))


parameter specification:

support : 0.1

maxsize :   5

maxlen  :   4


algorithmic control:

bfstype  : FALSE

verbose  :  TRUE

summary  : FALSE

tidLists : FALSE


preprocessing ... 1 partition(s), 0 MB [0.023s]

mining transactions ... 0 MB [0.013s]

reading sequences ... [0.011s]


total elapsed time: 0.047s

Warning message:

In makebin(data, file) : 'eventID' is a factor

> summary(seq_rule)

set of 21 sequences with


most frequent items:

   "AC"    "CA"    "RF"    "TV"    "AC, (Other) 

     11       8       8       8       4       4 


most frequent elements:

 {"AC"}  {"CA"}  {"RF"}  {"TV"}  {"AC,} (Other) 

     11       8       8       8       2       4 


element (sequence) size distribution:

sizes

1 2 3 4 

7 9 4 1 


sequence length distribution:

lengths

1 2 3 4 

6 9 5 1 


summary of quality measures:

    support      

 Min.   :0.5000  

 1st Qu.:0.5000  

 Median :0.5000  

 Mean   :0.5238  

 3rd Qu.:0.5000  

 Max.   :1.0000  


includes transaction ID lists: FALSE 


mining info:

 data ntransactions nsequences support

   tr             6          2     0.1

>

> # sequence rule as a DataFrame format

> as(seq_rule, "data.frame")

                        sequence support

1                       <{"AC"}>     1.0

2                       <{"AC,}>     0.5

3                       <{"CA"}>     0.5

4                       <{"RF"}>     0.5

5                       <{"TV"}>     0.5

6                        <{TV"}>     0.5

7                 <{"AC"},{TV"}>     0.5

8                   <{"AC,,TV"}>     0.5

9            <{"AC"},{"AC,,TV"}>     0.5

10               <{"AC"},{"RF"}>     0.5

11               <{"CA"},{"RF"}>     0.5

12               <{"TV"},{"RF"}>     0.5

13        <{"TV"},{"CA"},{"RF"}>     0.5

14        <{"TV"},{"AC"},{"RF"}>     0.5

15        <{"AC"},{"CA"},{"RF"}>     0.5

16 <{"TV"},{"AC"},{"CA"},{"RF"}>     0.5

17               <{"AC"},{"CA"}>     0.5

18               <{"TV"},{"CA"}>     0.5

19        <{"TV"},{"AC"},{"CA"}>     0.5

20               <{"AC"},{"AC,}>     0.5

21               <{"TV"},{"AC"}>     0.5

 



많은 도움 되었기를 바랍니다.

이번 포스팅이 도움이 되었다면 아래의 '공감~'를 꾹 눌러주세요. :-)

 

728x90
반응형
Posted by Rfriend
,