The numpy.nanprod()
function in NumPy returns the product of each element of an input array over a given axis in a way that Not a Number
(NaNs
) are treated as ones.
numpy.nanprod(a, axis=None, dtype=None, out=None)
The numpy.nanprod()
function takes the following parameter values:
a
(required): This is the input array that contains the numbers to be computed.axis
(optional): This is the axis along which the product is determined.dtype
(optional): This is the data type of the output array.out
(optional): This is the alternate array where the result is placed.The numpy.nanprod()
function returns an output array holding the result.
import numpy as np# Creating an arrayx = np.array([1, 2, np.nan, 2, np.nan])# Implementing the nanprod() functionmyarray = np.nanprod(x, axis=0)print(x)print(myarray)
numpy
module.x
using the array()
method.np.nanprod()
function on the array. The result is assigned to a variable, myarray
.x
.myarray
.Note: We get the output
4
product of the elements as follows: 1×2×np.nan×2×np.nan = 1×2×1×2×1 = 4. This is because theNans
values are treated as1
.