2017-05-07 59 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