【问题标题】:Association Rules using Arules and ArulesViz from Data使用来自数据的 Arules 和 ArulesViz 的关联规则
【发布时间】:2017-12-05 23:46:45
【问题描述】:

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

我正在尝试进行基本的先验分析并确定一起购买的产品的一些关联规则。我想使用 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

【问题讨论】:

    标签: r associations apriori market-basket-analysis


    【解决方案1】:

    这取自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

    【讨论】:

      猜你喜欢
      • 2018-11-21
      • 1970-01-01
      • 2015-04-19
      • 2015-05-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-22
      • 1970-01-01
      相关资源
      最近更新 更多