Skip to content

Instantly share code, notes, and snippets.

@enujo
Created July 27, 2017 04:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save enujo/6628ace174644f002391748259b24d01 to your computer and use it in GitHub Desktop.
Save enujo/6628ace174644f002391748259b24d01 to your computer and use it in GitHub Desktop.
getwd()
setwd("D:/BigData/source")
matrixData <- matrix(c(1:20), nrow=4)
matrixData
class(matrixData)
#matrixData 행렬을 데이터프레임으로 변환
matrixData <- as.data.frame(matrixData)
# member_city.txt파일을 읽어서 데이터프레임 mcDF생성
# read.table(파일명) 파일을 읽어서 그 값은 데이터 프레임형태로 저장
mcDF <- read.table("./BigData/member_city.txt")
class(mcDF)
# meber_city.txt파일에서 데이터에서 첫번째 행에 있는 member와 city는 열이름으로 지정하여 데이터프레임 mcColDF데이터 생성
mcColDF <- read.table("./BigData/member_city.txt", header=T)
mcColDF
# 타이타닉 데이터 셋 불러오기
titanic <- load("./BigData/titanic.raw.rdata")
titanic
titanic.raw
# 데이터 편집기를 이용해 titanic데이터 확인
fix(titanic.raw)
# 데이터의 기초 통계정보 확인
summary(titanic.raw)
# 연관규칙을 위한 라이브러리 로드
library(arules)
# titanic 데이터를 apriori 알고리즘으로 분석
ttRule.all <- apriori(titanic.raw)
# 연관규칙 개수 확인
ttRule.all
# 연관규칙 내용 보기
inspect(ttRule.all)
# 연관규칙으로 발견하는데 최소길이 2를 만족, 최소지지도 20%, 최소 상관도60%
ttRule.s2c6 <- apriori(titanic.raw, parameter = list(minlen=2, supp=0.2, conf=0.6)) # 최소 길이 minlen
sink("R11_Association.txt")
inspect(ttRule.s2c6)
sink()
# 연관규칙에서 살았는지(Survived="yes") 죽었는지(Survived="no")를 분석할 때
ttRul.suvival <- apriori(titanic.raw, parameter=list(minlen=2, supp=0.2, conf=0.6),appearance= list(rhs=c("Survived=Yes", "Survived=No"),default="lhs"))
sink("R11_Association2.txt")
inspect(ttRul.suvival)
sink()
# 결과값이 죽어잇는 사람만 있다 이럴땐 support를 내려줘야 한다
# 0.1 전체 10% 내에 살아있는 // 0.01과 0.005와 결과가 별차이가 없다 이럴땐 더 내릴 필요가 없다
# 어른의 65%가 죽었는데 여자가 살았다 15% 중복 세분화가 나타난다 -> 데이터가 문제가 있다. 2등석 1등석 살았다 3등석에서 죽었으니까 어른이 죽었다고 나올것이다
# 어른이 죽었다가 아니라 3등석의 어른이 죽었다가 ??? 조금더 세분화 시키기 위해 lhs 를 더 올리는 것
#ttRule.suvivaly <- apriori(titanic.raw, parameter=list(minlen=2, supp=0.02, conf=0.6),appearance= list(rhs=c("Survived=Yes", "Survived=No"),default="lhs"))
ttRule.suvivaly <- apriori(titanic.raw, parameter=list(minlen=3, supp=0.005, conf=0.8),appearance= list(rhs=c("Survived=Yes", "Survived=No"),default="lhs"))
# 똑같은 데이터를 종속되는 데이터를 분석할 필요가 없다 -> 하/상삼각 행렬 사용
sink("R11_Association3.txt")
inspect(ttRule.suvivaly)
sink()
# 불필요한 규칙을 제거
# is.subset(x,y) : y값이 x값에 포함되어있는지 여부를 Logical 표현(True/False)
ttRule.subset <- is.subset(ttRule.suvivaly, ttRule.suvivaly, sparse=FALSE) # 메모리상관 없이 출력을 해라
ttRule.subset
# 내 자신이 아닌데 종속되어 있다.
# 상삼각행렬
ttRule.subset[lower.tri(ttRule.subset, diag=T)] <- NA
ttRule.subset
#colSums(x) : x 열의 합을 구하는 함수
redundant <- colSums(ttRule.subset, na.rm=T) >= 1
redundant
# 데이터의 위치값(index)값을 확인
which(redundant)
# 불필요한(중복되는) 규칙을 제거
ttRule.pruned <- ttRule.suvivaly[!redundant]
inspect(ttRule.pruned)
# 그래프를 그리기 위한 라으브러리 로드
library(arulesViz)
# plot의 형식이 graph와 ㅔaracoord형식으로 출력
plot(ttRule.pruned, method="grouped")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment