R语言中关联规则的实现与可视化

标签:
r数据挖掘 |
分类: R语言 |
>
require(arules)
Warning message:
package ‘arules’ was built under R version 3.1.1
>
require(arulesViz)
Loading required package: arulesViz
Loading required package: grid
Attaching package: ‘arulesViz’
The following object is masked from ‘package:base’:
Warning message:
package ‘arulesViz’ was built under R version 3.1.1
>
data(Groceries)
>
summary(Groceries)
transactions as itemMatrix in sparse format with
most frequent items:
element (itemset/transaction) length distribution:
sizes
2159 1643 1299 1005
includes extended item information - examples:
1 frankfurter sausage meet and sausage
2
3
>
inspect(head(Groceries))
1 {citrus
fruit,
2 {tropical
fruit,
3 {whole
milk}
4 {pip
fruit,
5 {other
vegetables,
6 {whole
milk,
> rule0 <- apriori(Groceries)
parameter specification:
algorithmic control:
apriori - find association rules with the apriori algorithm
version 4.21
(2004.05.09)
set item appearances ...[0 item(s)] done [0.00s].
set transactions ...[169 item(s), 9835 transaction(s)] done [0.00s].
sorting and recoding items ... [8 item(s)] done [0.00s].
creating transaction tree ... done [0.00s].
checking subsets of size 1 2 done [0.00s].
writing ... [0 rule(s)] done [0.00s].
creating S4 object
> #以上使用apriori算法的默认参数进行运算
> #用inspect查看具体生成的规则
> inspect(rule0)
NULL
> #手动设置apriori算法的相关参数,包括support与confidence
> rule1 <- apriori(Groceries,parameter=list(support=0.01,confidence=0.5))
parameter specification:
algorithmic control:
apriori - find association rules with the apriori algorithm
version 4.21
(2004.05.09)
set item appearances ...[0 item(s)] done [0.00s].
set transactions ...[169 item(s), 9835 transaction(s)] done [0.00s].
sorting and recoding items ... [88 item(s)] done [0.00s].
creating transaction tree ... done [0.03s].
checking subsets of size 1 2 3 4 done [0.00s].
writing ... [15 rule(s)] done [0.00s].
creating S4 object
> inspect(rule1)
1
2
3
4
5
6
7
8
9
10 {tropical
fruit,
11 {root
vegetables,
12 {root
vegetables,
13 {root
vegetables,
14 {root
vegetables,
15 {other
vegetables,
> #关于关联规则三个主要参数的说明:支持度一般设为5%-10%,置信度一般在50%以上,提升度如果等于1,则两者相互独立,应大于1
> #查找与某个商品相关的另一个商品的方法:
> rule2 <- apriori(Groceries,parameter=list(maxlen=2,support=0.01,confidence=0.5),appearance = list(rhs="mustard",default="lhs"))
parameter specification:
algorithmic control:
apriori - find association rules with the apriori algorithm
version 4.21
(2004.05.09)
set item appearances ...[1 item(s)] done [0.00s].
set transactions ...[169 item(s), 9835 transaction(s)] done [0.00s].
sorting and recoding items ... [88 item(s)] done [0.00s].
creating transaction tree ... done [0.01s].
checking subsets of size 1 2 done [0.00s].
writing ... [0 rule(s)] done [0.00s].
creating S4 object
> rule2 <- apriori(Groceries,parameter=list(maxlen=2,support=0.01,confidence=0.5),appearance = list(rhs="whole milk",default="lhs"))
parameter specification:
algorithmic control:
apriori - find association rules with the apriori algorithm
version 4.21
(2004.05.09)
set item appearances ...[1 item(s)] done [0.00s].
set transactions ...[169 item(s), 9835 transaction(s)] done [0.00s].
sorting and recoding items ... [88 item(s)] done [0.00s].
creating transaction tree ... done [0.00s].
checking subsets of size 1 2 done [0.00s].
writing ... [0 rule(s)] done [0.00s].
creating S4 object
> inspect(rule2)
NULL
> #使用图形显示关联规则
> plot(rule1)
http://s9/mw690/0027nJCegy6KTKzSTMc08&690
> #通过交互图形显示关联规则
> plot(rule1,interactive=TRUE)
http://s12/mw690/0027nJCegy6KTKBNaTV7b&690
Interactive mode.
Select a region with two clicks!
Error in convertUnit(x, unitTo, "x", "location", "x", "location", valueOnly = valueOnly) :