【问题标题】:Transform csv into transactions for arules [duplicate]将csv转换为arules的事务[重复]
【发布时间】:2016-08-25 08:53:42
【问题描述】:

我有一个来自 csv 数据库的子集,它有几个不同的列,我想将数据转换为事务。我已经读过this post

library(arules)
library(arulesViz)

trans = read.transactions("data.csv", format = "single", sep = ",",
                     cols = c("EMAIL", "BRAND"))

但是无法使用建议的解决方案转换我的数据:

CATEGORY   BRAND   SKU   EMAIL         SEGMENT   SALES
shorts     gap     1564  one@mail.x    1         1
tops       gap     8974  one@mail.x    1         2
shoes      nike    3245  two@mail.x    4         3
jeans      levis   8956  two@mail.x    4         1

现在我想使用 arules 来了解客户通常会一起购买哪些品牌。为了使用 arules,我需要转换我的数据,使其如下所示:

gap, gap
nike, levis

谁能帮我弄清楚如何相应地转换我的数据?

【问题讨论】:

    标签: r csv arules


    【解决方案1】:

    如果我们将列 EMAIL 视为一种事务 ID,我们可以通过以下方式将您的 data.frame 转换为类 transactions

    library(arules)
    trans <- as(split(df[,"BRAND"], df[,"EMAIL"]), "transactions")
    
    # To explore the rules we could do
    rules <- apriori(trans)
    inspect(rules)
    #  lhs        rhs     support confidence lift
    #1 {levis} => {nike}  0.5     1          2   
    #2 {nike}  => {levis} 0.5     1          2   
    

    【讨论】:

    • 谢谢,这以正确的格式转换了数据。但是我收到了以下消息Warning message: In asMethod(object) : removing duplicated items in transactions,当我尝试inspect 规则[1:5] 我收到以下错误Error in slot(x, s)[i] : subscript out of bounds 你知道是什么原因造成的吗?
    • 你不能像 gap => gap 那样让相同的项目相互预测,因此会删除重复项。至于第二个,您需要更改 apriori() 调用中的值 support = confidence = 参数以获取更多规则。
    • 谢谢你,这更有意义。我修复了confidence = support = 参数。我究竟如何从事务数据集中删除重复项?我可以在使用df &lt;- unique(df[ , c(2,4) ] ) 将数据转换为事务之前简单地删除重复项吗?
    • 您不需要删除它们,apriori() 会自动为您删除它们。
    • 我再次运行了一切,它就像一个魅力。非常感谢您的帮助!
    猜你喜欢
    • 2017-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多