【问题标题】:Why does as(..., "transactions") for arules in R seem to lose transactions?为什么 R 中的 arules 的 as(..., "transactions") 似乎会丢失交易?
【发布时间】:2017-09-12 22:06:45
【问题描述】:

我有一个 CSV 格式的大型数据集:

see attached image

  • 有 50,000 行,每行是一个事务。
  • 每笔交易最多有 5 件商品,最少有 1 件商品。
  • 有 5000 种不同的可能项目值。
  • 交易中没有重复的项目。

将 CSV 加载到 RStudio 并应用 unclass() 后,我应用 as(...,"transactions")

结果是这样的:

# transactions in sparse format with
#  5 transactions (rows) and
#  1455 items (columns)

现在只有 5 笔交易,而不是 50,000 笔交易。

所有交易都去哪儿了?矩阵是否以某种方式转置(因为结果中的行数等于我的 CSV 的列数)?

这可能是数据预处理问题,但根据this post我的输入数据应该有正确的格式。

[我是第一次在这里发帖,对 R/RStudio 还很陌生。]

【问题讨论】:

    标签: r arules


    【解决方案1】:

    查看手册页? transactions 中的coercion 方法。您将看到您需要一个二元关联矩阵、交易列表或仅包含分类变量的 data.frame。您的数据不是其中之一,as(..., "transactions") 将失败。

    我认为read.transactions 可以读取您的数据。

    library(arules)
    
    # create and write some data
    data <- paste(
       "item1,item2,,,", 
       "item1,,,,", 
       "item2,item3,,,", 
       sep="\n")
    write(data, file = "demo_basket")
    
    # read the data
    tr <- read.transactions("demo_basket", format = "basket", sep=",")
    inspect(tr)
    
        items        
    [1] {item1,item2}
    [2] {item1}      
    [3] {item2,item3}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-28
      • 2016-01-28
      • 1970-01-01
      • 2011-07-04
      相关资源
      最近更新 更多