加载中…
个人资料
felix123
felix123
  • 博客等级:
  • 博客积分:0
  • 博客访问:846
  • 关注人气:10
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

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

(2014-08-01 10:57:09)
标签:

r数据挖掘

分类: R语言

> require(arules)                         #载入arules(Association Rules)

Warning message:

package ‘arules’ was built under R version 3.1.1

> require(arulesViz)                      #载入arulesViz关联规则可视化工具包

Loading required package: arulesViz

Loading required package: grid

 

Attaching package: ‘arulesViz’

 

The following object is masked from ‘package:base’:

 

    abbreviate

 

Warning message:

package ‘arulesViz’ was built under R version 3.1.1

> data(Groceries)                          #载入Groceries数据集,包含9000多购买数据

> summary(Groceries)                       #查看该数据集的汇总信息

transactions as itemMatrix in sparse format with

 9835 rows (elements/itemsets/transactions) and

 169 columns (items) and a density of 0.02609146

 

most frequent items:

      whole milk other vegetables       rolls/buns             soda           yogurt          (Other)

            2513             1903             1809             1715             1372            34055

 

element (itemset/transaction) length distribution:

sizes

   1                    10   11   12   13   14   15   16   17   18   19   20   21   22   23   24   26   27

2159 1643 1299 1005  855  645  545  438  350  246  182  117   78   77   55   46   29   14   14     11            1

  28   29   32

       1

 

   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.

  1.000   2.000   3.000   4.409   6.000  32.000

 

includes extended item information - examples:

       labels  level2           level1

1 frankfurter sausage meet and sausage

    sausage sausage meet and sausage

liver loaf sausage meet and sausage

> inspect(head(Groceries))               #了解前五组数据的详细情况

  items                    

1 {citrus fruit,           

   semi-finished bread,     

   margarine,              

   ready soups}            

2 {tropical fruit,         

   yogurt,                 

   coffee}                 

3 {whole milk}             

4 {pip fruit,              

   yogurt,                 

   cream cheese ,           

   meat spreads}           

5 {other vegetables,       

   whole milk,             

   condensed milk,         

   long life bakery product}

6 {whole milk,             

   butter,                 

   yogurt,                 

   rice,                    

   abrasive cleaner}       

> rule0 <- apriori(Groceries)

 

parameter specification:

 confidence minval smax arem  aval originalSupport support minlen maxlen target   ext

        0.8    0.1    1 none FALSE            TRUE     0.1         10  rules FALSE

 

algorithmic control:

 filter tree heap memopt load sort verbose

    0.1 TRUE TRUE  FALSE TRUE      TRUE

 

apriori - find association rules with the apriori algorithm

version 4.21 (2004.05.09)        (c) 1996-2004   Christian Borgelt

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  ... done [0.00s].

> #以上使用apriori算法的默认参数进行运算

> #inspect查看具体生成的规则

> inspect(rule0)

NULL

> #手动设置apriori算法的相关参数,包括supportconfidence

> rule1 <- apriori(Groceries,parameter=list(support=0.01,confidence=0.5))

 

parameter specification:

 confidence minval smax arem  aval originalSupport support minlen maxlen target   ext

        0.5    0.1    1 none FALSE            TRUE    0.01         10  rules FALSE

 

algorithmic control:

 filter tree heap memopt load sort verbose

    0.1 TRUE TRUE  FALSE TRUE      TRUE

 

apriori - find association rules with the apriori algorithm

version 4.21 (2004.05.09)        (c) 1996-2004   Christian Borgelt

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  ... done [0.00s].

> inspect(rule1)

   lhs                     rhs                   support confidence     lift

{curd,                                                                  

    yogurt}             => {whole milk}       0.01006609  0.5823529 2.279125

{other vegetables,                                                      

    butter}             => {whole milk}       0.01148958  0.5736041 2.244885

{other vegetables,                                                       

    domestic eggs}      => {whole milk}       0.01230300  0.5525114 2.162336

{yogurt,                                                                

    whipped/sour cream} => {whole milk}       0.01087951  0.5245098 2.052747

{other vegetables,                                                      

    whipped/sour cream} => {whole milk}       0.01464159  0.5070423 1.984385

{pip fruit,                                                             

    other vegetables}   => {whole milk}       0.01352313  0.5175097 2.025351

{citrus fruit,                                                          

    root vegetables}    => {other vegetables} 0.01037112  0.5862069 3.029608

{tropical fruit,                                                         

    root vegetables}    => {other vegetables} 0.01230300  0.5845411 3.020999

{tropical fruit,                                                        

    root vegetables}    => {whole milk}       0.01199797  0.5700483 2.230969

10 {tropical fruit,                                                        

    yogurt}             => {whole milk}       0.01514997  0.5173611 2.024770

11 {root vegetables,                                                       

    yogurt}             => {other vegetables} 0.01291307  0.5000000 2.584078

12 {root vegetables,                                                       

    yogurt}             => {whole milk}       0.01453991  0.5629921 2.203354

13 {root vegetables,                                                        

    rolls/buns}         => {other vegetables} 0.01220132  0.5020921 2.594890

14 {root vegetables,                                                       

    rolls/buns}         => {whole milk}       0.01270971  0.5230126 2.046888

15 {other vegetables,                                                      

    yogurt}             => {whole milk}       0.02226741  0.5128806 2.007235

> #关于关联规则三个主要参数的说明:支持度一般设为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:

 confidence minval smax arem  aval originalSupport support minlen maxlen target   ext

        0.5    0.1    1 none FALSE            TRUE    0.01          rules FALSE

 

algorithmic control:

 filter tree heap memopt load sort verbose

    0.1 TRUE TRUE  FALSE TRUE      TRUE

 

apriori - find association rules with the apriori algorithm

version 4.21 (2004.05.09)        (c) 1996-2004   Christian Borgelt

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  ... done [0.00s].

> rule2 <- apriori(Groceries,parameter=list(maxlen=2,support=0.01,confidence=0.5),appearance = list(rhs="whole milk",default="lhs"))

 

parameter specification:

 confidence minval smax arem  aval originalSupport support minlen maxlen target   ext

        0.5    0.1    1 none FALSE            TRUE    0.01          rules FALSE

 

algorithmic control:

 filter tree heap memopt load sort verbose

    0.1 TRUE TRUE  FALSE TRUE      TRUE

 

apriori - find association rules with the apriori algorithm

version 4.21 (2004.05.09)        (c) 1996-2004   Christian Borgelt

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  ... done [0.00s].

> 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) :

  'x' argument must be a unit object

 

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有