Return the cumulative product of array elements over axis 0 treating NaNs as one in Python

PythonNumpyServer Side ProgrammingProgramming

To return the cumulative product of array elements over a given axis treating NaNs as one, use the nancumprod() method. The cumulative product does not change when NaNs are encountered and leading NaNs are replaced by ones. Ones are returned for slices that are all-NaN or empty.

The method returns a new array holding the result is returned unless out is specified, in which case it is returned. Cumulative works like, 5, 5*10, 5*10*15, 5*10*15*20. The 1st parameter is the input array. The 2nd parameter is the Axis along which the cumulative product is computed. By default the input is flattened.

The 3rd parameter is the type of the returned array, as well as of the accumulator in which the elements are multiplied. If dtype is not specified, it defaults to the dtype of a, unless a has an integer dtype with a precision less than that of the default platform integer. In that case, the default platform integer is used instead. The 4th parameter is the alternative output array in which to place the result. It must have the same shape and buffer length as the expected output but the type of the resulting values will be cast if necessary.

Steps

At first, import the required libraries −

import numpy as np

Creating a numpy array using the array() method. We have added elements of int type with nan −

arr = np.array([[5, 10, 15], [20, np.nan, 30]])

Display the array −

print("Our Array...\n",arr)

Check the Dimensions −

print("\nDimensions of our Array...\n",arr.ndim)

Get the Datatype −

print("\nDatatype of our Array object...\n",arr.dtype)

To return the cumulative product of array elements over a given axis treating NaNs as one, use the nancumprod() method. The cumulative product does not change when NaNs are encountered and leading NaNs are replaced by ones. Ones are returned for slices that are all-NaN or empty. The method returns a new array holding the result is returned unless out is specified, in which case it is returned −

print("\nCumulative Product of array elements...\n",np.nancumprod(arr, axis = 0))

Example

import numpy as np

# Creating a numpy array using the array() method
# We have added elements of int type with nan
arr = np.array([[5, 10, 15], [20, np.nan, 30]])

# Display the array
print("Our Array...\n",arr)

# Check the Dimensions
print("\nDimensions of our Array...\n",arr.ndim)

# Get the Datatype
print("\nDatatype of our Array object...\n",arr.dtype)

# To return the cumulative product of array elements over a given axis treating NaNs as one, use the nancumprod() method
print("\nCumulative Product of array elements...\n",np.nancumprod(arr, axis = 0))

Output

Our Array...
[[ 5. 10. 15.]
[20. nan 30.]]

Dimensions of our Array...
2

Datatype of our Array object...
float64

Cumulative Product of array elements...
[[ 5. 10. 15.]
[100. 10. 450.]]
raja
Updated on 02-Mar-2022 07:27:00

Advertisements