【发布时间】:2017-08-20 00:09:22
【问题描述】:
今天一个问题是关于一个用数据挖掘先验算法填充的 for 循环。 我正在对先验算法中的结果进行分析,但正如您已经知道的那样,算法的两个主要参数(置信度和支持度)是在不知道结果的情况下设置的。这意味着有时您必须尝试不同的参数组合才能达到令人满意的结果。我决定尝试在 R 中设置一个 for 循环,我打算达到这种类型的结果:
vector s c
x1 y1 z1
x2 y1 z2
x3 y1 z3
x4 y2 z1
x5 y2 z2
x6 y2 z3
...
xn yn zn
x 的向量为创建的规则数,向量 s 为支持参数(0
显然,我是自己开始寻找解决方案的。我认为这两个参数应该是一对序列,所以不知道用两个序列做一个 for 循环,并使用我的一个老问题:
for loop with decimals and store results in a vector
我尝试制作一个简单的 for 循环,其中只有一个“移动”参数,第二个固定。 首先,我创建了一些假数据,因为非常小而有用。
# here the data
id <- c("1","1","1","2","2","2","3","3","3")
obj <- c("a", "b", "j", "a", "g","c", "a","k","c")
df <- data.frame(id,obj)
然后,进行转换,使数据可被 arules 包的 apriori 函数消化:
# here the rewritten data
library(arules)
transactions <- as(split(df$obj, df$id), "transactions")
inspect(transactions)
最后,只有一个移动参数的函数,支持:
test <- function(x, y1, y2, y3, z){
# the sequence for the support
s <- seq(y1, y2, by = y3)
# empty vector
my_vector <- vector("numeric")
# for loop with moving support (in the seq) and fixed confidence
for(i in seq_along(s)){my_vector <- nrow( data.frame(
# this is a small trick to have the row of the rules, do not know if it is perfect
labels(lhs(apriori(x,parameter=list(supp = s[i], conf = z))))))}
my_vector
# put the result in a data frame
data <- data.frame (vector = as.numeric(my_vector),s = as.numeric(s))
return(data)
}
这是第一个有结果的应用程序:
# the function applied
test(transactions, 0.01, 0.1, 0.01, 0.1)
# the result: the apriori function generates also its output, avoided here
vector s
1 31 0.01
2 31 0.02
3 31 0.03
4 31 0.04
5 31 0.05
6 31 0.06
7 31 0.07
8 31 0.08
9 31 0.09
10 31 0.10
如果你提交这个
apriori(transactions,parameter=list(supp = 0.01, conf = 0.1))
apriori(transactions,parameter=list(supp = 0.1, conf = 0.1))
结果是一致的。
现在是困难的部分(对我来说)。 我还希望置信度参数有所不同。我研究了一下这个:
Including multiple conditions in for-loop
但是我有一个很大的限制,我无法想象如何应用它。我可以改变第一个参数,并为每个值尝试“移动”第二个参数。在这种情况下,如果支持度在 0.1 和 0.01 之间变化 0.01,置信度也因此变化,结果应该是 100 行的向量。
另外,我有一些技术问题,我没有能力做提到的这种事情。 我知道这个过程对机器来说可能有点苛刻,但我希望有一个能够使用的。
我想得到帮助, 并提前感谢您的宝贵时间。
【问题讨论】:
标签: r algorithm for-loop data-mining apriori