.nanprod()

byteSolver49786's avatar
Published Oct 19, 2024
Contribute to Docs

In NumPy, the .nanprod() function calculates the product of array elements over a specified axis, treating NaN (Not a Number) values as 1. This is useful when working with arrays that contain missing or undefined values represented as NaN.

Syntax

numpy.nanprod(a, axis=None, dtype=None, out=None, keepdims=<no value>)
  • a: An array for which the product is calculated.
  • axis: An optional parameter used to specify the axis along which the product is computed. If not specified, the product of all elements in the array is calculated.
  • dtype : An optional parameter used to specify the data type of the result. If not specified, it defaults to the data type of the input array, or the default platform integer if the input contains integers.
  • out: An optional parameter specifying the array to store the result. If not provided, a new array is created.
  • keepdims: An optional parameter. If set to True, the reduced axes are retained in the result as dimensions with size one, allowing the result to broadcast correctly.

Example 1

This example demonstrates using .nanprod() function to calculate the product of an array containing NaN values:

import numpy as np
# Creating a numpy array with NaN values
arr = np.array([1, 2, np.nan, 4, 5])
# Computing the product of the array, treating NaN as 1
result = np.nanprod(arr)
print(result)

The above example code results in the following output:

40.0

Example 2

This example shows how to use .nanprod() function along a specific axis:

import numpy as np
# Creating a 2D numpy array with NaN values
arr = np.array([[1, 2, np.nan], [4, 5, 6]])
# Computing the product along axis 0 (columns)
result = np.nanprod(arr, axis=0)
print(result)

The above example code results in the following output:

[4. 10. 6.]

Codebyte Example

In this codebyte example, the .nanprod() method computes the product of the elements in the array along both axes, demonstrating the use of the keepdims parameter:

Code
Output
Loading...

All contributors

Contribute to Docs

Learn Python:NumPy on Codecademy