【问题标题】:R arulesSequences Find which patterns are supported by a sequenceR arulesSequences 查找序列支持哪些模式
【发布时间】:2016-11-02 11:29:49
【问题描述】:

我在使用 R 中的 arulesSequences 库时遇到问题

我有一个包含时间信息的事务数据集(在这里,让我们使用默认的zaki 数据集)。我使用 SPADE(cspade 函数)来查找数据集中的频繁子序列。

library(arulesSequences)
data(zaki)
frequent_sequences <- cspade(zaki, parameter=list(support=0.5))

现在,我想要为每个序列(即每个客户)找到它支持的频繁子序列。我尝试了%in%subset 的各种组合,但没有取得多大成功。

例如对于第二个客户,初始交易inspect(zaki[zaki@itemsetInfo$sequenceID==2])是:

items     sequenceID eventID SIZE
5 {A,B,F} 2          15      3   
6 {E}     2          20      1 

整个数据集中inspect(frequent_sequences)的频繁序列为:

items support 
1 <{A}>    1.00 
2 <{B}>    1.00 
3 <{D}>    0.50 
4 <{F}>    1.00 
5 <{A, F}>    0.75 
6 <{B, F}>    1.00 
7 <{D}, {F}>    0.50 
8 <{D}, {B, F}>    0.50 
9 <{A, B, F}>    0.75 
10 <{A, B}>    0.75 
11 <{D}, {B}>    0.50 
12 <{B}, {A}>    0.50 
13 <{D}, {A}>    0.50 
14 <{F}, {A}>    0.50 
15 <{D}, {F}, {A}>    0.50 
16 <{B, F}, {A}>    0.50 
17 <{D}, {B, F}, {A}>    0.50 
18 <{D}, {B}, {A}>    0.50 

我想看到的是,客户 2 支持频繁序列 1、2、4、5、6、9 和 10,但不支持其他的。

我也可以接受相反的信息:哪些是支持给定频繁子序列的基本序列? R 不知何故知道这个信息,因为它使用它来计算频繁序列的支持度。

在我看来这应该很容易(而且很可能是!)但我似乎无法弄清楚......

有什么想法吗?

【问题讨论】:

    标签: r arules


    【解决方案1】:

    经过一番冷静的挖掘后,我找到了一种方法,而且确实很简单……因为 support 函数可以完成这项工作!

    ids <- unique(zaki@itemsetInfo$sequenceID)
    encoding <- data.frame()
    
    # Prepare the data.frame: as many columns as there are frequent sequences
    for (seq_id in 1:length(frequent_sequences)){
        encoding[,labels(frequent_sequences[seq_id])] <- logical(0)
    }
    
    # Fill the rows
    for (id in ids){
        transaction_subset <- zaki[zaki@itemsetInfo$sequenceID==id]
        encoding[id, ] <- as.logical(
            support(frequent_sequences, transaction_subset, type="absolute")
            )
    }
    

    可能有更美观的方式来达到结果,但这会产生预期的结果:

    > encoding
      <{A}> <{B}> <{D}> <{F}> <{A,F}> <{B,F}> <{D},{F}> <{D},{B,F}> <{A,B,F}>
    1  TRUE  TRUE  TRUE  TRUE    TRUE    TRUE      TRUE        TRUE      TRUE
    2  TRUE  TRUE FALSE  TRUE    TRUE    TRUE     FALSE       FALSE      TRUE
    3  TRUE  TRUE FALSE  TRUE    TRUE    TRUE     FALSE       FALSE      TRUE
    4  TRUE  TRUE  TRUE  TRUE   FALSE    TRUE      TRUE        TRUE     FALSE
      <{A,B}> <{D},{B}> <{B},{A}> <{D},{A}> <{F},{A}> <{D},{F},{A}> <{B,F},{A}>
    1    TRUE      TRUE      TRUE      TRUE      TRUE          TRUE        TRUE
    2    TRUE     FALSE     FALSE     FALSE     FALSE         FALSE       FALSE
    3    TRUE     FALSE     FALSE     FALSE     FALSE         FALSE       FALSE
    4   FALSE      TRUE      TRUE      TRUE      TRUE          TRUE        TRUE
      <{D},{B,F},{A}> <{D},{B},{A}>
    1            TRUE          TRUE
    2           FALSE         FALSE
    3           FALSE         FALSE
    4            TRUE          TRUE
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-03
      • 1970-01-01
      相关资源
      最近更新 更多