R:所有带有arules的篮子的建议

时间:2016-04-08 17:18:20

标签: r arules

我的数据是这样的交易:

ID=c("A123","A123","A123","A123","B456","B456","B456","C789","C789")
item=c("bread", "butter", "milk", "eggs", "meat","milk", "peas", "peas", "meat")

df=data.frame(cbind(ID, item))

ID   item
1 A123  bread
2 A123 butter
3 A123   milk
4 A123   eggs
5 B456   meat
6 B456   milk
7 B456   peas

我想提出建议,因此我会转换数据并构建规则

library(arules)
trans = as(split(df$item, df$ID), "transactions")
rules = apriori(trans, parameter = list(support = 0.006, confidence = 0.25,
                                      minlen = 2))

有关购物篮3的客户建议如下:

basket = trans[3] 
rulesMatchLHS = is.subset(rules@lhs,basket)
suitableRules =  rulesMatchLHS & !(is.subset(rules@rhs,basket))
order.rules = sort(rules[suitableRules], by = "lift")
LIST(order.rules@rhs)[[1]]

[1]“牛奶”

但我怎样才能为所有篮子提出建议?我试过这个,但得到一个错误:

reco=function(x){
    rulesMatchLHS = is.subset(rules@lhs,x)
    suitableRules =  rulesMatchLHS & !(is.subset(rules@rhs,x))
    order.rules = sort(rules[suitableRules], by = "lift")
    LIST(order.rules@rhs)[[1]]
}

results = lapply(trans, function(x) reco(x))
  

as.vector(数据)出错:     没有将此S4类强制转换为向量的方法

我如何运行所有篮子的建议?

1 个答案:

答案 0 :(得分:1)

试试这个:

from productapp.views import ProductList
from userapp.views import login_user

urlpatterns = [
    url(r'^admin/', include(admin.site.urls)),
    url(r'^', include(router.urls)),
    url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')),
    url(r'^login$', login_user, name="login"),
    url(r'^products$', ProductList.as_view(), name="product-list"),
]

显然,没有将事务类转换为向量的函数。