[英]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:我的问题:
Y
efficiently?是否有用于有效计算Y
的标准 Python function?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.