1

我有以下格式的数据:

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 接受数据的格式。

4

2 回答 2

2

它应该像这样工作:

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在数据集中找到规则。

于 2015-07-17T07:13:41.537 回答
0

我在强制方面遇到了很多麻烦(例如,'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 列中的项目名称)

于 2015-10-19T20:31:39.247 回答