official document
cumprod() Cumulative Multiplication
(axis=None, skipna=True, *args, **kwargs) #Implementation Functions:Return cumulative product over a DataFrame or Series axis. #Implementation Functions:Returns a DataFrame or Series of the same size containing the cumulative product. #return:scalar or Series
cumsum() cumulative concatenation
official document
(axis=None, skipna=None, level=None, numeric_only=None, min_count=0, **kwargs) # Implementation Functions:Return the product of the values for the requested axis. # return:scalar or Series
Pros didn't see the point, because the .prod() used in the regular case is not a function under pandas, but under numpy.
official document
(a, axis=None, dtype=None, out=None, keepdims=<class numpy._globals._NoValue>) # Implementation Functions:Return the product of array elements over a given axis. # return:product_along_axis : ndarray
Returns the product of the array elements on the given axis.
Unlike cumprod, which calculates the current cumulative multiplication of all previous data, and is more of a list; prod returns the final value on a given axis.
ADDED: [python for beginners] Easy-to-understand diagrams: and What are functions really doing?
I am a python white, recently finished the basics of python, after reading the "use of python for data analysis" book, the book cumsum function with a stroke left the white "confused under the tree you and I", of course, is my own problem is not a problem of the book, after the drawing of understanding Gradually understand the function in the end in the dry.
1. - Concept of the axis
First of all, before learning about the cumsum function we should understand what an axis is, as illustrated by the following code:
arr=(1,17,1).reshape((2,2,4)) arr
array([[[ 1, 2, 3, 4], [ 5, 6, 7, 8]], [[ 9, 10, 11, 12], [13, 14, 15, 16]]])
In fact, the axes (axis) of the array are the dimensions of the array, and the code above generates a 22An array of 4, so
1. The 0-axis of this array is 2 ,axis=0
2. 1 axis of this array is 2 ,axis=1
3. The 2-axis of this array is 4 ,axis=2
The array is shown in the figure (blue, orange, yellow, green are 2-axis, orange and green on the "2-axis" drawing forgot to label):
I'd also like to add that the red numbers are only black because it's inconvenient for me to draw on my iPad, so just ignore them!
1.2cumsum(axis=0)
Role of cumsum:Calculates the axial element sum and returns an array of intermediate results.
The part of this concept that I think people have a hard time understanding would be the followingaxial elementCumulative.
First of all, by the previous understanding of the concept of axis we can know that
axis=0 represents the outermost dimension, which is the 0-axis (this may not be correct, mainly to match the picture in the previous section), so it is the 0-axis of the cumulative calculation, we have previously used the array as an example (the red dotted line indicates that according to the 0-axis of the cumulative):
step1:
Accumulation along the 0-axis
step2:
Accumulate [1,2,3,4] and [9,10,11,12], add [5,6,7,8] and [13,14,15,16]
Code:
arr=([[[ 1, 2, 3, 4], [ 5, 6, 7, 8]], [[ 9, 10, 11, 12], [13, 14, 15, 16]]]) (axis=0)
Results for:
array([[[ 1, 2, 3, 4], [ 5, 6, 7, 8]], [[10, 12, 14, 16], [18, 20, 22, 24]]])
1.3cumsum(axis=1)
Here we still take the previous example of the array as an example, along the 1 axis to accumulate (that is, 2 * 2 * 4 in the second 2), here in order to facilitate the explanation of the array I will be placed in a different position, does not affect the Ha ~ ~
step1:
The red dotted line means that we should now accumulate along axis 1!
step2:
Since we are accumulating along the 1-axis, shouldn't we be accumulating inside the 1-axis?
So it should be [1,2,3,4] and [5,6,7,8] to accumulate, [9,10,11,12] and [13,14,15,16] to accumulate
Code Results:
(axis=1) #Running results array([[[ 1, 2, 3, 4], [ 6, 8, 10, 12]], [[ 9, 10, 11, 12], [22, 24, 26, 28]]])
1.4cumsum(axis=2)
Have been talking about along the axis 2 for accumulation, nonsense will not say more directly put the map, we look at whether there is no right to do it!
step1:
The old rule: the red dotted line indicates accumulation along the 2-axis, so it should be 1, 2, 3, 4 for accumulation, 5, 6, 7, 8 for accumulation and so on
step2
We are based onblue (color)This item is an example:
First item: 1 Second item: 1+2=3 Third item: 1+2+3=6 Fourth item: 1+2+3+4=10
Code Results:
(axis=2) #Running results array([[[ 1, 3, 6, 10], [ 5, 11, 18, 26]], [[ 9, 19, 30, 42], [13, 27, 42, 58]]])
At this point I believe you should be able to figure out what the cumprod function is doing! In this article, because of the need to explain the combination of pictures, so some sentences are not appropriate ~ I hope that this article can make you understand the cumsum function in the end what ah ~