NumPy nancumprod – A Complete Guide

NumPy Nancumprod Cover Image

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

Recommended Read: NumPy cumprod – A Complete Guide, NumPy nanprod – A Complete Guide


What is NumPy nancumprod?

In Python, NaN denotes Not a Number. If we have an array that contains some NaN values and want to find its cumulative product, we can use the nancumprod() method from NumPy. 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 nancumprod() method in NumPy is a function that returns the cumulative product of the array elements calculated by treating the NaN values in the array as equal to 1. 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 nancumprod

numpy.nancumprod(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 or axis=1 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.nancumprod() method

Let us now see how to use this function with the help of some examples.

The cumulative product of a single element

import numpy as np

a = 10
ans = np.nancumprod(a)
print("a =", a)
print("Cumulative product =", ans)

Output:

a = 10
Cumulative product = [10]

The cumulative product of a 1-dimensional array containing NaNs

import numpy as np

a = [5, np.nan, 10, np.nan, np.nan, 2]
ans = np.nancumprod(a)
print("a =", a)
print("Cumulative product =", ans)

Output:

a = [5, nan, 10, nan, nan, 2]
Cumulative product = [  5.   5.  50.  50.  50. 100.]

In the above code, the array contains 3 NaN values.

While calculating the product, the nancumprod() function treats that NaN value as 1 and calculates the cumulative product as 5, 5*, 5*1*10, 5*1*10*1, 5*1*10*1*1, 5*1*10*1*1*2 which results in 5, 5, 50, 50, 50, 100.


The cumulative product of a 2-dimensional array containing NaNs

import numpy as np

a = [[3, np.nan, 6], [8, np.nan, np.nan]]
ans = np.nancumprod(a)
print("a =", a)
print("Cumulative product =", ans)

Output:

a = [[3, nan, 6], [8, nan, nan]]
Cumulative product = [  3.   3.  18. 144. 144. 144.]

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 by treating NaNs as 1.

In the above example, the array is first flattened as [3, np.nan, 6, 8, np.nan, np.nan] i.e. row-wise and then its cumulative product is calculated as [3, 3*1, 3*1*6, 3*1*6*8, 3*1*6*8,*1, 3*1*6*8,*1*1] which results in the array [3, 3, 18, 144, 144, 144] which is returned by the function.


Cumulative product along the axis treating NaN as 1

axis = 0

import numpy as np

a = [[5, 2, np.nan], [10, np.nan, 3]]
# cumulative product along axis=0
ans = np.nancumprod(a, axis=0)
print("a =\n", a)
print("Cumulative product =\n", ans)

Output:

a =
 [[5, 2, nan], [10, nan, 3]]
Cumulative product =
 [[ 5.  2.  1.]
 [50.  2.  3.]]

Treating NaN as 1, the first row is as it is. The second row contains cumulative products calculated as 5*10, 2*1, 1*3 i.e. 50, 2, and 3. That is, the cumulative product is calculated column-wise and stored in the form of a row.

axis = 1

import numpy as np

a = [[5, 2, np.nan], [10, np.nan, 3]]
# cumulative product along axis=1
ans = np.nancumprod(a, axis=1)
print("a =\n", a)
print("Cumulative product =\n", ans)

Output:

a =
 [[5, 2, nan], [10, nan, 3]]
Cumulative product =
 [[ 5. 10. 10.]
 [10. 10. 30.]]

Here, the first column is as it is and the second column contains the cumulative product calculated as 5*2, 10*1 resulting in 10, 10 and the third column has the cumulative product of 5*2*1, 10*1*3 i.e. 10 and 30. That is, the cumulative product is calculated row-wise and stored in the form of a column.


Summary

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


Reference