将csv转换为交易规则 [英] Transform csv into transactions for arules
问题描述
我从csv中的数据库中获得一个子集,该子集具有多个不同的列,我想将数据转换为事务.我已经读过此帖子
I have a subset from a database in csv which has several different columns and I would like to convert the data into transactions. I've already read this post
library(arules)
library(arulesViz)
trans = read.transactions("data.csv", format = "single", sep = ",",
cols = c("EMAIL", "BRAND"))
但是无法使用建议的解决方案转换我的数据:
However wasn't able to convert my data with the proposed solution:
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
现在,我想使用规则来了解客户通常一起购买的品牌.为了使用规则,我需要转换数据,使其看起来如下:
Now I want to use arules to understand what brands customers generally buy together. In order to use arules I need to convert my data so it looks as follows:
gap, gap
nike, levis
有人可以帮助我弄清楚如何相应地转换我的数据吗?
Can anybody help me figure out how to convert my data accordingly?
推荐答案
如果我们将EMAIL
列视为一种交易ID,则可以通过以下方式将您的data.frame
转换为类transactions
:
If we consider the column EMAIL
as a sort of transaction ID, we can transform your data.frame
to class transactions
by:
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
这篇关于将csv转换为交易规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!