如何评估数组块内的值之和 [英] How to evaluate the sum of values within array blocks

查看:75
本文介绍了如何评估数组块内的值之和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据数组,形状为100x100.我想将其划分为5x5的块,每个块有20x20的网格.我想要的每个块的值是其中所有值的总和.

I have data array, with shape 100x100. I want to divide it into 5x5 blocks, and each block has 20x20 grids. The value of each block I want is the sum of all values in it.

是否有更优雅的方法来完成它?

Is there a more elegant way to accomplish it?

x = np.arange(100)
y = np.arange(100)
X, Y = np.meshgrid(x, y)
Z = np.cos(X)*np.sin(Y)
Z_new = np.zeros((5, 5))
for i in range(5):
  for j in range(5):
    Z_new[i, j] = np.sum(Z[i*20:20+i*20, j*20:20+j*20])

这是基于索引的,如果基于x呢?

This is based on index, how if based on x?

x = np.linspace(0, 1, 100)
y = np.linspace(0, 1, 100)
X, Y = np.meshgrid(x, y)
Z = np.cos(X)*np.sin(Y)
x_new = np.linspace(0, 1, 15)
y_new = np.linspace(0, 1, 15)

Z_new?

推荐答案

简单地

Simply reshape splitting each of those two axes into two each with shape (5,20) to form a 4D array and then sum reduce along the axes having the lengths 20, like so -

Z_new = Z.reshape(5,20,5,20).sum(axis=(1,3))

功能相同,但具有

Functionally the same, but potentially faster option with np.einsum -

Z_new = np.einsum('ijkl->ik',Z.reshape(5,20,5,20))

通用块大小

扩展到一般情况-

H,W = 5,5 # block-size
m,n = Z.shape
Z_new = Z.reshape(H,m//H,W,n//W).sum(axis=(1,3))

随着einsum变为-

Z_new = np.einsum('ijkl->ik',Z.reshape(H,m//H,W,n//W))

要计算跨块的平均值/均值,请使用mean而不是sum方法.

To compute average/mean across blocks, use mean instead of sum method.

常规块大小和缩小操作

扩展使用具有reduction操作.reduce"rel =" nofollow noreferrer> ufuncs 支持多个axes参数和axis来进行缩减,应该是-

Extending to use reduction operations that have ufuncs supporting multiple axes parameter with axis for reductions, it would be -

def blockwise_reduction(a, height, width, reduction_func=np.sum):
    m,n = a.shape
    a4D = a.reshape(height,m//height,width,n//width)
    return reduction_func(a4D,axis=(1,3))

因此,要解决我们的具体情况,应该是:

Thus, to solve our specific case, it would be :

blockwise_reduction(Z, height=5, width=5)

对于逐段​​平均计算,将为-

and for a block-wise average computation, it would be -

blockwise_reduction(Z, height=5, width=5, reduction_func=np.mean)

这篇关于如何评估数组块内的值之和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆