Created
July 26, 2017 08:59
-
-
Save enujo/0a5cff3ca7fad8038527415648efa353 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
getwd() | |
## 데이터 불러오기 | |
# csv파일을 일반적으로 불러올때 : read.csv("파일명") | |
csvGrocery<-read.csv("./BigData/grocery.csv") | |
csvGrocery | |
# 데이터 분석시 데이터를 확인 함수 : fix() | |
fix(csvGrocery) | |
## 연관규칙 분석 시작 | |
# 사용할 라이브러리 로드 | |
# 만약에 라이브러리가 없다면 install.packages("라이브러리") 실행 | |
# install.packages("arules") | |
library(arules) | |
# 연관규칙 분석을 위해서 데이터를 불러옴 : read.transactions() | |
asGrocery <- read.transactions(file="./BigData/grocery.csv",format ="basket",sep="," ) | |
asGrocery | |
# 트랜잭션 형식으로 불러온 데이터를 출력 : insperct() | |
inspect(asGrocery) | |
summary(asGrocery) | |
# apriori 알고리즘 수행 | |
groceryRule.all <- apriori(asGrocery) | |
groceryRule.all# 규칙의 개수만 출력 | |
# 발견된 규칙(트랜잭션 데이터)을 출력 | |
inspect(groceryRule.all) | |
# 발견된 규칙이 너무 많거나 저장하고자 할때는 파일에 출력결과를 입력 | |
# 결과를 입력할 파일 생성 및 open | |
sink("R10_Association.txt") | |
inspect(groceryRule.all) | |
sink() | |
## apriori알고리즘을 수행하는데 있어서 아이템길이가 2, 최소지지도 0.3(support), 최소 상관도 0.9(confidence)를 만족하는 규칙을 발견 | |
# verbose : 진행과정에 대한 정보를 출력할 것인지 여부 체크(T:출력, F:출력안함) | |
groceryRule.s3c9 <- apriori(asGrocery, control = list(verbose=T), parameter = list(minlen=2, supp=0.3,conf=0.9))# | |
groceryRule.s3c9 | |
sink("R10_Association2.txt") | |
inspect(groceryRule.s3c9) | |
sink() | |
inspect(groceryRule.s3c9) | |
# apriori 알고리즘에서 실행결과 중 특정값만 추출해서 사용(support, confidence, lift) | |
quality(groceryRule.s3c9) | |
# 측정결과값을 기준으로 정렬하여 출력 : sort() | |
# sort(데이터, by="정렬기준 측정값") | |
groceyrRule.s3c9.sort <-sort(groceryRule.s3c9, by="support") | |
sink("R10_Association3.txt") | |
inspect(groceryRule.s3c9) | |
sink() | |
# 측정 결과에서 쇼핑백을 제외하고 분석 | |
groceryRule.nonShopping <- apriori(asGrocery, control=list(verbose=F),parameter=list(minlen = 2,maxlen=3, supp=0.3,conf=0.7), appearance =list(none="shopping bags")) | |
groceryRule.nonShopping | |
groceryRule.nonShopping.sort <- sort(groceryRule.nonShopping, by="confidence")#A를 삿을때 B를 살 확률이 높은애들 위주로 | |
sink("R10_Association4.txt") | |
inspect(groceryRule.nonShopping) | |
sink() | |
sink("R10_Association5.txt") | |
inspect(groceryRule.nonShopping.sort) | |
sink() | |
# 시각화(Visualizing) | |
# assciation 시각화를 위해서는 arulesViz 패키지 필요 | |
install.packages("arulesViz") | |
library(arulesViz) | |
# 지지도가 0.1 이상인 item 출력 | |
itemFrequencyPlot(asGroccery,support=0.1) # 규칙이 아닌 제일 처음 데이터를 | |
# 상위 5개 항목 | |
itemFrequencyPlot(asGroccery, topN=5) | |
# 원의 크기가 지지도/ 색상은 향상도 (높을수록 진한색)/ 연관규칙의 수 +1 item (item이 하나 더있다.) | |
plot(groceryRule.s3c9, method ="grouped") | |
plot(groceryRule.nonShopping, method="graph") | |
plot(groceryRule.nonShopping, method="paracoord") # 데이터가 작을때 용이 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment