NumPy.nanprod() Function

NumPy.nanprod() Function
NumPy.nanprod() Function

Arrays are some of the most common and fundamental objects when working with NumPy. Therefore, learning to perform various actions on array objects is hugely significant to your productivity.

This tutorial resolves to guide you on using NumPy.nanprod() method, starting with the function syntax, accepted parameters, return value, and practical examples.

Introduction to the NumPy nanprod() Function

When working with NumPy, you will encounter an array that contains missing values. In most cases, NumPy and other Python packages represent missing values as NaN or Not A Number.

The nanprod() function in NumPy enables us to determine the product of each element in a given array, even in an array containing NaN values.

Function Syntax

Provided below is the syntax of the nanprod() function in NumPy:

numpy.nanprod(a, **axis**=None, **dtype**=None, **out**=None, **keepdims**=<no value>, **initial**=<no value>, **where**=<no value>)

Function Parameters

From the syntax above, we can see the function accepts the following arguments:

  • a – represents the input array whose elements' products are to be computed. It is a required parameter; the function will return an error if its value is missing.
  • axis – This parameter (optional) defines along which axis the product of the array is computed. With the axis set to 0, the function returns the product of the elements along the columns. Otherwise, if the axis is set to 1, the product is calculated for the elements along the rows. By default, the axis value is set to None, which returns the product of all elements in the array.
  • dtype – the dtype parameter enables you to specify the data type of the array to be returned.
  • out – the out parameter is an optional value that defines an alternative array to place the resulting array. The alternative array must have a similar shape as the expected output.
  • keepdims – If this parameter's value is set to true, the reduced axes are left in the result as the dimensions with size one.
  • initial – states the starting value for the product.
  • where – sets the elements to include in the product computation.

Function Return Value

The function returns the product of each element in the given array over the specified axis. The array returned is the same shape as the input array defined by the a parameter.
The function treats all NaN values as 1.

Examples of Using the NumPy.nanprod() Function

The example below provides an example code on how to use the nanprod() function with a 1-dimensional array:

import numpy as np
arr = np.array([15, np.nan, 30])
product = np.nanprod(arr)
print("original array: ", arr)
print("resulting array: ", product)

Output

original array: [15. nan 30.]
resulting array: 450.0

We declare a new 1-D array with three elements in the example above. One element is a NaN value as provided by np.nan.

Calculating the product of the array forces the value of NaN to be treated as 1, leading to the computation:
$$
15 * 1 * 30 = 450.0
$$

Using the nanprod() function with a 2-Dimensional Array

The nanprod() function supports operation on a 2-D array, as demonstrated in the code below:

import numpy as np
arr = np.array([[15, np.nan, 30],
       [10, np.nan, np.nan]])
product = np.nanprod(arr)
print("original array: ", arr)
print("resulting array: ", product)

After the product operation:

original array: [[15. nan 30.]
 [10. nan nan]]
 resulting array: 4500.0

The function will treat all NaN values as 1, making the resulting expression:
$$
15 * 1 * 30 * 10 * 1 * 1 = 4500.0
$$

Using the nanprod() function along the zero axis (column-wise)

The example code below illustrates how to use the nanprod() function along the 0 axis. It will only perform the product along the column values.

import numpy as np
arr = np.array([[15, np.nan, 30],
       [10, np.nan, np.nan]])
product = np.nanprod(arr, axis=0)
print("original array: ", arr)
print("resulting array: ", product)

Output from the above operation:

original array: [[15. nan 30.]
[10. nan nan]]
resulting array: [150.  1. 30.]

In this case, since we are performing the product along the column values, the resulting expression:
$$
Column[0] \rightarrow 15 * 10 = 150\
Column[1] \rightarrow 1 * 1 = 1\
Column[2] \rightarrow 30 * 1 = 10\
$$

Using the nanprod() function along the one axis (row-wise)

We can also perform row-wise product computation as demonstrated in the code below:

import numpy as np
arr = np.array([[15, np.nan, 30],
       [10, np.nan, np.nan]])
product = np.nanprod(arr, axis=1)
print("original array: ", arr)
print("resulting array: ", product)

Result:

original array: [[15. nan 30.]
[10. nan nan]]
resulting array: [450. 10.]

Similarly, the resulting expression along the one axis:
$$
Row[0] \rightarrow 15 * 1 * 30 = 450\
Row[1] \rightarrow 10 * 1 * 1 = 10
$$

Using the nanprod() Function on NaN array

The example below calls the nanprod() function on an array containing all NaN values.

import numpy as np
 arr = np.array([[np.nan, np.nan, np.nan],
        [np.nan, np.nan, np.nan]])
 product = np.nanprod(arr, axis=None)
 print("original array: ", arr)
 print("resulting array: ", product)

Output:

original array: [[nan nan nan]
[nan nan nan]]
resulting array: 1.0

As you can guess, passing an array of NaN values to the nanprod function returns 1.

Conclusion

This tutorial discussed how to use NumPy.nanprod() function to calculate the product of elements in a given input array while treating NaN values as 1.

If you enjoy our content, please consider buying us a coffee to support our work:

Great! Next, complete checkout for full access to GeekBits.
Welcome back! You've successfully signed in.
You've successfully subscribed to GeekBits.
Success! Your account is fully activated, you now have access to all content.
Success! Your billing info has been updated.
Your billing was not updated.