R语言APRIORI实例

  • A+

Apriori算法是一种挖掘关联规则的频繁项集算法,其核心思想是通过候选集生成和情节的向下封闭检测两个阶段来挖掘频繁项集。

R语言APRIORI实例

下面做探索性事例:

  1. ################################################################################
  2. #########################APRIORI ALGORITHM#######################################
  3. ################################################################################
  4. library(DBI)
  5. library(ROracle)
  6. library(arules)
  7. drv=dbDriver('Oracle')
  8. ##and gxsj>=trunc(sysdate,'yyyy')
  9. #conn=dbConnect(drv,'zjzhzx','zjzhzx_2016','10.118.1.1:1521/orcl')
  10. conn=dbConnect(drv,'acci_tz','acci_tz','192.111.1.1:1521/jgyw')
  11. #############################基础数据获取#########################################
  12. dataQuery<-dbSendQuery(conn,"with temp as (
  13.                        select t.tq,t.njd,t.dlaqsx,t.lmzk,t.lbqk,t.lmjg,d.nl,d.jl,t.sglx  from  dw_acd_file t, dw_acd_filehuman d
  14.                        where t.sgbh=d.sgbh   and lh=10104
  15.                        and t.tq is not null 
  16.                        and   t.njd is not null 
  17.                        and t.dlaqsx is not null
  18.                        and t.lmzk is not null 
  19.                        and t.lbqk is not null
  20.                        and t.lmjg is not null
  21.                        and d.nl is not null
  22.                        and d.jl is not null 
  23.                        and t.sglx is not null
  24. )
  25. select t1.dmsm1 tq,t2.dmsm1 njd,t3.dmsm1 dlaqsx
  26. , t4.dmsm1 lmzk,t5.dmsm1 lbqk,t6.dmsm1 lmjg
  27. ,t7.dmsm1 nl,t8.dmsm1 jl,t9.dmsm1 sglx
  28. from temp t 
  29. join dw_dim_tq t1 on t.tq=t1.dmz
  30. join dw_dim_njd t2 on t.njd=t2.dmz
  31. join dw_dim_dlaqsx t3 on t.dlaqsx=t3.dmz
  32. join dw_dim_lmzk t4 on t.lmzk=t4.dmz
  33. join dw_dim_lbqk t5 on t.lbqk=t5.dmz
  34. join dw_dim_lmjg t6 on t.lmjg=t6.dmz
  35. join dw_dim_nl t7 on t.nl=t7.dmz
  36. join dw_dim_jl t8 on t.jl=t8.dmz   
  37. join dw_dim_sglx t9 on t.sglx=t9.dmz")
  38. dataBasic<-fetch(dataQuery)
  39. ################################需要把元素啊转换为factor##############################
  40. dataBasic$TQ <- as.factor(dataBasic$TQ)
  41. dataBasic$NJD <- as.factor(dataBasic$NJD)
  42. dataBasic$DLAQSX <- as.factor(dataBasic$DLAQSX)
  43. dataBasic$LMZK <- as.factor(dataBasic$LMZK)
  44. dataBasic$LBQK <- as.factor(dataBasic$LBQK)
  45. dataBasic$LMJG <- as.factor(dataBasic$LMJG)
  46. dataBasic$NL <- as.factor(dataBasic$NL)
  47. dataBasic$JL <- as.factor(dataBasic$JL)
  48. dataBasic$SGLX <- as.factor(dataBasic$SGLX)
  49. ##############################修改列名,便于展现######################################
  50. colnames(dataBasic)<-c('天气','能见度','道路安全属性','路面状况','路表情况','路面结构','年龄','驾龄','事故类型')
  51. #dataBasic <- as.data.frame(as.factor(dataBasic$TQ))
  52. ######################求频繁项集#####################################################
  53. frequentSets <- eclat(dataBasic,parameter = list(support=0.05,maxlen=6))
  54. #察看求得的频繁项集
  55. inspect(frequentSets[1:10])
  56. #根据支持度对求得的频繁项集排序并察看(等价于inspect(sort(frequentsets)[1:10])
  57. inspect(sort(frequentSets,by="support")[1:10])
  58. ####################求关联规则########################################################
  59. rules <- apriori(dataBasic,parameter = list(support=0.01,confidence=0.01))
  60. ##################察看求得的关联规则之摘要###############################################
  61. summary(rules)
  62. ########对规则进行过滤#################################################################
  63. ###############lhs是关联规则的左侧,rhs是关联规则的右侧。####################################
  64. ######support,confidence,lift三列分别是支持度,信任度和提升度####################################
  65. ##############求所需要的关联规则子集#####################################################
  66. x <- subset(rules,subset=rhs%in%"事故类型=死亡事故")
  67. ################根据支持度对求得的关联规则子集排序并察看####################################
  68. val<-inspect(sort(x,by="confidence")[1:20])
  69. inspect(x)
  70. ################数据提取并入库#########################################################
  71. vx<-as.matrix(val$lhs)
  72. vy<-as.matrix(val$rhs)
  73. supportx<-as.matrix(val$support)
  74. confidencey<-as.matrix(val$confidence)
  75. resultVal<-cbind(vx,vy,supportx,confidencey)
  76. colnames(resultVal)<-c('ITEMX','ITEMY','SUPPORTX','CONFIDENCEY')
  77. RESULTV<-as.data.frame(resultVal)
  78. dbRemoveTable(conn,'ACD_FACT_APRIOR')
  79. dbWriteTable(conn,'ACD_FACT_APRIOR',RESULTV,row.names = F, append = TRUE)

结果:

R语言APRIORI实例

R语言APRIORI实例

结果不是很理想(中间缺少相关性分析以及需要剔除正常的数据量较大属性)(个人见解)。

华青莲日常点滴,方便自己,成长他人!!!

MySQL必知必会
Excel数据可视化分析方法大全
基于大数据的用户特征分析
2016年度中国软件开发者白皮书下载(PDF)

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: