3

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

我的导入数据是一个像这样的稀疏矩阵:

       i    j    x
1 2 3 1
2 3 5 1
3 3 1 1
4 2 5 1
。. . .
. . . .
200000000 . . .

稀疏矩阵大小为200,000,000 X 3,矩阵为200000 X 100000(大数据?)

我想用这些数据在 R 中做关联规则,
是使用 ' Package arules ' 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”

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

4

2 回答 2

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)
于 2013-05-03T11:30:20.813 回答
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

这一切都有效。

于 2016-10-17T23:22:47.727 回答