r语言 - 使用来自数据的 Arules 和 ArulesViz 的关联规则



我有一个带有customer_id和product_name的R数据框。一个客户可以拥有多个产品。在客户列中,由于他们有多个产品,因此存在重复customer_ids。

我正在尝试进行基本的先验分析,并确定一起购买的产品的一些关联规则。我想使用 R 中的 Arules 和 ArulesViz 包来执行此操作。

当我尝试运行它时,我通常会得到 0 条规则或 lhs 产品 --> rhs customer_id。因此,我不相信我正确加载数据以查看单个客户的多个产品以得出关联。

任何帮助将不胜感激!

基本数据框示例

df <- data.frame( cust_id = as.factor(c('1aa2j', '1aa2j', '2b345',
'2b345', 'g78a8', 'y67r3')), product = as.factor(c("Bat", "Sock",
"Hat", "Shirt", "Ball", "Shorts")))
rules <- apriori(df) inspect(rules)
lhs rhs support confidence lift 1 {product=Bat} => {cust_id=1aa2j} 0.167 1 3
2 {product=Sock} => {cust_id=1aa2j} 0.167 1 3
3 {product=Hat} => {cust_id=2b345} 0.167 1 3
4 {product=Shirt} => {cust_id=2b345} 0.167 1 3
5 {cust_id=g78a8} => {product=Ball} 0.167 1 6
6 {product=Ball} => {cust_id=g78a8} 0.167 1 6
7 {cust_id=y67r3} => {product=Shorts} 0.167 1 6
8 {product=Shorts} => {cust_id=y67r3} 0.167 1 6

这是从transactions的例子中摘录的(略有修改(:

library(arules)
df <- data.frame( cust_id = as.factor(c('1aa2j', '1aa2j', '2b345',
'2b345', 'g78a8', 'y67r3')), product = as.factor(c("Bat", "Sock",
"Hat", "Shirt", "Ball", "Shorts")))
trans <- as(split(df[,"product"], df[,"cust_id"]), "transactions")
inspect(trans)
items       transactionID
[1] {Bat,Sock}  1aa2j        
[2] {Hat,Shirt} 2b345        
[3] {Ball}      g78a8        
[4] {Shorts}    y67r3 

现在,您可以在trans上使用apriori

最新更新