我有几个历史订单信息,其中两列作为数据框:订单 ID 和项目。它包含大约 100 万条记录。我正在尝试使用此数据框进行关联规则挖掘,为了利用 arules 包,我必须将大数据框转换为事务格式。但是,转换需要很长时间,我尝试使用具有相同结构的较小数据帧(300K 行(,转换需要几秒钟才能完成,但对于较大的数据帧,它需要永远。由于我将处理更大的数据集以进行关联规则挖掘,因此是否有更有效的方法来实现此目的?
我正在使用一台相当强大的机器,并在较小的数据帧中取得了成功。以下是我用于进行转换的代码。
library(tidyverse)
library(arules)
OrderID<-c("0001","0001","0002","0002")
Item<-c("ProductA","ProductB","ProductB","ProductC")
df<-data.frame(OrderID,Item)
df$OrderID<-as.factor(df$OrderID)
df$Item<-as.factor(df$Item)
df_trans<-as(split(df[,"Item"],df[,"OrderID"]),"transactions")
这是一个常见问题。以下是?transactions
手册页中的解决方案:
## example 4: creating transactions from a data.frame with
## transaction IDs and items (by converting it into a list of transactions first)
a_df3 <- data.frame(
TID = c(1,1,2,2,2,3),
item=c("a","b","a","b","c", "b")
)
a_df3
trans4 <- as(split(a_df3[,"item"], a_df3[,"TID"]), "transactions")
trans4
inspect(trans4)
## Note: This is very slow for large datasets. It is much faster to
## read transactions using read.transactions() with format = "single".
## This can be done using an anonymous file.
write.table(a_df3, file = tmp <- file(), row.names = FALSE)
trans4 <- read.transactions(tmp, format = "single",
header = TRUE, cols = c("TID", "item"))
close(tmp)
inspect(trans4)