Python numpy module universal functon accumulate() function usage

Here is a brief introduction numpyto accumulate()the usage of the functions in the module .
code show as below:

# -*- coding: utf-8 -*-
import numpy as np


class Debug:
    def __init__(self):
        self.array1 = np.array([1, 2, 3, 4])
        self.array3 = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])

    def mainProgram(self):
        result = np.add.accumulate(self.array1)
        print("The value of result is: ")
        print(result)
        result1 = np.add.accumulate(self.array3, axis=0)
        print("The value of result1 is: ")
        print(result1)
        result2 = np.add.accumulate(self.array3, axis=1)
        print("The value of result2 is: ")
        print(result2)


if __name__ == '__main__':
    main = Debug()
    main.mainProgram()
"""
The value of result is: 
[ 1  3  6 10]
The value of result1 is: 
[[ 1  2  3  4]
 [ 6  8 10 12]]
The value of result2 is: 
[[ 1  3  6 10]
 [ 5 11 18 26]]
"""

We can see that a accumulate()function is an accumulated operation. When it is applied to a add()function, it is an accumulation operation. self.array1The value is [1, 2, 3, 4]obtained after accumulation [ 1 3 6 10]. We can see that the second value 3=1+2, the third Value 6=1+2+3, the fourth value 10=1+2+3+4. From the result1results and result2the results, we can see that, when specified axis=0it is along yfor accumulating shaft, specified axis=1along the time xaccumulated axis, why particular, reference may axes problem np.repeat () a .

If you find it useful, please raise your hand to give a like and let me recommend it for more people to see~

Guess you like

Origin blog.csdn.net/u011699626/article/details/109014632