NumPy cumprod – A Complete Guide

NumPy Cumprod Cover Image

Hello and welcome to this tutorial on Numpy cumprod. In this tutorial, we will be learning about the NumPy cumprod() method and also seeing a lot of examples regarding the same. So let us begin!

Also read: NumPy cumsum – A Complete Guide


What is NumPy cumprod?

The cumulative product is a sequence of partial products of a given sequence. If {a, b, c, d, e, f,…..} is a sequence then its cumulative product is represented as {a, a*b, a*b*c, a*b*c*d,….}.

The cumprod() method in NumPy returns the cumulative product of the elements of the input array along the specified axis.  It can be the cumulative product of the flattened array, the cumulative product of the array elements along the rows or the cumulative product of the array elements along the columns. 

We will see the examples for each of these in the upcoming section of this tutorial.


Syntax of NumPy cumprod

numpy.cumprod(a, axis=None, dtype=None, out=None)
ParameterDescriptionRequired/Optional
aInput array.Required
axisAxis along which the cumulative product of the array is to be calculated. It can be axis=0 i.e. along columns or axis=1 i.e. along rows or axis=None which implies that the cumulative product of the flattened array is to be returned.Optional
dtype (data type)The data type of the array to be returned.Optional
outAn alternative output array in which to place the result. It must have the same shape and length as the expected output.Optional

Returns:
A new array that contains the output. If out is mentioned, then a reference to it is returned.


Examples of the numpy.cumprod method

Let’s now get right into using the numpy.cumprod method so we can understand the outputs.

The cumulative product of a single element

import numpy as np

a = 5
ans = np.cumprod(a)

print("a =", a)
print("Cumulative product =", ans)

Output:

a = 5
Cumulative product = [5]

The cumulative product of an empty array

import numpy as np

a = []
ans = np.cumprod(a)

print("a =", a)
print("Cumulative product =", ans)

Output:

a = []
Cumulative product = []

The cumulative product of a 1-dimensional array

import numpy as np

a = [2, 10, 3 ,6]
ans = np.cumprod(a)

print("a =", a)
print("Cumulative product of the array =", ans)

Output:

a = [2, 10, 3, 6]
Cumulative product of the array = [  2  20  60 360]

Here, the cumulative product is calculated as 2, 2*10, 2*10*3, 2*10*3*6 i.e. 2, 20, 60, 360.


The cumulative product of a 2-dimensional array

import numpy as np

a = [[8, 3], [5, 2]]
ans = np.cumprod(a)

print("a =", a)
print("Cumulative product of the array =", ans)

Output:

a = [[8, 3], [5, 2]]
Cumulative product of the array = [  8  24 120 240]

In the case of a 2-dimensional array, when no axis is mentioned, the array is first flattened and then its cumulative product is calculated.
In the above example, the array is first flattened as [8, 3, 5, 2] i.e. row-wise and then its cumulative product is calculated as [8, 8*3, 8*3*5, 8*3*5*2] which results in the array [8, 24, 120, 240] which is returned by the function.


Return Numpy.cumprod() of the array as float data type

This is the same as the above examples except that here the returned values are of float data type.

import numpy as np

a = [2, 10, 3, 6]
ans = np.cumprod(a, dtype=float)

print("a =", a)
print("Cumulative product of the array =", ans)

Output:

a = [2, 10, 3, 6]
Cumulative product of the array = [  2.  20.  60. 360.]

Cumulative product along the axis

axis = 0

import numpy as np

a = [[3, 2, 1], [4, 5, 6]]
# cumulative product along axis=0
ans = np.cumprod(a, axis=0)

print("a =\n", a)
print("Cumulative product of the array =\n", ans)

Output:

a =
 [[3, 2, 1], [4, 5, 6]]
Cumulative product of the array =
 [[ 3  2  1]
 [12 10  6]]

Here, the first row is as it is and the second row contains the cumulative product calculated as 3*4, 2*5, 1*6 resulting in 12, 10 and 6.

axis = 1

import numpy as np

a = [[3, 2, 1], [4, 5, 6]]
# cumulative product along axis=1
ans = np.cumprod(a, axis=1)

print("a =\n", a)
print("Cumulative product of the array =\n", ans)

Output:

a =
 [[3, 2, 1], [4, 5, 6]]
Cumulative product of the array =
 [[  3   6   6]
 [  4  20 120]]

Here, the first column is as it is and the second column contains the cumulative product calculated as 3*2, 4*5 resulting in 6, 20 and the third column has the cumulative product of 3*2*1, 4*5*6 i.e. 6 and 120.


Summary

That’s all! In this tutorial, we learned about the Numpy cumprod method and practiced different types of examples using the same.


Reference