求数组的所有元素的累计和,可通过参数axis指定求某个轴向的统计值。
>>> a = np.array([[1, 2, 3], [4, 5, 6]])
>>> a
array([[1, 2, 3],
[4, 5, 6]])
>>> a.cumsum()
array([ 1, 3, 6, 10, 15, 21], dtype=int32)
>>> arr
array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])
>>> np.cumsum(arr, axis=0)
array([[ 0, 1, 2],
[ 3, 5, 7],
[ 9, 12, 15]], dtype=int32)
>>>
>>> arr
array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])
>>> arr.cumsum(axis=1)
array([[ 0, 1, 3],
[ 3, 7, 12],
[ 6, 13, 21]], dtype=int32)