gpt4 book ai didi

R - arules apriori Error in length(obj) : Method length not implemented for class rules

转载 作者:行者123 更新时间:2023-12-01 01:45:59 24 4
gpt4 key购买 nike

我正在尝试使用 apriori 设置关联规则 - 我使用的是不同的数据集,但 starwars 数据集包含类似的问题。使用 arules 我试图列出规则并应用 arulesViz 图。根据我的理解,所有字符串都必须作为因子运行,列为事务,然后 apriori 应该可以正常运行,但是在运行以下代码后我得到以下输出,并且规则未添加到环境中:

install.packages("arules")
install.packages("arulesViz")
library(arulesViz)
library(arules)
data <- starwars[,c(4:6,8:10)]
data <- data.frame(sapply(data,as.factor))
data <- as(data, "transactions")
rules <- apriori(data, parameter = list(supp = 0.15, conf = 0.80))


inspect(rules)
inspect(sort(rules))

subrules <- head(sort(rules, by="lift"), 10)
plot(subrules, method="graph")

以下是运行 apriori 的输出
rules <- apriori(data, parameter = list(supp = 0.15, conf = 0.80))
Apriori

Parameter specification:
confidence minval smax arem aval originalSupport maxtime support minlen maxlen target ext
0.8 0.1 1 none FALSE TRUE 5 0.15 1 10 rules FALSE

Algorithmic control:
filter tree heap memopt load sort verbose
0.1 TRUE TRUE FALSE TRUE 2 TRUE

Absolute minimum support count: 78

set item appearances ...[0 item(s)] done [0.00s].
set transactions ...[131 item(s), 522 transaction(s)] done [0.00s].
sorting and recoding items ... [0 item(s)] done [0.00s].
creating transaction tree ... done [0.00s].
checking subsets of size 1 done [0.00s].
writing ... [0 rule(s)] done [0.00s].
creating S4 object ... done [0.02s].
Error in length(obj) : Method length not implemented for class rules

我还运行了以下参数更改
target = "rules"

并尝试仅使用空参数运行

任何帮助是极大的赞赏!

最佳答案

如果我用 starwars 运行你的代码数据,我得到以下结果 -

> data <- starwars[,c(4:6,8:10)]
> data <- data.frame(sapply(data,as.factor))
> data <- as(data, "transactions")
> rules <- apriori(data, parameter = list(supp = 0.15, conf = 0.80))
Apriori

Parameter specification:
confidence minval smax arem aval originalSupport maxtime support minlen maxlen target ext
0.8 0.1 1 none FALSE TRUE 5 0.15 1 10 rules FALSE

Algorithmic control:
filter tree heap memopt load sort verbose
0.1 TRUE TRUE FALSE TRUE 2 TRUE

Absolute minimum support count: 13

set item appearances ...[0 item(s)] done [0.00s].
set transactions ...[147 item(s), 87 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 3 done [0.00s].
writing ... [3 rule(s)] done [0.00s].
creating S4 object ... done [0.00s].

您可以清楚地看到,生成了 3 个规则。这意味着如果我运行检查 - 我会看到以下内容:
  lhs                  rhs             support   confidence lift    
[1] {skin_color=fair} => {species=Human} 0.1839080 0.9411765 2.339496
[2] {skin_color=fair} => {gender=male} 0.1609195 0.8235294 1.155598
[3] {eye_color=brown} => {species=Human} 0.1954023 0.8095238 2.012245

但是,如果我通过增加支持计数来运行相同的操作,我将生成 0 个规则(因此在您的情况下 - 当您只有 87 个观察值时,starwars 数据集的绝对支持计数为 78)。

所以 您需要降低(或调整)支持度或置信度 所以你至少有 1 条规则或更多。另外, target = "rules"无能为力,因为您可以看到它正在生成 0 条规则。

关于R - arules apriori Error in length(obj) : Method length not implemented for class rules,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49308597/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com