【问题标题】:How to convert a csv data in to a market basket format?如何将 csv 数据转换为购物篮格式?
【发布时间】:2015-07-17 05:38:18
【问题描述】:

我有以下格式的数据:

TxnId    Items
    1        a
    1        b
    1        c
    2        r 
    2        t

其中“TxnId”和“Items”是列。我在 R 中导入了文件并运行了以下命令:

df_fact <- data.frame(lapply(MyData,as.factor))
df_trans <- as(df_fact, 'transactions')

当我运行 apriori 命令时,它会引发错误。

rules = apriori(df_trans, parameter=list(supp=0.95, conf=0.95, target=”rules”))
inspect(rules)
#NULL
inspect(rules[1:5])
Error in inspect(rules[1:5]) : 
  error in evaluating the argument 'x' in selecting a method for function 'inspect': Error in slot(x, s)[i] : subscript out of bounds

还请告诉我 R 接受数据的格式。

【问题讨论】:

  • inspect(rules) 的结果是NULL。你的对象是空的。

标签: r apriori market-basket-analysis


【解决方案1】:

它应该像这样工作:

MyData <- read.table(header = TRUE, text = "
TxnId    Items
1        a
1        b
1        c
2        a 
2        b
")
library(arules)
df_trans <- as(split(MyData$Items, MyData$TxnId), "transactions")
rules <- apriori(df_trans, parameter=list(supp=0.95, conf=0.95, target="rules"))
inspect(rules)
#   lhs    rhs support confidence lift
# 1 {}  => {a}       1          1    1
# 2 {}  => {b}       1          1    1
# 3 {a} => {b}       1          1    1
# 4 {b} => {a}       1          1    1

您可能需要放松supp 和/或conf 才能在数据集中查找规则。

【讨论】:

    【解决方案2】:

    我在强制转换方面遇到了很多麻烦(例如,'as(dataname, "transactions"..)。

    我认为这是因为我有重复记录(即,当数据为“单一”格式时,同一商品在同一交易中购买了多次)。

    这最终对我有用:

    Transactions<- read.transactions("Data with tx ids, item names, in
                          single format.csv", 
                          rm.duplicates= TRUE, sep=",",
                          format = "single", cols = c(7,9));
    

    (第 7 列中的 tx id,第 9 列中的项目名称)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-15
      • 1970-01-01
      相关资源
      最近更新 更多