2013-05-01 27 views
3

我从来没有使用R,但现在我需要导入一个稀疏矩阵做关联规则中的R

我的进口数据是一个稀疏矩阵是这样的:可以导入稀疏矩阵做R中的关联规则吗?

ĴX
。 。 。 。
。 。 。 。
200000000。 。 。

稀疏矩阵大小为2亿×3, 矩阵是200000 X 100000(大数据?)

我想用这个数据做关联规则在R,
是用'包装圆点'itemMatrix-class & tidLists-class()?或其他人?

怎么办?

我非常喜欢这一点,但不工作:

channel <- odbcConnect("test") 
data<-sqlQuery(channel,"select i,j,x from table") # it's work 
(args <- data.frame(data))     # it's work ,print sparse matrix 
# i j x 
#1 2 3 1 
#2 3 5 1 
#3 3 1 1 
#3 2 5 1 
# .... 
(Aa <- do.call(sparseMatrix, args))   # it's work ,print sparse Matrix of class "dgCMatrix" 
# 200000 X 100000 sparse Matrix of class "dgCMatrix" 
#  1 2 3 4 5.... 
# [1,] . . . . . 
# [2,] . . | . | 
# [3,] | . . . | 
# .... 
rules <- apriori(Aa)       # it's not work 

Error in as(data, "transactions") : 
no method or default for coercing “dgCMatrix” to “transactions” 

可以在先验函数使用稀疏矩阵?
也许我使用错误的包?
我需要稀疏矩阵 - >矩阵 - >关联规则
稀疏矩阵 - >关联规则

+0

*这不是工作*但还有什么?你能提供错误信息吗? – flodel 2013-05-02 00:42:49

+1

as(data,“transactions”)中的错误: 没有方法或强制将“dgCMatrix”强制为“transactions”的默认方法 – user2340340 2013-05-02 09:08:07

回答

1

进口I,J

library(RODBC) 
library(arulse) 
channel <- odbcConnect("DB", uid="XXXX", pwd="XXXX") 
data<-sqlQuery(channel,"select distinct i as TID,j as item from table") 
trans <- as(split(data[,"item"], data[,"TID"]), "transactions") # add this 
rules <- apriori(trans) 
+1

“split”操作速度令人难以置信,而且效率低下。由于“交易”对象在内部是稀疏矩阵,因此似乎应该有一种直接的方法将“矩阵”对象转换为“交易”对象。 – Zach 2013-11-20 19:24:16

0

在内部,arules曾经使用dgcMatrix,但切换到更高效的ngcMatrix(二进制)。如果我们转换到这一点,我们很酷。

library(tidyverse) 
library(arules) 

data = data.frame(ID = sample(LETTERS[1:3], 20, T), item = sample(letters[1:5], 20, T), stringsAsFactors = F) 

data %>% 
    unique %>% 
    xtabs(~ item + ID, data = ., sparse = T) -> 
    m 

head(m) 
#> 3 x 5 sparse Matrix of class "dgCMatrix" 
#> a b c d e 
#> A . 1 1 1 1 
#> B 1 . 1 1 1 
#> C . 1 1 1 . 

apriori(m) 
#> Error in as(data, "transactions"): no method or default for coercing "dgCMatrix" to "transactions" 

这是我们期望的错误 - 但如果我们转换为另一种稀疏矩阵(非常快) -

m1 <- as(m, "ngCMatrix") 

apriori(m1) 
#> Apriori 
#> 
#> Parameter specification: 
#> confidence minval smax arem aval originalSupport maxtime support minlen 
#>   0.8 0.1 1 none FALSE   TRUE  5  0.1  1 
#> maxlen target ext 
#>  10 rules FALSE 
#> 
#> Algorithmic control: 
#> filter tree heap memopt load sort verbose 
#>  0.1 TRUE TRUE FALSE TRUE 2 TRUE 
#> 
#> Absolute minimum support count: 0 
#> 
#> set item appearances ...[0 item(s)] done [0.00s]. 
#> set transactions ...[3 item(s), 5 transaction(s)] done [0.00s]. 
#> sorting and recoding items ... [3 item(s)] done [0.00s]. 
#> creating transaction tree ... done [0.00s]. 
#> checking subsets of size 1 2 3 done [0.00s]. 
#> writing ... [4 rule(s)] done [0.00s]. 
#> creating S4 object ... done [0.00s]. 
#> set of 4 rules 

它所有的作品。