简体   繁体   English

如何有效地计算二维累积和

[英]How to compute 2D cumulative sum efficiently

Given a two-dimensional numerical array X of shape (m,n) , I would like to compute an array Y of the same shape, where Y[i,j] is the cumulative sum of X[i_,j_] for 0<=i_<=i, 0<=j_<=j .给定一个形状为(m,n)的二维数值数组X ,我想计算一个相同形状的数组Y ,其中Y[i,j]X[i_,j_]对于0<=i_<=i, 0<=j_<=j If X describes a 2D probability distribution, Y could be thought of as the 2D cumulative distribution function (CDF).如果X描述了一个二维概率分布,则Y可以被认为是二维累积分布 function (CDF)。

I can obviously compute all entries of Y in a double for loop.我显然可以在双for循环中计算Y的所有条目。 However, there is a recursive aspect to this computation, as Y[i,j] = X[i,j] + Y[i-1,j] + Y[i,j-1] - Y[i-1,j-1] (where negative indexing means 0).但是,此计算存在递归方面,因为Y[i,j] = X[i,j] + Y[i-1,j] + Y[i,j-1] - Y[i-1,j-1] (其中负索引表示 0)。

I was looking for "2d Python cumsum", and I've found that NumPy's cumsum merely flattens the array.我正在寻找“2d Python cumsum”,我发现 NumPy 的cumsum只是使数组变平。

My Questions:我的问题:

  1. Is there a standard Python function for computing Y efficiently?是否有用于有效计算Y的标准 Python function?
  2. If not, is the recursive idea above optimal?如果不是,上面的递归思想是最优的吗?

Thanks.谢谢。

A kernel splitting method can be applied here to solve this problem very efficiently with only two np.cumsum : one vertical and one horizontal (or the other way since this is symatric).在这里可以应用kernel 拆分方法来非常有效地解决这个问题,只需要两个np.cumsum :一个垂直和一个水平(或另一种方式,因为这是对称的)。

Here is an example:这是一个例子:

x = np.random.randint(0, 10, (4, 5))
print(x)
y = np.cumsum(np.cumsum(x, axis=0), axis=1)
print(y)

Here is the result:结果如下:

[[1 9 8 1 7]
 [0 6 8 2 3]
 [1 3 6 4 4]
 [0 8 1 2 9]]

[[ 1 10 18 19 26]
 [ 1 16 32 35 45]
 [ 2 20 42 49 63]
 [ 2 28 51 60 83]]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM