Python-总和4D数组 [英] Python - Sum 4D Array
问题描述
给出一个4D
数组M: (m, n, r, r)
,我如何求和所有m * n
内部矩阵(形状为(r, r)
)以得到形状为(r * r)
的新矩阵?
Given a 4D
array M: (m, n, r, r)
, how can I sum all the m * n
inner matrices (of shape (r, r)
) to get a new matrix of shape (r * r)
?
例如
M [[[[ 4, 1],
[ 2, 1]],
[[ 8, 2],
[ 4, 2]]],
[[[ 8, 2],
[ 4, 2]],
[[ 12, 3],
[ 6, 3]]]]
我希望结果应该是
[[32, 8],
[16, 8]]
推荐答案
您可以使用 einsum :
In [21]: np.einsum('ijkl->kl', M)
Out[21]:
array([[32, 8],
[16, 8]])
其他选项包括将前两个轴重塑为一个轴,然后调用sum
:
In [24]: M.reshape(-1, 2, 2).sum(axis=0)
Out[24]:
array([[32, 8],
[16, 8]])
或两次调用sum方法:
or calling the sum method twice:
In [26]: M.sum(axis=0).sum(axis=0)
Out[26]:
array([[32, 8],
[16, 8]])
但是使用np.einsum
更快:
In [22]: %timeit np.einsum('ijkl->kl', M)
100000 loops, best of 3: 2.42 µs per loop
In [25]: %timeit M.reshape(-1, 2, 2).sum(axis=0)
100000 loops, best of 3: 5.69 µs per loop
In [43]: %timeit np.sum(M, axis=(0,1))
100000 loops, best of 3: 6.08 µs per loop
In [33]: %timeit sum(sum(M))
100000 loops, best of 3: 8.18 µs per loop
In [27]: %timeit M.sum(axis=0).sum(axis=0)
100000 loops, best of 3: 9.83 µs per loop
注意:由于许多因素(操作系统,NumPy版本,NumPy库,硬件等),timeit基准测试可能会有很大差异.有时,各种方法的相对性能也可能取决于M的大小.因此,有必要在更接近实际使用情况的M上做自己的基准测试.
Caveat: timeit benchmarks can vary significantly due to many factors (OS, NumPy version, NumPy libraries, hardware, etc). The relative performance of various methods can sometimes also depend on the size of M. So it pays to do your own benchmarks on an M which is closer to your actual use case.
例如,对于稍大的数组M
,两次调用sum
方法可能是最快的:
For example, for slightly larger arrays M
, calling the sum
method twice may be fastest:
In [34]: M = np.random.random((100,100,2,2))
In [37]: %timeit M.sum(axis=0).sum(axis=0)
10000 loops, best of 3: 59.9 µs per loop
In [39]: %timeit np.einsum('ijkl->kl', M)
10000 loops, best of 3: 99 µs per loop
In [40]: %timeit np.sum(M, axis=(0,1))
10000 loops, best of 3: 182 µs per loop
In [36]: %timeit M.reshape(-1, 2, 2).sum(axis=0)
10000 loops, best of 3: 184 µs per loop
In [38]: %timeit sum(sum(M))
1000 loops, best of 3: 202 µs per loop
这篇关于Python-总和4D数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!