SoFunction
Updated on 2024-11-16

Specific use of the python cumsum function

The function's function is to return the cumulative sum on the given axis The prototype of the function is as follows: see detailsdoc

 (a, axis=None, dtype=None, out=None)
    Return the cumulative sum of the elements along a given axis.

The official document is no detailed explanation, I did the test myself to write down the understanding.

1. For a one-dimensional input a (can be a list, can be array, assuming that a = [1, 2, 3, 4, 5, 6, 7] , that is, the current column before the sum is added to the current column, as follows:

>>>import numpy as np
>>> a=[1,2,3,4,5,6,7]
>>> (a)
array([ 1,  3,  6, 10, 15, 21, 28, 36, 45, 55, 75, 105])

2. For two-dimensional input a, axis = 0 (the first line does not move, the first line will be added to the other lines); axis = 1 (into the innermost, into the column processing. The first column does not move, the first column will be added to the other columns), as follows:

>>>import numpy as np
>>> c=[[1,2,3],[4,5,6],[7,8,9]]
>>> (c,axis=0)
array([[ 1, 2, 3],
    [ 5, 7, 9],
    [12, 15, 18]])
>>> (c,axis=1)
array([[ 1, 3, 6],
    [ 4, 9, 15],
    [ 7, 15, 24]])

3. For three-dimensional input a, axis = 0 (the first line does not move, the first line will be added to the other lines); axis = 1 (into the second layer, each of the second layer of the first line does not move, added to the other lines); axis = 2 (into the innermost layer, into the processing of columns. Column 1 does not move and is added to the other columns), noting that the dimensions are numbered 0-2 from the outside to the inside, as follows:

>>>import numpy as np
>>> a
[[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 20, 30]]]
>>> (a,axis=0)
array([[[ 1, 2, 3],
    [ 4, 5, 6]],
 
    [[ 8, 10, 12],
    [14, 25, 36]]])
>>> (a,axis=1)
array([[[ 1, 2, 3],
    [ 5, 7, 9]],
 
    [[ 7, 8, 9],
    [17, 28, 39]]])
>>> (a,axis=2)
array([[[ 1, 3, 6],
    [ 4, 9, 15]],
 
    [[ 7, 15, 24],
    [10, 30, 60]]])

a is a 2 x 2 x 3 list, which is a bit tricky to explain, so let's start by styling a differently

[ //axis=0
       [//axis=1, also line 1 for axis=0, note the entire contents of the parentheses.
          [1, 2, 3], //axis=2, also row 1 for axis=1.
          [4, 5, 6] //axis=2
       ],
       [//axis=1, also line 2 for axis=0.
          [7, 8, 9], //axis=2, also row 1 for axis=1.
          [10, 20, 30] //axis=2
       ],
   ]

axis=0 means, dimension 1, contains 2 rows, green bracketed list, row 1 (i.e. list consisting of 1,2,3,4,5,6) does not move, accumulates to the second row, note that it is cumulative
axis=1 means that in dimension 2, the first row in each green bracket remains unchanged, i.e., [1,2,3] and [7,8,9] do not move and are added to the same sibling rows (rows belonging to the same bracket).
axis=2 indicates that the 3rd dimension, which is also the innermost level, is transformed into a columnar process, where the columns where the purple numbers are located do not move and are accumulated on the other columns

For higher dimensions, you can refer to the 3 dimensions to understand them by peeling them off from the outside to the inside

This is the whole content of this article.