我正在尝试使用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个观测值时,对于星际大战数据集的绝对支持数为78)。
因此,您需要减少(或调整)对的支持或置信度,以便您至少拥有1条规则或更多规则。另外,
target = "rules"
也无济于事,因为您可以看到它正在生成0条规则。关于R-arules apriori长度错误(obj): Method length not implemented for class rules,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49308597/