59doit

[ R ] 연관분석 연습문제 본문

Q.

[ R ] 연관분석 연습문제

yul_S2 2022. 12. 5. 09:01
반응형

 1. tranExam.csv 파일을 대상으로 중복된 트랜잭션 없이 1-2컬럼만 single형식으로 트랜잭션 객체를 생성하시오.

# 1단계: 트랜잭션 객체 생성 및 확인
# 2단계: 각 items별로 빈도수 확인
# 3단계: 파라미터(supp = 0.3, conf = 0.1)를 이용하여 규칙(rule)생성
# 4단계: 연관규칙 결과 보기

 

 

tranExam.csv
0.00MB

 

 


1) 트랜잭션 객체 생성 및 확인

setwd("C:/")
tranExam <- read.transactions("tranExam.csv",format="single",sep=",",cols=c(1,2),rm.duplicates=T)
tranExam
inspect(tranExam)
# transactions in sparse format with
# 5 transactions (rows) and
# 4 items (columns)

구분자 콤마로 나눠주어야한다 : sep=","

 

 

2) 각 items별로 빈도수 확인

summary(tranExam)
# transactions as itemMatrix in sparse format with
# 5 rows (elements/itemsets/transactions) and
# 4 columns (items) and a density of 0.6 

# most frequent items:
#   1       2       3       4 (Other) 
# 4       3       3       2       0 

# element (itemset/transaction) length distribution:
#   sizes
# 2 4 
# 4 1 

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

# includes extended item information - examples:
#   labels
# 1      1
# 2      2
# 3      3

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

 

 

3) 파라미터(supp = 0.3, conf = 0.1)를 이용하여 규칙(rule)생성

rule <- apriori(tranExam, parameter = list(supp = 0.3, conf = 0.1))
# Apriori

# Parameter specification:
#   confidence minval smax arem  aval originalSupport maxtime support minlen maxlen target  ext
# 0.1    0.1    1 none FALSE            TRUE       5     0.3      1     10  rules TRUE

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

# Absolute minimum support count: 1 

# set item appearances ...[0 item(s)] done [0.00s].
# set transactions ...[4 item(s), 5 transaction(s)] done [0.00s].
# sorting and recoding items ... [4 item(s)] done [0.00s].
# creating transaction tree ... done [0.00s].
# checking subsets of size 1 2 3 done [0.00s].
# writing ... [12 rule(s)] done [0.00s].
# creating S4 object  ... done [0.00s].

rule
# set of 12 rules 

 

4) 연관규칙 결과 보기

inspect(rule)
# lhs    rhs support confidence coverage lift      count
# [1]  {}  => {4} 0.4     0.4000000  1.0      1.0000000 2    
# [2]  {}  => {2} 0.6     0.6000000  1.0      1.0000000 3    
# [3]  {}  => {3} 0.6     0.6000000  1.0      1.0000000 3    
# [4]  {}  => {1} 0.8     0.8000000  1.0      1.0000000 4    
# [5]  {4} => {1} 0.4     1.0000000  0.4      1.2500000 2    
# [6]  {1} => {4} 0.4     0.5000000  0.8      1.2500000 2    
# [7]  {2} => {3} 0.4     0.6666667  0.6      1.1111111 2    
# [8]  {3} => {2} 0.4     0.6666667  0.6      1.1111111 2    
# [9]  {2} => {1} 0.4     0.6666667  0.6      0.8333333 2    
# [10] {1} => {2} 0.4     0.5000000  0.8      0.8333333 2    
# [11] {3} => {1} 0.4     0.6666667  0.6      0.8333333 2    
# [12] {1} => {3} 0.4     0.5000000  0.8      0.8333333 2  

 

 

 

 

 2. Adult데이터 셋을 대상으로 다음 조건에 맞게 연관분석을 수행하시오.
# 1) 최소 support = 0.5, 최소 confidence = 0.9를 지정하여 연관규칙을 생성한다.
# 2) 수행한 결과를 lift기준으로 정렬하여 상위 10개 규칙을 기록한다.
# 3) 연관분석 결과를 LHS와 RHS의 빈도수로 시각화한다.
# 4) 연관분석 결과를 연관어의 네트워크 형태로 시각화한다.
# 5) 연관어 중심 단어를 해설한다.

 

data(Adult)
Adult
head(Adult)
str(Adult)

adult <- as(Adult,"data.frame")
str(adult)
head(adult)

 

1) 최소 support = 0.5, 최소 confidence = 0.9를 지정하여 연관규칙을 생성한다.

ar <- apriori(Adult, parameter = list(supp = 0.5, conf = 0.9))
# Apriori

# Parameter specification:
#   confidence minval smax arem  aval originalSupport maxtime support minlen maxlen target  ext
# 0.9    0.1    1 none FALSE            TRUE       5     0.5      1     10  rules TRUE

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

# Absolute minimum support count: 24421 

# set item appearances ...[0 item(s)] done [0.00s].
# set transactions ...[115 item(s), 48842 transaction(s)] done [0.04s].
# sorting and recoding items ... [9 item(s)] done [0.00s].
# creating transaction tree ... done [0.02s].
# checking subsets of size 1 2 3 4 done [0.00s].
# writing ... [52 rule(s)] done [0.00s].
# creating S4 object  ... done [0.00s].

ar
# set of 52 rules 

inspect(ar)

 

2) 수행한 결과를 lift기준으로 정렬하여 상위 10개 규칙을 기록한다.

inspect(head(sort(ar,by="lift"),10))

 

 

3) 연관분석 결과를 LHS와 RHS의 빈도수로 시각화한다.

library(arulesViz)
plot(ar, method = "grouped")



 

4) 연관분석 결과를 연관어의 네트워크 형태로 시각화한다.

plot(ar, method="graph", control=list(type="items")) 

 

5) 연관어 중심 단어를 해설한다

capital-loss,capital-gain,native-country 중심으로 연관어가 형성되어있다.

 

 

 

반응형
Comments