一种方法是重新塑造这两个轴,使每个轴变成两个,并在后面的一个轴上找到均值,从而得到平均值。我们将保留使用 keepdims=True
的维度,以便稍后使用 np.repeat
复制沿着缩小的轴进行最终输出。
因此,一种实现方式是-
b = a.reshape(2,2,2,2).mean((1,3), keepdims=1)
out = np.repeat(np.repeat(b,(2),axis=(1)),(2), axis=3).reshape(4,4)
示例运行:
In [17]: a
Out[17]:
array([[ 1, 2, 3, 4],
[ 5, 6, 7, 8],
[ 9, 10, 11, 12],
[13, 14, 15, 16]])
In [18]: b = a.reshape(2,2,2,2).mean((1,3), keepdims=1)
In [19]: np.repeat(np.repeat(b,(2),axis=(1)),(2), axis=3).reshape(4,4)
Out[19]:
array([[ 3.5, 3.5, 5.5, 5.5],
[ 3.5, 3.5, 5.5, 5.5],
[ 11.5, 11.5, 13.5, 13.5],
[ 11.5, 11.5, 13.5, 13.5]])
通俗来说,解决方案如下所示 -
m,n = a.shape
p,q = (2,2)
b = a.reshape(m//p,p,n//q,q).mean((1,3), keepdims=1)
out = np.repeat(np.repeat(b,(p),axis=(1)),(q), axis=3).reshape(a.shape)
性能提升
我们可以用一个基于初始化的部分来替换那个复制的部分,像这样 -
out = np.empty((m//p,p,n//q,q),dtype=float)
out[:] = b
out.shape = a.shape