2017-05-07 40 views
0

我使用关联规则在事件的顺序链如何使用R来搜索n维矢量列表以找到该列表中的矢量?

我使用Arules [R包规则

  lhs   rhs support confidence lift  itemset 
[1]  {11,3,4} => {10} 0.9523810  1.00 1.05  2 
[2]  {10,3,4} => {11} 0.9523810  1.00 1.00  2 
[3]  {10,11,4} => {3} 0.9523810  1.00 1.05  2 
[4]  {10,11,3} => {4} 0.9523810  1.00 1.05  2 
[5] {11,12,3,4} => {10} 0.8095238  1.00 1.05  3 
[6] {10,12,3,4} => {11} 0.8095238  1.00 1.00  3 
[7] {10,11,3,4} => {12} 0.8095238  0.85 1.05  3 
[8] {10,11,12,4} => {3} 0.8095238  1.00 1.05  3 
[9] {10,11,12,3} => {4} 0.8095238  1.00 1.05  3 
[10] {11,3,4,8} => {10} 0.8095238  1.00 1.05  4 
[11] {10,3,4,8} => {11} 0.8095238  1.00 1.00  4 
[12] {10,11,4,8} => {3} 0.8095238  1.00 1.05  4 
[13] {10,11,3,8} => {4} 0.8095238  1.00 1.05  4 
[14] {10,11,3,4} => {8} 0.8095238  0.85 1.05  4 
[15] {10,11,3,4} => {0} 0.8095238  0.85 1.05  5 
[16] {0,11,3,4} => {10} 0.8095238  1.00 1.05  5 
[17] {0,10,3,4} => {11} 0.8095238  1.00 1.00  5 
[18] {0,10,11,4} => {3} 0.8095238  1.00 1.05  5 
[19] {0,10,11,3} => {4} 0.8095238  1.00 1.05  5 
[20] {10,11,3,4} => {1} 0.8571429  0.90 1.05  6 

现在我要采取像一个向量来预测下一事件(1 ,5,8,9)作为输入并通过每个规则搜索它, 我不清楚什么类型的数据类型是规则$ lhs [1],typeof(规则$ lhs [1])给出整数,

这将是伟大的帮助任何人可以建议如何在R

在此先感谢

回答

0

arules使用S4类系统。要得到LHS使用lhs(rules)。班级是itemMatrix。您可以使用encode(查看? encode)将项目名称转换为稀疏itemMatrix编码。

这里是你想要做什么的例子:

> library(arules) 
> data(Groceries) 
> rules <- apriori(Groceries, parameter=list(support = 0.001)) 
> class(lhs(rules)) 
[1] "itemMatrix" 
attr(,"package") 
[1] "arules" 

# these are the items I want to match the lhs of the rules with 
> lhs_character <- c("rice", "sugar") 
> lhs_itemMatrix <- encode(lhs_character, itemLabels = itemLabels(Groceries)) 

# subset finds all rules with the items in the lhs 
> ss <- which(is.subset(lhs_itemMatrix, lhs(rules))) 
> inspect(rules[ss]) 
    lhs    rhs   support  confidence lift  count 
[1] {rice,sugar} => {whole milk} 0.001220132 1   3.913649 12