注,有任何建议或疑问,请加 QQ: 1595218767 ,共同探讨学习
如R/python代码编程作图等方面需要帮忙,欢迎来店咨询 之恒科技, 挥动热情的小爪爪期待你哦
关联规则主要用来发现Pattern,最经典的应用是购物篮分析,当然其他类似案例也可以应用关联规则进行模式发现,如电影推荐/约会网站/药物间的相互副作用/点击流分析等
关联规则分析(非购物篮分析)数据要求:
1.预测变量和目标变量必须都是类别变量或者定序变量
2.如果是数值变量但值分布数量有限(可以当分类变量理解)或者将数值变量分组后也可使用本方案
规则生成基本流程
一共有2步:
- 找出频繁项集. n个item,可以产生2^(n- 1)个项集(itemset). 所以,需要指定最小支持度来过滤掉非频繁项集
- 找出上步中频繁项集的规则. n个item,总共可以产生3^n - 2^(n+1) + 1条规则. 所以需要指定最小置信度来过滤掉弱规则
案例应用 -- 泰坦尼克号幸存因素分析
数据获取
元数据请移步 qq 群 174225475
load('http://www.rdatamining.com/data/titanic.raw.rdata')
> str(titanic.raw)
'data.frame': 2201 obs. of 4 variables:
$ Class : Factor w/ 4 levels "1st","2nd","3rd",..: 3 3 3 3 3 3 3 3 3 3 ...
$ Sex : Factor w/ 2 levels "Female","Male": 2 2 2 2 2 2 2 2 2 2 ...
$ Age : Factor w/ 2 levels "Adult","Child": 2 2 2 2 2 2 2 2 2 2 ...
$ Survived: Factor w/ 2 levels "No","Yes": 1 1 1 1 1 1 1 1 1 1 ...
> head(titanic.raw)
Class Sex Age Survived
1 3rd Male Child No
2 3rd Male Child No
3 3rd Male Child No
4 3rd Male Child No
5 3rd Male Child No
6 3rd Male Child No
关联分析
library(arules)
# find association rules with default settings
rules <- apriori(titanic.raw)
inspect(rules[1:5])
lhs rhs support confidence lift count
[1] {} => {Age=Adult} 0.9504771 0.9504771 1.0000000 2092
[2] {Class=2nd} => {Age=Adult} 0.1185825 0.9157895 0.9635051 261
[3] {Class=1st} => {Age=Adult} 0.1449341 0.9815385 1.0326798 319
[4] {Sex=Female} => {Age=Adult} 0.1930940 0.9042553 0.9513700 425
[5] {Class=3rd} => {Age=Adult} 0.2848705 0.8881020 0.9343750 627
规则提取
提取有用规则
只保留结果中包含生存变量的关联规则
# rules with rhs containing “Survived” only
rules <- apriori(titanic.raw,
parameter = list(minlen=2, supp=0.005, conf=0.8),
appearance = list(rhs=c('Survived=No', 'Survived=Yes'),
default='lhs'),
control = list(verbose=F))
rules.sorted <- sort(rules, by='lift')
inspect(rules.sorted)
lhs rhs support confidence lift count
[1] {Class=2nd,Age=Child} => {Survived=Yes} 0.010904134 1.0000000 3.095640 24
[2] {Class=2nd,Sex=Female,Age=Child} => {Survived=Yes} 0.005906406 1.0000000 3.095640 13
[3] {Class=1st,Sex=Female} => {Survived=Yes} 0.064061790 0.9724138 3.010243 141
[4] {Class=1st,Sex=Female,Age=Adult} => {Survived=Yes} 0.063607451 0.9722222 3.009650 140
[5] {Class=2nd,Sex=Female} => {Survived=Yes} 0.042253521 0.8773585 2.715986 93
[6] {Class=Crew,Sex=Female} => {Survived=Yes} 0.009086779 0.8695652 2.691861 20
[7] {Class=Crew,Sex=Female,Age=Adult} => {Survived=Yes} 0.009086779 0.8695652 2.691861 20
[8] {Class=2nd,Sex=Female,Age=Adult} => {Survived=Yes} 0.036347115 0.8602151 2.662916 80
[9] {Class=2nd,Sex=Male,Age=Adult} => {Survived=No} 0.069968196 0.9166667 1.354083 154
[10] {Class=2nd,Sex=Male} => {Survived=No} 0.069968196 0.8603352 1.270871 154
[11] {Class=3rd,Sex=Male,Age=Adult} => {Survived=No} 0.175829169 0.8376623 1.237379 387
[12] {Class=3rd,Sex=Male} => {Survived=No} 0.191731031 0.8274510 1.222295 422
总共生成了12条跟人员生存相关的规则
去除冗余的规则
subset.matrix <- is.subset(rules.sorted, rules.sorted)
subset.matrix[lower.tri(subset.matrix, diag=T)] <- FALSE
redundant <- colSums(subset.matrix) >= 1
which(redundant)
# remove redundant rules
rules.pruned <- rules.sorted[!redundant]
inspect(rules.pruned)
lhs rhs support confidence lift count
[1] {Class=2nd,Age=Child} => {Survived=Yes} 0.010904134 1.0000000 3.095640 24
[2] {Class=1st,Sex=Female} => {Survived=Yes} 0.064061790 0.9724138 3.010243 141
[3] {Class=2nd,Sex=Female} => {Survived=Yes} 0.042253521 0.8773585 2.715986 93
[4] {Class=Crew,Sex=Female} => {Survived=Yes} 0.009086779 0.8695652 2.691861 20
[5] {Class=2nd,Sex=Male,Age=Adult} => {Survived=No} 0.069968196 0.9166667 1.354083 154
[6] {Class=2nd,Sex=Male} => {Survived=No} 0.069968196 0.8603352 1.270871 154
[7] {Class=3rd,Sex=Male,Age=Adult} => {Survived=No} 0.175829169 0.8376623 1.237379 387
[8] {Class=3rd,Sex=Male} => {Survived=No} 0.191731031 0.8274510 1.222295 422
上述语句实现了 superset 对 subset的合并,如下图所示
对于结果的解释,一定要慎重,千万不要盲目下结论。从下面的四条规则看,好像确实像电影中描述的那样:妇女和儿童优先
1 {Class=2nd, Age=Child} => {Survived=Yes} 0.010904134 1.0000000 3.095640
2 {Class=1st, Sex=Female} => {Survived=Yes} 0.064061790 0.9724138 3.010243
3 {Class=2nd, Sex=Female} => {Survived=Yes} 0.042253521 0.8773585 2.715986
4 {Class=Crew, Sex=Female} => {Survived=Yes} 0.009086779 0.8695652 2.691861
若减小最小支持率和置信度的阈值,则能看到更多的真相
rules <- apriori(titanic.raw, parameter = list(minlen=3, supp=0.002, conf=0.2),
appearance = list(rhs=c('Survived=Yes'),
lhs=c('Class=1st', 'Class=2nd', 'Class=3rd',
'Age=Child', 'Age=Adult'), default='none'),
control = list(verbose=F))
rules.sorted <- sort(rules, by='confidence')
inspect(rules.sorted)
lhs rhs support confidence lift count
[1] {Class=2nd,Age=Child} => {Survived=Yes} 0.010904134 1.0000000 3.0956399 24
[2] {Class=1st,Age=Child} => {Survived=Yes} 0.002726034 1.0000000 3.0956399 6
[3] {Class=1st,Age=Adult} => {Survived=Yes} 0.089504771 0.6175549 1.9117275 197
[4] {Class=2nd,Age=Adult} => {Survived=Yes} 0.042707860 0.3601533 1.1149048 94
[5] {Class=3rd,Age=Child} => {Survived=Yes} 0.012267151 0.3417722 1.0580035 27
[6] {Class=3rd,Age=Adult} => {Survived=Yes} 0.068605179 0.2408293 0.7455209 151
从规则3和规则5以及之前的规则2和3可以看出泰坦尼克号获得优先权的主要是头等舱、二等舱的妇孺
据统计,头等舱男乘客的生还率比三等舱中儿童的生还率还稍高一点.美国新泽西州州立大学教授,著名社会学家戴维·波普诺研究后毫不客气地修改了曾使英国人颇感'安慰'的'社会规范'(妇女和儿童优先):在泰坦尼克号上实践的社会规范这样表述可能更准确一些:'头等舱和二等舱的妇女和儿童优先'
可视化
# visualize rules
library(arulesViz)
plot(rules)
plot(rules, method=”graph”, control=list(type=”items”))
plot(rules, method=”paracoord”, control=list(reorder=TRUE))
从图中可以清晰地看出:
1.头等舱和二等舱的孩子 生存几率非常大
2.头等舱的 adult 幸存率最大
References:
- 关联规则:R 与 SAS 的比较 -- 统计之都