楼主: Lisrelchen
1257 1

Mining Associations with Apriori Algorithm using R [推广有奖]

  • 0关注
  • 62粉丝

VIP

已卖:4194份资源

院士

67%

还不是VIP/贵宾

-

TA的文库  其他...

Bayesian NewOccidental

Spatial Data Analysis

东西方数据挖掘

威望
0
论坛币
50288 个
通用积分
83.6306
学术水平
253 点
热心指数
300 点
信用等级
208 点
经验
41518 点
帖子
3256
精华
14
在线时间
766 小时
注册时间
2006-5-4
最后登录
2022-11-6

楼主
Lisrelchen 发表于 2015-3-13 22:26:11 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
  1. #clean the workspace and memory
  2. rm( list=ls() )
  3. gc()

  4. tbl <- read.csv("data/itemsets001.csv", header=FALSE)
  5. tbl <- as.matrix(tbl)
  6. colnames(tbl) <- NULL
  7. itemsets <- tbl
  8. items <- c(1,2,3,4,5)
  9. min_sup <- 0.22*nrow(itemsets)

  10. #######
  11. #        Algorithm: Apriori,
  12. #                generate the frequent itemsets from the input datasets with the specified support
  13. #        Input:
  14. #                data, MATRIX
  15. #                base_items, VECTOR
  16. #                MIN_SUP, FLOAT
  17. #                parameter,
  18. #######
  19. Apriori <- function(data,base_items,MIN_SUP,parameter=NULL){
  20.         f <- InitCandidateSet(data,base_items)
  21.         c <- list()
  22.         c[[1]] <- FindFrequentItemset(f,base_items,1,MIN_SUP)
  23.         k <- 2
  24.         len4data <- GetDatasetSize(data)
  25.         while( !IsEmpty(c,k-1) ){
  26.                 f[[k]] <- AprioriGen(c,k-1)
  27.                 if(length(f)==k){
  28.                         f[[k]] <- IncreaseSupportCount(f[[k]],data)
  29.                         c[[k]] <- FindFrequentItemset(f,base_items,k,MIN_SUP)               
  30.                 }else{break}
  31.                 k <- k+1
  32.         }
  33.         c
  34. }

  35. AprioriGen <- function(c,k){
  36.         ck <- c[[k]][,-ncol(c[[k]])]
  37.         f <- NULL
  38.         len <- nrow(ck)
  39.         for(idx in seq(nrow(ck))){
  40.                 jdx <- idx+1
  41.                 while(idx<jdx && jdx<=len){
  42.                         a <- ck[idx,]
  43.                         b <- ck[jdx,]
  44.                         if( k==1 || identical(a[1:(k-1)],b[1:(k-1)]) ){
  45.                                         ab <- ifelse(a+b,1,0)
  46.                                         if( !NeedPrune(ck,ab,k) ){
  47.                                                 f <- rbind(f,ab)
  48.                                         }else{
  49.                                                 #print("Pruned")
  50.                                         }
  51.                         }
  52.                         jdx <- jdx + 1
  53.                 }
  54.         }

  55.         if(length(f)){
  56.                  f <- cbind(f,rep(0,dim(f)[1]))
  57.                  rownames(f) <- NULL
  58.         }
  59.         return(f)
  60. }

  61. NeedPrune <- function(ck,ab,k){
  62.         ck <- rbind(ck,ab)
  63.         len <- dim(ck)[1]
  64.         for(idx in which(ab>0)){
  65.                 temp <- ab
  66.                 temp[idx] <- 0
  67.                 for(idx in seq(len)){
  68.                         if(identical(temp,ck[idx,]))break
  69.                 }
  70.                 if(idx==len)return(TRUE)
  71.         }
  72.         return(FALSE)
  73. }

  74. IncreaseSupportCount <- function(fk,data){
  75.         w4f <- ncol(fk)
  76.         len4f <- nrow(fk)
  77.         len4d <- nrow(data)
  78.         for(idx in seq(len4d)){
  79.                 for(jdx in seq(len4f)){
  80.                         if(identical(fk[jdx,-w4f],fk[jdx,-w4f]*data[idx,])){
  81.                                 fk[jdx,w4f] <- fk[jdx,w4f] + 1
  82.                         }
  83.                 }
  84.         }
  85.         return(fk)
  86. }

  87. IsEmpty <- function(ck,k){
  88.         return(ifelse(nrow(ck[[k]])>0,FALSE,TRUE))
  89. }

  90. GetDatasetSize <- function(data){
  91.         return( nrow(data) )
  92. }

  93. InitCandidateSet <- function(data,base_items){
  94.         list(cbind(diag(length(base_items)),apply(data,2,sum)))
  95. }

  96. FindFrequentItemset <- function(fk,base_items,k,MIN_SUP){
  97.         data <- fk[[k]]
  98.         return(data[data[,dim(data)[2]]>MIN_SUP,])
  99. }

  100. frequent_itemsets <- Apriori(itemsets,items,min_sup)
  101. print(frequent_itemsets)
复制代码
Reference
  • Learning Data Mining with R
  • Bater Makhabel
  • Published 2015-01-31
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:associations Association Algorithm Apriori Priori

沙发
Lisrelchen 发表于 2015-3-13 22:28:12
  1. > #clean the workspace and memory
  2. > rm( list=ls() )
  3. > gc()
  4.          used (Mb) gc trigger (Mb) max used (Mb)
  5. Ncells 266431 14.3     407500 21.8   350000 18.7
  6. Vcells 476429  3.7     905753  7.0   786396  6.0
  7. >
  8. > tbl <- read.csv("data/itemsets001.csv", header=FALSE)
  9. > tbl <- as.matrix(tbl)
  10. > colnames(tbl) <- NULL
  11. > itemsets <- tbl
  12. > items <- c(1,2,3,4,5)
  13. > min_sup <- 0.22*nrow(itemsets)
  14. >
  15. > #######
  16. > #        Algorithm: Apriori,
  17. > #                generate the frequent itemsets from the input datasets with the specified support
  18. > #        Input:
  19. > #                data, MATRIX
  20. > #                base_items, VECTOR
  21. > #                MIN_SUP, FLOAT
  22. > #                parameter,
  23. > #######
  24. > Apriori <- function(data,base_items,MIN_SUP,parameter=NULL){
  25. +         f <- InitCandidateSet(data,base_items)
  26. +         c <- list()
  27. +         c[[1]] <- FindFrequentItemset(f,base_items,1,MIN_SUP)
  28. +         k <- 2
  29. +         len4data <- GetDatasetSize(data)
  30. +         while( !IsEmpty(c,k-1) ){
  31. +                 f[[k]] <- AprioriGen(c,k-1)
  32. +                 if(length(f)==k){
  33. +                         f[[k]] <- IncreaseSupportCount(f[[k]],data)
  34. +                         c[[k]] <- FindFrequentItemset(f,base_items,k,MIN_SUP)               
  35. +                 }else{break}
  36. +                 k <- k+1
  37. +         }
  38. +         c
  39. + }
  40. >
  41. > AprioriGen <- function(c,k){
  42. +         ck <- c[[k]][,-ncol(c[[k]])]
  43. +         f <- NULL
  44. +         len <- nrow(ck)
  45. +         for(idx in seq(nrow(ck))){
  46. +                 jdx <- idx+1
  47. +                 while(idx<jdx && jdx<=len){
  48. +                         a <- ck[idx,]
  49. +                         b <- ck[jdx,]
  50. +                         if( k==1 || identical(a[1:(k-1)],b[1:(k-1)]) ){
  51. +                                         ab <- ifelse(a+b,1,0)
  52. +                                         if( !NeedPrune(ck,ab,k) ){
  53. +                                                 f <- rbind(f,ab)
  54. +                                         }else{
  55. +                                                 #print("Pruned")
  56. +                                         }
  57. +                         }
  58. +                         jdx <- jdx + 1
  59. +                 }
  60. +         }
  61. +
  62. +         if(length(f)){
  63. +                  f <- cbind(f,rep(0,dim(f)[1]))
  64. +                  rownames(f) <- NULL
  65. +         }
  66. +         return(f)
  67. + }
  68. >
  69. > NeedPrune <- function(ck,ab,k){
  70. +         ck <- rbind(ck,ab)
  71. +         len <- dim(ck)[1]
  72. +         for(idx in which(ab>0)){
  73. +                 temp <- ab
  74. +                 temp[idx] <- 0
  75. +                 for(idx in seq(len)){
  76. +                         if(identical(temp,ck[idx,]))break
  77. +                 }
  78. +                 if(idx==len)return(TRUE)
  79. +         }
  80. +         return(FALSE)
  81. + }
  82. >
  83. > IncreaseSupportCount <- function(fk,data){
  84. +         w4f <- ncol(fk)
  85. +         len4f <- nrow(fk)
  86. +         len4d <- nrow(data)
  87. +         for(idx in seq(len4d)){
  88. +                 for(jdx in seq(len4f)){
  89. +                         if(identical(fk[jdx,-w4f],fk[jdx,-w4f]*data[idx,])){
  90. +                                 fk[jdx,w4f] <- fk[jdx,w4f] + 1
  91. +                         }
  92. +                 }
  93. +         }
  94. +         return(fk)
  95. + }
  96. >
  97. > IsEmpty <- function(ck,k){
  98. +         return(ifelse(nrow(ck[[k]])>0,FALSE,TRUE))
  99. + }
  100. >
  101. > GetDatasetSize <- function(data){
  102. +         return( nrow(data) )
  103. + }
  104. >
  105. > InitCandidateSet <- function(data,base_items){
  106. +         list(cbind(diag(length(base_items)),apply(data,2,sum)))
  107. + }
  108. >
  109. > FindFrequentItemset <- function(fk,base_items,k,MIN_SUP){
  110. +         data <- fk[[k]]
  111. +         return(data[data[,dim(data)[2]]>MIN_SUP,])
  112. + }
  113. >
  114. > frequent_itemsets <- Apriori(itemsets,items,min_sup)
  115. > print(frequent_itemsets)
  116. [[1]]
  117.      [,1] [,2] [,3] [,4] [,5] [,6]
  118. [1,]    1    0    0    0    0    6
  119. [2,]    0    1    0    0    0    7
  120. [3,]    0    0    1    0    0    6
  121. [4,]    0    0    0    1    0    2
  122. [5,]    0    0    0    0    1    2

  123. [[2]]
  124.      [,1] [,2] [,3] [,4] [,5] [,6]
  125. [1,]    1    1    0    0    0    4
  126. [2,]    1    0    1    0    0    4
  127. [3,]    1    0    0    0    1    2
  128. [4,]    0    1    1    0    0    4
  129. [5,]    0    1    0    1    0    2
  130. [6,]    0    1    0    0    1    2

  131. [[3]]
  132.      [,1] [,2] [,3] [,4] [,5] [,6]
  133. [1,]    1    1    1    0    0    2
  134. [2,]    1    1    0    0    1    2

  135. >
复制代码

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注jltj
拉您入交流群
GMT+8, 2026-1-4 01:44