大数据集的市场桶分析

时间:2018-06-08 17:08:37

标签: r

我正在对包含2列(OrderID和Product)的大型数据集创建市场桶分析。集合中有超过一百万行,并且使用apriori packagage我能够使用较小的数据子集创建有效的规则列表,但是当尝试使用完整集时,我无法使用split函数通过OrderID聚合数据。是否有另一个具有类似功能的功能可以处理这么多数据?代码如下:

MyData <- read.csv("C:/Market Basket Analysis/BOD16-Data.csv") #Abreviated for proprietary reasons
View(MyData)

library(arules)
summary(MyData)

#Using the split function, we are able to aggregate the transactions, so that each
#product on the transaction is grouped into its respective, singular, transID

start.time <- Sys.time() #Timer used to measure run time on the split function
aggregateData <- split(MyData$Product, MyData$OrderID)
end.time<- Sys.time()

time.taken = end.time- start.time
time.taken


#Using the split function, we are able to aggregate the transactions, so that each
#product on the transaction is grouped into its respective, singular, transID
aggregateData <- split(MyData$Product, MyData$OrderID)
head(aggregateData)

#Need to convert the aggregated data into a form that 'Arules' package 
#can accept
txns <- as(aggregateData, "transactions")
#txns <- read.transactions("Trans", format = "basket", sep=",", rm.duplicates=TRUE)
summary(txns)


#Apriori Algorithem generates the rules 
Rules <- apriori(txns,parameter=list(supp=0.0025,conf=0.4,target="Rules",minlen=2))
inspect(Rules)

编辑: 我的数据如下:

OrderId     Product
1       1234
1       1357
1       2468
1       1324
2       1234
2       2468
3       4321
4       5432
5       1357

AggregateData should be:

[1]
1234,1357,2468,1324

[2]
1234, 2468

[3]
4321

[4]
5432

[5]
1357

目前我使用split函数来实现这些结果,但是当将它应用于更大的集合时,运行时间超过30分钟 在我停止剧本之前。

1 个答案:

答案 0 :(得分:1)

这对你来说更快吗?

library(dplyr) 

df <- tribble(
    ~OrderId, ~Product,
    1,       1234,
    1,       1357,
    1,       2468,
    1,       1324,
    2,       1234,
    2,       2468,
    3,       4321,
    4,       5432,
    5,       1357
    )

    df %>% 
         group_by(OrderId) %>% 
         summarize(Product = list(Product)) %>% 
         mutate(Product = purrr::set_names(Product, OrderId)) %>% 
         pull(Product)

因此,对于您的代码,您应该能够:

library(dplyr)

MyData <- read.csv("C:/Market Basket Analysis/BOD16-Data.csv")


 aggregateData <-   MyData %>% 
          group_by(OrderId) %>% 
          summarize(Product = list(Product)) %>% 
          mutate(Product = purrr::set_names(Product, OrderId)) %>% 
          pull(Product)

这应该与做的一样(希望更快):

MyData <- read.csv("C:/Market Basket Analysis/BOD16-Data.csv")

aggregateData <- split(MyData$Product, MyData$OrderID)