用于大量数据中挖掘出有价值的数据项之间的相关关系 常用于用户购物篮分析,使用它来发现顾客的购买习惯 两个不相交的非空集合X、Y,如果有X->Y,就说X->Y是一条关联规则。关联规则的强度用支持度(support)和自信度(confidence)来描述,关联规则是否可用,使用提升度(Lift)来描述。 挖掘定义 给定一个数据集,找出其中所有支持度support>=min_support,自信度confidence>=min_confifence的关联规则。
支持度(Support) support(X->Y)=集合X与集合Y中的项在一条记录中同时出现的次数/数据的个数 例如: support({啤酒->尿布})=啤酒和尿布同时出现的次数/记录数
自信度(Cconfidence) confidence(X->Y)=集合X与集合Y中的项在一条记录中同时出现的次数/集合X出现的个数 例如: confidence({啤酒}->{尿布})=啤酒和尿布同时出现的次数/啤酒出现的次数
提升度(Lift) 度量规则是否可用的指标,描述的是相对于不用规则,使用规则可以提高多少,有用的规则的提升度大于1 计算公式=lift({A→B})=confidence({A→B})/support(B)
实现关联规则的API install.packages(“arules”) apriori(x,parameter=list(support=0.5,confidence=0.5))
以经典的啤酒尿布为例,看看在R中如何实现关联规则算法
data <- read.transactions(
"data.csv",
format = "single",
cols = c(1,2),
sep=',',
skip=1
)
inspect(data)
data2 <- read.transactions(
"data2.txt",
format="basket",
sep=",",
skip = 1
)
inspect(data2)
rules <- apriori(data)
inspect(rules)
rules <- apriori(
data,
parameter=list(
support=0.5,
confidence=0.5
)
)
inspect(rules)
lhs rhs support confidence lift
[1] {} => {啤酒} 0.6 0.60 1.0000
[2] {} => {牛奶} 0.8 0.80 1.0000
[3] {} => {面包} 0.8 0.80 1.0000
[4] {} => {尿布} 0.8 0.80 1.0000
[5] {啤酒} => {尿布} 0.6 1.00 1.2500
[6] {尿布} => {啤酒} 0.6 0.75 1.2500
[7] {牛奶} => {面包} 0.6 0.75 0.9375
[8] {面包} => {牛奶} 0.6 0.75 0.9375
[9] {牛奶} => {尿布} 0.6 0.75 0.9375
[10] {尿布} => {牛奶} 0.6 0.75 0.9375
[11] {面包} => {尿布} 0.6 0.75 0.9375
[12] {尿布} => {面包} 0.6 0.75 0.9375
由关联规则算法的结果告诉我们,最有价值的商品组合为啤酒-尿布!