Here is a brief introduction numpy
to 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.array1
The 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 result1
results and result2
the results, we can see that, when specified axis=0
it is along y
for accumulating shaft, specified axis=1
along the time x
accumulated 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~