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”

アプリオリ関数で疎行列を使用できますか?
間違ったパッケージを使用している可能性がありますか?sparse matrix-> matrix->association rule
が必要ですか? または疎行列 -> 関連付けルール?

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 に答える