查看数据menu_orders.txt文件存在多少条关联规则,并按支持度降序排序输出
#导入arules包
install.packages("arules")
library ( arules ) setwd('D:\\data')
Gary<- read.transactions("menu_orders.txt", format = "basket", sep=",")
summary(Gary) #查看部分规则
inspect(Gary) #支持度0.2,置信度0.5
rules0=apriori(Gary,parameter=list(support=0.2,confidence=0.5)) #按支持度降序排序输出
rules0.sorted_sup = sort(rules0, by="support") #读取rules0中存在多少条数据
rules0
inspect(rules0)
Gary.R
apriori函数
apriori(data, parameter = NULL, appearance = NULL, control = NULL)
data:数据
parameter 设置参数,默认情况下parameter=list(supp=0.1,conf=0.8,maxlen=10,minlen=1,target=”rules”)
supp: 支持度(support)
conf: 置信度(confidence)
maxlen,minlen: 每个项集所含项数的最大最小值(lhs+rhs的长度)
target: “rules”或“frequent itemsets”(输出关联规则/频繁项集)
apperence: 对先决条件X(lhs),关联结果Y(rhs)中具体包含哪些项进行限制,如:设置lhs=beer,将仅输出lhs含有beer这一项的关联规则。默认情况下,所有项都将无限制出现。
control: 控制函数性能,如可以设定对项集进行升序sort=1或降序sort=-1排序,是否向使用者报告进程(verbose=F/T)
排序:
通过支持度控制:rules.sorted_sup = sort(rules, by=”support”)
通过置信度控制:rules.sorted_con = sort(rules, by=”confidence”)
通过提升度控制:rules.sorted_lift = sort(rules, by=”lift”)
read.transactions(file, format =c("basket", "single"), sep = NULL, cols = NULL, rm.duplicates =FALSE, encoding = "unknown") file:文件名,对应click_detail中的“click_detail.txt” format:文件格式,可以有两种,分别为“basket”,“single”,click_detail.txt中用的是basket。basket: basket就是篮子,一个顾客买的东西都放到同一个篮子,所有顾客的transactions就是一个个篮子的组合 结果。如下形式,每条交易都是独立的。single: single的意思,顾名思义,就是单独的交易,简单说,交易记录为:顾客1买了产品1, 顾客1买了产品2,顾客2买了产品3……(产品1,产品2,产品3中可以是单个产品,也可以是多个产品) sep:文件中数据是怎么被分隔的,默认为空格,click_detail里面用逗号分隔 cols:对basket, col=1,表示第一列是数据的transaction ids(交易号),如果col=NULL,则表示数据里面没有交易号这一列;对single,col=c(1,2)表示第一列是transaction ids,第二列是item ids rm.duplicates:是否移除重复项,默认为FALSE encoding:写到这里研究了encoding是什么意思,发现前面txt可以不是”ANSI”类型,如果TXT是“UTF-8”,写encoding=”UTF-8”。
read.transactions函数
实现过程
读取数据并展示
> setwd('D:\\data')
> Gary<- read.transactions("menu_orders.txt", format = "basket", sep=",")
> summary(Gary)
transactions as itemMatrix in sparse format with
10 rows (elements/itemsets/transactions) #10行(元素/项集/事务)
5 columns (items) and a density of 0.54 #5列(项)和0.54的密度 most frequent items:
b a c e d (Other)
8 7 7 3 2 0 element (itemset/transaction) length distribution: #元素(项集/事务)长度分布
sizes
2 3 4
5 3 2 Min. 1st Qu. Median Mean 3rd Qu. Max.
2.0 2.0 2.5 2.7 3.0 4.0 includes extended item information - examples:
labels
1 a
2 b
3 c
查看部分规则
> inspect(Gary)
items
[1] {a,c,e}
[2] {b,d}
[3] {b,c}
[4] {a,b,c,d}
[5] {a,b}
[6] {b,c}
[7] {a,b}
[8] {a,b,c,e}
[9] {a,b,c}
[10] {a,c,e}
支持度0.2,置信度0.5
> rules0=apriori(Gary,parameter=list(support=0.2,confidence=0.5))
Apriori Parameter specification:
confidence minval smax arem aval originalSupport maxtime support minlen maxlen target ext
0.5 0.1 1 none FALSE TRUE 5 0.2 1 10 rules FALSE Algorithmic control: #算法控制:
filter tree heap memopt load sort verbose #过滤树堆
0.1 TRUE TRUE FALSE TRUE 2 TRUE Absolute minimum support count: 2 #绝对最小支持计数:2 set item appearances ...[0 item(s)] done [0.00s].
set transactions ...[5 item(s), 10 transaction(s)] done [0.00s].
sorting and recoding items ... [5 item(s)] done [0.00s].
creating transaction tree ... done [0.00s].
checking subsets of size 1 2 3 done [0.00s].
writing ... [18 rule(s)] done [0.00s].
creating S4 object ... done [0.00s].
按支持度降序排序输出
> rules0.sorted_sup = sort(rules0, by="support")
> rules0
set of 18 rules
#根据支持度对求得的关联规则子集进行查看
> inspect(rules0)
lhs rhs support confidence lift count
[1] {} => {c} 0.7 0.7000000 1.0000000 7
[2] {} => {b} 0.8 0.8000000 1.0000000 8
[3] {} => {a} 0.7 0.7000000 1.0000000 7
[4] {d} => {b} 0.2 1.0000000 1.2500000 2
[5] {e} => {c} 0.3 1.0000000 1.4285714 3
[6] {e} => {a} 0.3 1.0000000 1.4285714 3
[7] {c} => {b} 0.5 0.7142857 0.8928571 5
[8] {b} => {c} 0.5 0.6250000 0.8928571 5
[9] {c} => {a} 0.5 0.7142857 1.0204082 5
[10] {a} => {c} 0.5 0.7142857 1.0204082 5
[11] {b} => {a} 0.5 0.6250000 0.8928571 5
[12] {a} => {b} 0.5 0.7142857 0.8928571 5
[13] {c,e} => {a} 0.3 1.0000000 1.4285714 3
[14] {a,e} => {c} 0.3 1.0000000 1.4285714 3
[15] {a,c} => {e} 0.3 0.6000000 2.0000000 3
[16] {b,c} => {a} 0.3 0.6000000 0.8571429 3
[17] {a,c} => {b} 0.3 0.6000000 0.7500000 3
[18] {a,b} => {c} 0.3 0.6000000 0.8571429 3