通过这些条目的值复制稀疏矩阵中非零条目的行索引
Replicate row indices of non-zero entries in a sparse matrix by values of those entries
给定一个大的稀疏整数矩阵,我想要一个列表,其中列表的每个元素都是一个向量,其中包含相应行中非零元素的索引,复制为元素指定的次数。矩阵很大,所以我需要一个可扩展的解决方案。
这是一个运行速度非常慢的代码示例。
sparse_matrix <- matrix(c(1, 0, 0, 0, 2, 0, 1, 5, 0, 0, 0, 0), nrow = 2)
# [,1] [,2] [,3] [,4] [,5] [,6]
#[1,] 1 0 2 1 0 0
#[2,] 0 0 0 5 0 0
# A very slow attempt that gives the right answer
lapply(seq_len(nrow(sparse_matrix)), function(r) {
rep(seq_len(ncol(sparse_matrix)), sparse_matrix[r,])
} )
#[[1]]
#[1] 1 3 3 4
#
#[[2]]
#[1] 4 4 4 4 4
编辑: 我意识到我在原来的 post 中错误地指定了我的问题——抱歉。请参考上面的例子。
编辑 2: 为一个用例安排两个解决方案的时间:
sparse_matrix <- rsparsematrix (1E4,1E3, 0.01,rand.x = function(n) 1 + round(abs(rnorm(n))))
sparse_matrix <- as.matrix(sparse_matrix)
# 李哲源
ptm <- proc.time()
spM <- as(sparse_matrix, "dgRMatrix")
RowNumber <- rep(1:nrow(spM), diff(spM@p))
ColInd <- split(spM@j + 1, RowNumber)
nze <- split(spM@x, RowNumber)
output <- mapply(rep, ColInd, nze)
print(proc.time() - ptm)
# 0.232 seconds
#akrun
ptm <- proc.time()
v1 <- c(col(sparse_matrix) * !! sparse_matrix)
v1 <- setNames(v1, t(row(sparse_matrix)))
output <- rep(v1, sparse_matrix)
print(proc.time() - ptm)
# 1.8 seconds
我们可以使用 which
且 arr.ind
为 TRUE 来获取 matrix
中的行和列索引
which(sparse_matrix !=0, arr.ind = TRUE)
第二种情况
rep(col(sparse_matrix) * !! sparse_matrix, sparse_matrix)
给出一个vector
,但是如果我们需要一个标识符,那么创建一个named
向量
v1 <- c(col(sparse_matrix) * !! sparse_matrix)
v1 <- setNames(v1, t(row(sparse_matrix)))
rep(v1, sparse_matrix)
#1 1 1 2 2 2 2 2 2
#1 3 3 4 4 4 4 4 4
您是否熟悉用于稀疏矩阵的压缩行存储?您想要的索引只是此类存储中的关键组件。 R 包 Matrix
有它的 "dgRMatrix" class。
library(Matrix)
spM <- as(sparse_matrix, "dgRMatrix")
## which row do those non-zero entries lie?
RowNumber <- rep(1:nrow(spM), diff(spM@p))
## position index of those entries on each row, i.e., column index
ColInd <- split(spM@j + 1, RowNumber)
## none-zero-element on each row
nze <- split(spM@x, RowNumber)
## expand position index by matrix value
mapply(rep, ColInd, nze)
#$`1`
#[1] 1 3 3 4
#$`2`
#[1] 4 4 4 4 4
If the matrix is stored as a "dgCMatrix", can it be converted to "dgRMatrix"? In that case, the first line gives: no method or default for coercing 'dgCMatrix' to dgRMatrix'
这不是从 "dgCMatrix" 到 "dgRMatrix" 的强制方法。 sparse_matrix
与 post 中的密集矩阵一样。所以 as
背后的强制是从 "matrix" 到 "dgRMatrix".
但是,如果您已经将它作为 "dgCMatrix",那么您可以先转置它,然后在此 "dgCMatrix" 上做类似的事情。见下文。
spM <- as(sparse_matrix, "dgCMatrix")
## transpose
spM <- t(spM)
## which column do those non-zero entries lie?
ColNumber <- rep(1:ncol(spM), diff(spM@p))
## position index of those entries on each column, i.e., row index
RowInd <- split(spM@i + 1, ColNumber)
## none-zero-element on each column
nze <- split(spM@x, ColNumber)
## expand position index by matrix value
mapply(rep, RowInd, nze)
#$`1`
#[1] 1 3 3 4
#$`2`
#[1] 4 4 4 4 4
感谢 user20650 的(大)改进。
第一种情况"dgRMatrix",
spM <- as(sparse_matrix, "dgRMatrix")
RowNumber <- rep(1:nrow(spM), diff(spM@p))
split(rep(spM@j + 1, spM@x), rep(RowNumber, spM@x))
第二种情况"dgCMatrix"
spM <- as(sparse_matrix, "dgCMatrix")
ColInd <- rep(1:ncol(spM), diff(spM@p))
split(rep(ColInd, spM@x), rep(spM@i, spM@x))
给定一个大的稀疏整数矩阵,我想要一个列表,其中列表的每个元素都是一个向量,其中包含相应行中非零元素的索引,复制为元素指定的次数。矩阵很大,所以我需要一个可扩展的解决方案。
这是一个运行速度非常慢的代码示例。
sparse_matrix <- matrix(c(1, 0, 0, 0, 2, 0, 1, 5, 0, 0, 0, 0), nrow = 2)
# [,1] [,2] [,3] [,4] [,5] [,6]
#[1,] 1 0 2 1 0 0
#[2,] 0 0 0 5 0 0
# A very slow attempt that gives the right answer
lapply(seq_len(nrow(sparse_matrix)), function(r) {
rep(seq_len(ncol(sparse_matrix)), sparse_matrix[r,])
} )
#[[1]]
#[1] 1 3 3 4
#
#[[2]]
#[1] 4 4 4 4 4
编辑: 我意识到我在原来的 post 中错误地指定了我的问题——抱歉。请参考上面的例子。
编辑 2: 为一个用例安排两个解决方案的时间:
sparse_matrix <- rsparsematrix (1E4,1E3, 0.01,rand.x = function(n) 1 + round(abs(rnorm(n))))
sparse_matrix <- as.matrix(sparse_matrix)
# 李哲源
ptm <- proc.time()
spM <- as(sparse_matrix, "dgRMatrix")
RowNumber <- rep(1:nrow(spM), diff(spM@p))
ColInd <- split(spM@j + 1, RowNumber)
nze <- split(spM@x, RowNumber)
output <- mapply(rep, ColInd, nze)
print(proc.time() - ptm)
# 0.232 seconds
#akrun
ptm <- proc.time()
v1 <- c(col(sparse_matrix) * !! sparse_matrix)
v1 <- setNames(v1, t(row(sparse_matrix)))
output <- rep(v1, sparse_matrix)
print(proc.time() - ptm)
# 1.8 seconds
我们可以使用 which
且 arr.ind
为 TRUE 来获取 matrix
which(sparse_matrix !=0, arr.ind = TRUE)
第二种情况
rep(col(sparse_matrix) * !! sparse_matrix, sparse_matrix)
给出一个vector
,但是如果我们需要一个标识符,那么创建一个named
向量
v1 <- c(col(sparse_matrix) * !! sparse_matrix)
v1 <- setNames(v1, t(row(sparse_matrix)))
rep(v1, sparse_matrix)
#1 1 1 2 2 2 2 2 2
#1 3 3 4 4 4 4 4 4
您是否熟悉用于稀疏矩阵的压缩行存储?您想要的索引只是此类存储中的关键组件。 R 包 Matrix
有它的 "dgRMatrix" class。
library(Matrix)
spM <- as(sparse_matrix, "dgRMatrix")
## which row do those non-zero entries lie?
RowNumber <- rep(1:nrow(spM), diff(spM@p))
## position index of those entries on each row, i.e., column index
ColInd <- split(spM@j + 1, RowNumber)
## none-zero-element on each row
nze <- split(spM@x, RowNumber)
## expand position index by matrix value
mapply(rep, ColInd, nze)
#$`1`
#[1] 1 3 3 4
#$`2`
#[1] 4 4 4 4 4
If the matrix is stored as a "dgCMatrix", can it be converted to "dgRMatrix"? In that case, the first line gives: no method or default for coercing 'dgCMatrix' to dgRMatrix'
这不是从 "dgCMatrix" 到 "dgRMatrix" 的强制方法。 sparse_matrix
与 post 中的密集矩阵一样。所以 as
背后的强制是从 "matrix" 到 "dgRMatrix".
但是,如果您已经将它作为 "dgCMatrix",那么您可以先转置它,然后在此 "dgCMatrix" 上做类似的事情。见下文。
spM <- as(sparse_matrix, "dgCMatrix")
## transpose
spM <- t(spM)
## which column do those non-zero entries lie?
ColNumber <- rep(1:ncol(spM), diff(spM@p))
## position index of those entries on each column, i.e., row index
RowInd <- split(spM@i + 1, ColNumber)
## none-zero-element on each column
nze <- split(spM@x, ColNumber)
## expand position index by matrix value
mapply(rep, RowInd, nze)
#$`1`
#[1] 1 3 3 4
#$`2`
#[1] 4 4 4 4 4
感谢 user20650 的(大)改进。
第一种情况"dgRMatrix",
spM <- as(sparse_matrix, "dgRMatrix")
RowNumber <- rep(1:nrow(spM), diff(spM@p))
split(rep(spM@j + 1, spM@x), rep(RowNumber, spM@x))
第二种情况"dgCMatrix"
spM <- as(sparse_matrix, "dgCMatrix")
ColInd <- rep(1:ncol(spM), diff(spM@p))
split(rep(ColInd, spM@x), rep(spM@i, spM@x))