Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 二维数组的累加和_Python_Python 3.x_Numpy_Numpy Ndarray_Numpy Einsum - Fatal编程技术网

Python 二维数组的累加和

Python 二维数组的累加和,python,python-3.x,numpy,numpy-ndarray,numpy-einsum,Python,Python 3.x,Numpy,Numpy Ndarray,Numpy Einsum,假设我有一个2D numpy数组,如下所示 dat = np.array([[1,2],[3,4],[5,6],[7,8]) 我想得到一个新数组,其中每一行等于前几行的总和,如下所示 first row: [1,2] second row: [1,2] + [3,4] = [4,6] third row: [4,6] + [5,6] = [9,12] forth row: [9,12] + [7,8] = [16,20] 所以数组应该是这样的 dat = np.array([[1,2],[4

假设我有一个2D numpy数组,如下所示

dat = np.array([[1,2],[3,4],[5,6],[7,8])
我想得到一个新数组,其中每一行等于前几行的总和,如下所示

first row: [1,2]
second row: [1,2] + [3,4] = [4,6]
third row: [4,6] + [5,6] = [9,12]
forth row: [9,12] + [7,8] = [16,20]
所以数组应该是这样的

dat = np.array([[1,2],[4,6],[9,12],[16,20])    
这是您正在寻找的:

dat = np.array([[1,2],[3,4],[5,6],[7,8]])
result = np.cumsum(dat, axis=0)