创建共生矩阵

jac*_*tra 28 r matrix

我正在尝试解决共同出现矩阵的问题.我有一个事务和项目的数据文件,我想看到一起显示项目的事务数量的矩阵.

我是R编程的新手,我很乐意找到R所有的快捷方式,而不是创建特定的循环(我以前使用的是C年前,现在只坚持使用Excel宏和SPSS).我已经检查过这里的解决方案,但是没有找到一个有效的方法(最接近的是这里给出的解决方案:使用SAC的共生矩阵? - 但是当我使用projection_tm时它产生了一条错误信息,我怀疑cbind不是'在我的案例中成功.

基本上我有一个包含以下内容的表:

TrxID Items Quant
Trx1 A 3
Trx1 B 1
Trx1 C 1
Trx2 E 3
Trx2 B 1
Trx3 B 1
Trx3 C 4
Trx4 D 1
Trx4 E 1
Trx4 A 1
Trx5 F 5
Trx5 B 3
Trx5 C 2
Trx5 D 1, etc.
Run Code Online (Sandbox Code Playgroud)

我想创建类似的东西:

   A B C D E F
A  0 1 1 0 1 1
B  1 0 3 1 1 0
C  1 3 0 1 0 0
D  1 1 1 0 1 1
E  1 1 0 1 0 0
F  0 1 1 1 0 0
Run Code Online (Sandbox Code Playgroud)

我做的是(你可能会笑我的菜鸟R方法):

library(igraph)
library(tnet)

trx <- read.table("FileName.txt", header=TRUE) 
transID <- t(trx[1])
items <- t(trx[2])

id_item <- cbind(items,transID)
item_item <- projecting_tm(id_item, method="sum")
item_item <- tnet_igraph(item_item,type="weighted one-mode tnet")
item_matrix <-get.adjacency(item_item,attr="weight")
item_matrix
Run Code Online (Sandbox Code Playgroud)

如上所述,cbind可能不成功,所以projection_tm无法给我任何结果.

任何替代方法或我的方法的更正?

非常感谢您的帮助!

A5C*_*2T1 23

使用上述任一答案中的"dat",尝试crossprodtable:

V <- crossprod(table(dat[1:2]))
diag(V) <- 0
V
#      Items
# Items A B C D E F
#     A 0 1 1 1 1 0
#     B 1 0 3 1 1 1
#     C 1 3 0 1 0 1
#     D 1 1 1 0 1 1
#     E 1 1 0 1 0 0
#     F 0 1 1 1 0 0
Run Code Online (Sandbox Code Playgroud)


Tyl*_*ker 18

我将使用reshape2包和矩阵代数的组合:

#read in your data
dat <- read.table(text="TrxID Items Quant
Trx1 A 3
Trx1 B 1
Trx1 C 1
Trx2 E 3
Trx2 B 1
Trx3 B 1
Trx3 C 4
Trx4 D 1
Trx4 E 1
Trx4 A 1
Trx5 F 5
Trx5 B 3
Trx5 C 2
Trx5 D 1", header=T)

#making the boolean matrix   
library(reshape2)
dat2 <- melt(dat)
w <- dcast(dat2, Items~TrxID)
x <- as.matrix(w[,-1])
x[is.na(x)] <- 0
x <- apply(x, 2,  function(x) as.numeric(x > 0))  #recode as 0/1
v <- x %*% t(x)                                   #the magic matrix 
diag(v) <- 0                                      #repalce diagonal
dimnames(v) <- list(w[, 1], w[,1])                #name the dimensions
v
Run Code Online (Sandbox Code Playgroud)

对于图表可能......

g <- graph.adjacency(v, weighted=TRUE, mode ='undirected')
g <- simplify(g)
# set labels and degrees of vertices
V(g)$label <- V(g)$name
V(g)$degree <- degree(g)
plot(g)
Run Code Online (Sandbox Code Playgroud)


Mar*_*ker 15

出于效率原因,尤其是稀疏数据,我建议使用稀疏矩阵.

dat <- read.table(text="TrxID Items Quant
Trx1 A 3
Trx1 B 1
Trx1 C 1
Trx2 E 3
Trx2 B 1
Trx3 B 1
Trx3 C 4
Trx4 D 1
Trx4 E 1
Trx4 A 1
Trx5 F 5
Trx5 B 3
Trx5 C 2
Trx5 D 1", header=T)

library("Matrix")

# factors for indexing matrix entries and naming dimensions
trx.fac <- factor(dat[,1])
itm.fac <- factor(dat[,2])

s <- sparseMatrix(
        as.numeric(trx.fac), 
        as.numeric(itm.fac),
        dimnames = list(
                as.character(levels(trx.fac)), 
                as.character(levels(itm.fac))),
        x = 1)

# calculating co-occurrences
v <- t(s) %*% s

# setting transactions counts of items to zero
diag(v) <- 0
v
Run Code Online (Sandbox Code Playgroud)

我试着在这个帖子中发布每个解决方案.它们都不适用于大型矩阵(我使用的是1,500 x 2,000,000矩阵).

有点偏离主题:在计算共生矩阵之后,我通常想要计算各个项目之间的距离.可以在共生矩阵上有效地计算余弦相似度/距离,如下所示:

# cross-product of vectors (numerator)
num <- v %*% v

# square root of square sum of each vector (used for denominator)
srss <- sqrt(apply(v^2, 1, sum))

# denominator
den <- srss %*% t(srss)

# cosine similarity
v.cos.sim <- num / den

# cosine distance
v.cos.dist <- 1 - v.cos.sim
Run Code Online (Sandbox Code Playgroud)


Ian*_*ske 11

我会用xtabs:

dat <- read.table(text="TrxID Items Quant
Trx1 A 3
Trx1 B 1
Trx1 C 1
Trx2 E 3
Trx2 B 1
Trx3 B 1
Trx3 C 4
Trx4 D 1
Trx4 E 1
Trx4 A 1
Trx5 F 5
Trx5 B 3
Trx5 C 2
Trx5 D 1", header=T)


term_doc <- xtabs(~ TrxID + Items, data=dat, sparse = TRUE)
co_occur <- crossprod(term_doc, term_doc)
diag(co_occur) <- 0
co_occur
Run Code Online (Sandbox Code Playgroud)

我向它sparse = TRUE表明,这可以用于非常大的数据集.


Gab*_*rdi 5

如果首先创建一个二部图,实际上是非常容易且干净的,其中顶部节点是事务,底部节点是项目。然后,创建一个到底部节点的投影。

dat <- read.table(text="TrxID Items Quant
Trx1 A 3
Trx1 B 1
Trx1 C 1
Trx2 E 3
Trx2 B 1
Trx3 B 1
Trx3 C 4
Trx4 D 1
Trx4 E 1
Trx4 A 1
Trx5 F 5
Trx5 B 3
Trx5 C 2
Trx5 D 1", header=T)

library(igraph)
bip <- graph.data.frame(dat)
V(bip)$type <- V(bip)$name %in% dat[,1]

## sparse=TRUE is a good idea if you have a large matrix here
v <- get.adjacency(bipartite.projection(bip)[[2]], attr="weight", sparse=FALSE)

## Need to reorder if you want it alphabetically
v[order(rownames(v)), order(colnames(v))]

#   A B C D E F
# A 0 1 1 1 1 0
# B 1 0 3 1 1 1
# C 1 3 0 1 0 1
# D 1 1 1 0 1 1
# E 1 1 0 1 0 0
# F 0 1 1 1 0 0
Run Code Online (Sandbox Code Playgroud)