Source: blog.csdn.net/yuansuo0516…

C umsum: Computes the summation of axial elements and returns an array of intermediate results

The point is that the return value is”An array of intermediate results

The following code runs successfully on python3.6!

Define an array of 2*2*3, so its shape is 2, 2, 3, and its indexes are 0,1,2

shape The index
2 0
2 1
3 2

Code:


  

    

     

    

    

     

      import numpy 
      as np
     

    

    

     

    

    

     

      arr  = np.array([[[
      1.2.3], [8.9.12]], [[1.2.4], [2.4.5]]]) 
      # 2 * 2 * 3
     

    

    

     

    

    

     

      print(arr.cumsum(
      0))
     

    

    

     

    

    

     

      print(arr.cumsum(
      1))
     

    

    

     

    

    

     

      print(arr.cumsum(
      2))
     

    

  
Copy the code

  
Copy the code
Output result:#cumsum(0)
      

     

     

      

     

     

      

       [[[ 1  2  3]
      

     

     

      

     

     

      

         [ 8  9 12]]
      

     

     

      

     

     

      
 
      

     

     

      

     

     

      

        [[ 2  4  7]
      

     

     

      

     

     

      

         [10 13 17]]]
      

     

     

      

     

     

      

       #cumsum(1)
      

     

     

      

     

     

      

       [[[ 1  2  3]
      

     

     

      

     

     

      

         [ 9 11 15]]
      

     

     

      

     

     

      
 
      

     

     

      

     

     

      

        [[ 1  2  4]
      

     

     

      

     

     

      

         [ 3  6  9]]]
      

     

     

      

     

     

      

       #cumsum(2)
      

     

     

      

     

     

      

       [[[ 1  3  6]
      

     

     

      

     

     

      

         [ 8 17 29]]
      

     

     

      

     

     

      
 
      

     

     

      

     

     

      

        [[ 1  3  7]
      

     

     

      

     

     

      

         [ 2  6 11]]]
      

     

   
Copy the code
Copy the code

Copy the code

Note:

Arr is a 2*2*3 3d matrix with index values 0,1,2

Cumsum (0) : cumsum on axis 0: take the outermost element as the unit and start with [[1,2,3],[8,9,12]] to realize the corresponding accumulation of the following elements

Cumsum (1) : to realize the accumulation on axis 1: To realize the corresponding accumulation of the following elements starting from [1,2,3] with the middle array elements as the unit

Cumsum (2) : realize the accumulation on the 2 axis: start with the innermost element as the accumulation unit, i.e., 1, and realize the accumulation of the following elements

Four – dimensional array implementation

Let’s look at a four-dimensional array 2*2*2*4 with indexes 0,1,2,3

Code:


  

    

     

    

    

     

      import numpy 
      as np
     

    

    

     

    

    

     

      arr  = np.arange(
      32).reshape((
      2.2.2.4))
     

    

    

     

    

    

     

      print(arr)
     

    

    

     

    

    

     

      print(arr.cumsum(
      0))
     

    

    

     

    

    

     

      print(arr.cumsum(
      1))
     

    

    

     

    

    

     

      print(arr.cumsum(
      2))
     

    

    

     

    

    

     

      print(arr.cumsum(
      3))
     

    

  
Copy the code

   

     

      

     

     

      

       arr:
      

     

     

      

     

     

      

       [[[[ 
       0  
       1  
       2  
       3]
      

     

     

      

     

     

      

          [ 
       4  
       5  
       6  
       7]]
      

     

     

      

     

     

      
 
      

     

     

      

     

     

      

         [[ 
       8  
       9 
       10 
       11]
      

     

     

      

     

     

      

          [
       12 
       13 
       14 
       15[[[]]]16 
       17 
       18 
       19]
      

     

     

      

     

     

      

          [
       20 
       21 
       22 
       23]]
      

     

     

      

     

     

      
 
      

     

     

      

     

     

      

         [[
       24 
       25 
       26 
       27]
      

     

     

      

     

     

      

          [
       28 
       29 
       30 
       31]]]]
      

     

   
Copy the code
Arr is a2*2*2*4A four dimensional matrix with an index of0.1.2.3
   
Copy the code

Cumsum (0) : implement the sum on the 0 axis namely: in the outermost element of the array namely

[[0 12 3] [4 5 6 7]] [[8 9 10 11] [12 13 14 15]]Copy the code

with


   

     

      

     

     

      

       [[[
       16 
       17 
       18 
       19]
      

     

     

      

     

     

      

          [
       20 
       21 
       22 
       23]]
      

     

     

      

     

     

      
 
      

     

     

      

     

     

      

         [[
       24 
       25 
       26 
       27]
      

     

     

      

     

     

      

          [
       28 
       29 
       30 
       31]]]]
      

     

   
Copy the code

Add the elements in the corresponding position \

Results:


   

     

      

     

     

      

       [[[[ 
       0  
       1  
       2  
       3]
      

     

     

      

     

     

      

          [ 
       4  
       5  
       6  
       7]]
      

     

     

      

     

     

      
 
      

     

     

      

     

     

      

         [[ 
       8  
       9 
       10 
       11]
      

     

     

      

     

     

      

          [
       12 
       13 
       14 
       15[[[]]]16 
       18 
       20 
       22]
      

     

     

      

     

     

      

          [
       24 
       26 
       28 
       30]]
      

     

     

      

     

     

      
 
      

     

     

      

     

     

      

         [[
       32 
       34 
       36 
       38]
      

     

     

      

     

     

      

          [
       40 
       42 
       44 
       46]]]]
      

     

   
Copy the code

Cumsum (1) : to realize the accumulation on axis 1, namely:

[0 1 2 3] [4 5 6 7]]Copy the code

add


   

     

      

     

     

      

         [[ 
       8  
       9 
       10 
       11]
      

     

     

      

     

     

      

          [
       12 
       13 
       14 
       15]]
      

     

   
Copy the code

add

[[16 17 18 19] [20 21 22 23]Copy the code

add


   

     

      

     

     

      

       [[
       24 
       25 
       26 
       27]
      

     

     

      

     

     

      

          [
       28 
       29 
       30 
       31]]
      

     

   
Copy the code

The intermediate results of the accumulative process are recorded in the array

So, as a result:


   

     

      

     

     

      

       [[[[ 
       0  
       1  
       2  
       3]
      

     

     

      

     

     

      

          [ 
       4  
       5  
       6  
       7]]
      

     

     

      

     

     

      
 
      

     

     

      

     

     

      

         [[ 
       8 
       10 
       12 
       14]          
      

     

     

      

     

     

      

          [
       16 
       18 
       20 
       22[[[]]]16 
       17 
       18 
       19]
      

     

     

      

     

     

      

          [
       20 
       21 
       22 
       23]]
      

     

     

      

     

     

      
 
      

     

     

      

     

     

      

         [[
       40 
       42 
       44 
       46]
      

     

     

      

     

     

      

          [
       48 
       50 
       52 
       54]]]]
      

     

   
Copy the code

Cumsum (2) starts from [0, 1, 2, 3] and records the intermediate result

Cumsum (3) = 0 cumsum(3) = 0 cumsum(3) = 0

Original explanation, prohibit copying, support reprint! Indicate the source **\


\