use*_*903 5 r matrix mean sparse-matrix
我有一个与之相似的稀疏矩阵,但是更大。
library(Matrix)
dfmtest<-new("dgCMatrix"
, i = c(0L, 1L, 2L, 4L, 5L, 6L, 8L, 0L, 1L, 2L, 3L, 4L, 6L, 7L, 8L,
0L, 2L, 3L, 6L, 7L, 8L, 1L, 2L, 4L, 5L, 6L, 7L, 8L, 9L, 0L, 1L,
3L, 4L, 5L, 6L, 7L, 8L, 9L, 0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L,
9L, 0L, 1L, 3L, 4L, 6L, 7L, 8L, 9L, 0L, 2L, 3L, 5L, 6L, 7L, 9L,
0L, 1L, 2L, 3L, 4L, 5L, 6L, 8L, 9L, 0L, 1L, 2L, 3L, 4L, 5L, 6L,
7L, 9L)
, p = c(0L, 7L, 15L, 21L, 29L, 38L, 48L, 56L, 63L, 72L, 81L)
, Dim = c(10L, 10L)
, Dimnames = list(NULL, NULL)
, x = c(4, 3, 1, 2, 3, 1, 2, 1, 3, 3, 2, 3, 3, 3, 4, 2, 1, 2, 3, 2,
1, 4, 1, 2, 2, 3, 2, 3, 4, 1, 4, 1, 3, 4, 3, 2, 2, 2, 4, 1, 2,
2, 1, 2, 3, 1, 1, 1, 4, 1, 1, 2, 1, 1, 1, 4, 3, 3, 2, 1, 2, 2,
1, 1, 3, 3, 4, 1, 2, 4, 2, 4, 1, 2, 2, 3, 4, 2, 1, 2, 4)
, factors = list()
)
Run Code Online (Sandbox Code Playgroud)
我希望能够找到每列(最终是行)的平均值,但不包括0值。如果我尝试手动执行此操作,则由于稀疏矩阵的大小,会遇到内存问题。
nzmean <- function(x) {
mean(x[x!=0])
}
dfmmeans <- apply(dfmtest, 2, nzmean)
# 1 2 3 4 5 6 7 8
#2.285714 2.750000 1.833333 2.625000 2.444444 1.800000 1.875000 2.000000
# 9 10
#2.666667 2.333333
Run Code Online (Sandbox Code Playgroud)
当我在实际矩阵上运行以上代码时,出现以下错误:
Error in asMethod(object) :
Cholmod error 'problem too large' at file ../Core/cholmod_dense.c, line 105
Run Code Online (Sandbox Code Playgroud)
我也研究过使用该colMeans函数,但看起来好像它在计算中包括了所有0值。
dfmmeans <- colMeans(dfmtest)
#[1] 1.6 2.2 1.1 2.1 2.2 1.8 1.5 1.4 2.4 2.1
Run Code Online (Sandbox Code Playgroud)
在大的稀疏矩阵上有做到这一点的好方法吗?
Matrix 有一个很好的summary方法,它返回矩阵中非零元素的 i、j、x 数据框,可以使用aggregate(或 dplyr 或 data.table,如果你喜欢)轻松总结:
library(Matrix)
str(summary(dfmtest))
#> Classes 'sparseSummary' and 'data.frame': 81 obs. of 3 variables:
#> $ i: int 1 2 3 5 6 7 9 1 2 3 ...
#> $ j: int 1 1 1 1 1 1 1 2 2 2 ...
#> $ x: num 4 3 1 2 3 1 2 1 3 3 ...
#> - attr(*, "header")= chr "10 x 10 sparse Matrix of class \"dgCMatrix\", with 81 entries"
aggregate(x ~ j, summary(dfmtest), mean)
#> j x
#> 1 1 2.285714
#> 2 2 2.750000
#> 3 3 1.833333
#> 4 4 2.625000
#> 5 5 2.444444
#> 6 6 1.800000
#> 7 7 1.875000
#> 8 8 2.000000
#> 9 9 2.666667
#> 10 10 2.333333
Run Code Online (Sandbox Code Playgroud)
如果你想要一个纯粹的矩阵 ops 版本,你可以使用abs(sign(...))将所有非稀疏元素转换为一个,这样你就可以只用colSums以下方法计算列均值:
colSums(dfmtest) / colSums(abs(sign(dfmtest)))
#> [1] 2.285714 2.750000 1.833333 2.625000 2.444444 1.800000 1.875000
#> [8] 2.000000 2.666667 2.333333
Run Code Online (Sandbox Code Playgroud)
确实colMeans不支持去除零:
getMethod("colMeans", "dgCMatrix")
#Method Definition:
#
#function (x, na.rm = FALSE, dims = 1, ...)
#{
# .local <- function (x, na.rm = FALSE, dims = 1, sparseResult = FALSE)
# .Call(dgCMatrix_colSums, x, na.rm, sparseResult, FALSE, TRUE)
# .local(x, na.rm, dims, ...)
#}
#<environment: namespace:Matrix>
Run Code Online (Sandbox Code Playgroud)
所以我们需要计算出我们自己的函数。
colMeans_drop0 <- function (dgCMat) {
nnz_per_col <- diff(dgCMat@p)
ColInd <- rep.int(1:ncol(dgCMat), nnz_per_col)
sapply(split(dgCMat@x, ColInd), mean)
}
colMeans_drop0(dfmtest)
# 1 2 3 4 5 6 7 8
#2.285714 2.750000 1.833333 2.625000 2.444444 1.800000 1.875000 2.000000
# 9 10
#2.666667 2.333333
Run Code Online (Sandbox Code Playgroud)
注意:全零的列将被忽略。相似地:
rowMeans_drop0 <- function (dgCMat) {
RowInd <- dgCMat@i + 1
sapply(split(dgCMat@x, RowInd), mean)
}
Run Code Online (Sandbox Code Playgroud)
并且全零的行将被忽略。
评论
alistaire的回答也很好。
summary+方法aggregate是这个答案中想法的不同实现。
getMethod("summary", "sparseMatrix")
#Method Definition:
#
#function (object, ...)
#{
# d <- dim(object)
# T <- as(object, "TsparseMatrix")
# r <- if (is(object, "nsparseMatrix"))
# data.frame(i = T@i + 1L, j = T@j + 1L)
# else data.frame(i = T@i + 1L, j = T@j + 1L, x = T@x)
# attr(r, "header") <- sprintf("%d x %d sparse Matrix of class \"%s\", with %d entries",
# d[1], d[2], class(object), length(T@i))
# class(r) <- c("sparseSummary", class(r))
# r
#}
#<environment: namespace:Matrix>
Run Code Online (Sandbox Code Playgroud)
summary首先将任何稀疏矩阵类强制转换为"dgTMatrix"类,即三元组格式,并且内部aggregate依赖于split+ lapply。
colSums如果您想保留全零列的结果(当然是 0),那么使用 using 的想法可能是理想的。
与20650讨论
我们的函数的基于/colSums的rowSums实现也是可能的。
colMeans_drop0 <- function (dgCMat) {
nnz_per_col <- diff(dgCMat@p)
nnz_per_col[nnz_per_col == 0] <- 1 ## just avoid doing 0 / 0
setNames(colSums(dgCMat) / nnz_per_col, 1:ncol(dgCMat))
}
rowMeans_drop0 <- function (dgCMat) {
RowInd <- dgCMat@i + 1
nnz_per_row <- tabulate(RowInd)
nnz_per_row[nnz_per_row == 0] <- 1 ## just avoid doing 0 / 0
setNames(rowSums(dgCMat) / nnz_per_row, 1:nrow(dgCMat))
}
Run Code Online (Sandbox Code Playgroud)
由于colSums/rowSums删除了暗名称,我们将它们添加到 中setNames。这两个函数保留全零列/行的结果。我们还使用tabulate函数来有效地计算行上非零条目的数量。