[英]Calculate rowSums for three-dimensional array without for-loop/apply
取数组b_array
:
set.seed(123)
a_mtx = matrix(1:15,ncol=5)
b_mtx = matrix(seq(1,5,length.out=30),ncol=5)
b_array =
array(
b_mtx,
dim =
c(
nrow(b_mtx),
ncol(b_mtx),
nrow(a_mtx)
)
)
如果我想计算b_array
的每个“切片”或“工作表”的每一列的总和,我可以使用colSums
及其维度参数:
colSums(b_array, dim = 1)
# [,1] [,2] [,3]
#[1,] 8.068966 8.068966 8.068966
#[2,] 13.034483 13.034483 13.034483
#[3,] 18.000000 18.000000 18.000000
#[4,] 22.965517 22.965517 22.965517
#[5,] 27.931034 27.931034 27.931034
为了对行总和做同样的事情,我不能使用rowSums
的维度参数,因为它的处理方式不同,所以我求助于apply
:
apply(b_array, 3, rowSums)
# [,1] [,2] [,3]
#[1,] 13.27586 13.27586 13.27586
#[2,] 13.96552 13.96552 13.96552
#[3,] 14.65517 14.65517 14.65517
#[4,] 15.34483 15.34483 15.34483
#[5,] 16.03448 16.03448 16.03448
#[6,] 16.72414 16.72414 16.72414
我希望对具有更大维度的数组执行相同的计算,因此apply
和其他 for 循环方法效率不高。
有没有替代的、真正的矢量化方法?
默认的思考(我相信)与问候到MARGIN=
(第二个)参数apply
的是,它意味着(汇总时...简化的效果在这里)“也就是减少了轴”。 然而,另一种看待它的方式是所有其他维度都保持不变。
例如, colSums(ary)
的有效等效项是apply(ary, 2, sum)
,意思是“保持轴 1 未减少” 。 ( colSums
实际上是在内部完成的,而不是apply
。)所以要扩展“除”逻辑之外的所有轴,让我们为您的b_array
实现您希望保留第 1 个和第 3 个轴,所以这样做
apply(b_array, c(1,3), sum)
# [,1] [,2] [,3]
# [1,] 13.27586 13.27586 13.27586
# [2,] 13.96552 13.96552 13.96552
# [3,] 14.65517 14.65517 14.65517
# [4,] 15.34483 15.34483 15.34483
# [5,] 16.03448 16.03448 16.03448
# [6,] 16.72414 16.72414 16.72414
在使用n维数组进行“列”求和时,效率与(我认为)一样有效。
编辑:
@markus 使用aperm
的速度更快,适用于各种矩阵大小,尽管它似乎收敛于更大的矩阵。
ns <- c(10,50,100,1000)
set.seed(123)
arrays <- lapply(ns, function(n) array(runif(3*n*n), dim=c(n,n,3)))
mapply(identical,
lapply(arrays, function(a) t(colSums(aperm(a, perm = c(2, 3, 1))))),
lapply(arrays, function(a) apply(a, c(1,3), sum)))
# [1] TRUE TRUE TRUE TRUE
library(microbenchmark)
microbenchmark(
aperm10 = t(colSums(aperm(arrays[[1]], perm = c(2, 3, 1)))),
aperm50 = t(colSums(aperm(arrays[[2]], perm = c(2, 3, 1)))),
aperm100 = t(colSums(aperm(arrays[[3]], perm = c(2, 3, 1)))),
aperm1000 = t(colSums(aperm(arrays[[4]], perm = c(2, 3, 1)))),
apply10 = apply(arrays[[1]], c(1,3), sum),
apply50 = apply(arrays[[2]], c(1,3), sum),
apply100 = apply(arrays[[3]], c(1,3), sum),
apply1000 = apply(arrays[[4]], c(1,3), sum),
times=10
)
# Unit: microseconds
# expr min lq mean median uq max neval
# aperm10 19.1 25.5 46.74 39.55 59.2 105.8 10
# aperm50 55.7 77.2 96.36 94.30 115.6 149.8 10
# aperm100 231.2 247.2 267.14 258.35 295.5 301.8 10
# aperm1000 47282.5 47568.4 49235.19 49581.85 50118.4 52034.4 10
# apply10 53.7 59.1 78.42 63.15 105.6 123.5 10
# apply50 263.9 282.3 318.08 306.60 366.4 383.0 10
# apply100 637.7 686.6 712.65 710.75 741.5 799.7 10
# apply1000 40173.7 52735.7 52170.08 54349.65 55692.9 57375.9 10
(我还没有测试过内存使用情况。)
使用aperm
另一种选择
t(colSums(aperm(b_array, perm = c(2, 3, 1))))
# [,1] [,2] [,3]
#[1,] 13.27586 13.27586 13.27586
#[2,] 13.96552 13.96552 13.96552
#[3,] 14.65517 14.65517 14.65517
#[4,] 15.34483 15.34483 15.34483
#[5,] 16.03448 16.03448 16.03448
#[6,] 16.72414 16.72414 16.72414
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.