我有一个200K行x 27K列矩阵,我想将其转换为稀疏矩阵。我尝试过这样做,但是我遇到了分段错误:
> dim(my_regular)
[1] 196501 26791
> my_sparse <- as(my_regular, "sparseMatrix")
*** caught segfault ***
address 0x2b9e3e10e000, cause 'memory not mapped'
有更好的方法吗?
答案 0 :(得分:0)
这绝对不是理想的,但我能够实现转换的唯一方法是将50K行的矩阵分解,然后使用rbind
来组合它们:
my_sparse_1 <- Matrix::Matrix(my_regular[1:50000,], sparse = TRUE)
my_sparse_2 <- Matrix::Matrix(my_regular[50001:100000,], sparse = TRUE)
# etc.
my_sparse <- rbind(my_sparse_1, my_sparse_2, etc.)
如果有人有更好的建议,我不会接受我的回答
答案 1 :(得分:0)
首先,如果as(my_regular, "sparseMatrix")
给出了段错误,请向Matrix
软件包维护者报告(可在此处找到https://cran.r-project.org/web/packages/Matrix/index.html)。
作为一种解决方法,您可以使用以下内容:
library(Matrix)
nc = 50
nr = 100
sparsity = 0.9
m = sample(c(0, 1), size = nr * nc, replace = TRUE, prob = c(sparsity, 1 - sparsity))
m = matrix(m, nr, nc)
# normal way of coercing dense to sparse
spm1 = as(m, "CsparseMatrix")
# get indices of non-zero elements
ind_nnz = which(m != 0)
# construct zero-based indices of row and column
i = (ind_nnz - 1L) %% nr
j = as.integer((ind_nnz - 1L) / nr)
# get non-zero values
x = m[ind_nnz]
spm2 = sparseMatrix(i = i, j = j, x = x, dims = c(nr, nc), index1 = FALSE)
identical(spm1, spm2)
# TRUE