【问题标题】:How to search through a list of n-dimen vector to find a vector in that list using R?如何使用 R 搜索 n 维向量列表以在该列表中查找向量?
【发布时间】:2017-05-07 08:07:02
【问题描述】:

我正在使用关联规则来预测事件顺序链中的下一个事件

我使用 Arules R 包 As 获得了规则

         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) 这样的向量作为输入,并通过每个规则对其进行搜索, 我不清楚 rules$lhs[1] 是什么类型的数据类型,typeof(rules$lhs[1]) 给出整数,

任何人都可以建议如何在 R 中做到这一点,这将是很大的帮助

提前致谢

【问题讨论】:

    标签: r vector arules


    【解决方案1】:

    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
    

    【讨论】:

      猜你喜欢
      • 2015-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-16
      • 2017-12-06
      • 2014-09-03
      • 1970-01-01
      相关资源
      最近更新 更多