numpy.nanprod() in Python
numpy.nanprod()
function is used when we want to compute the product of array elements over a given axis treating NaNs
as ones. One is returned for slices that are all-NaN or empty.
Syntax : numpy.nanprod(arr, axis=None, dtype=None, out=None, keepdims=’class numpy._globals._NoValue’).
Parameters :
arr : [array_like] Array containing numbers whose sum is desired. If arr is not an array, a conversion is attempted.
axis : Axis along which the product is computed. The default is to compute the product of the flattened array.
dtype : The type of the returned array and of the accumulator in which the elements are summed. By default, the dtype of arr is used.
out : [ndarray, optional] A location into which the result is stored.
-> If provided, it must have a shape that the inputs broadcast to.
-> If not provided or None, a freshly-allocated array is returned.
keepdims : If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the original arr.Return : A new array holding the result is returned unless out is specified, in which case it is returned.
Code #1 : Working
# Python program explaining # numpy.nanprod() function import numpy as geek in_num = 10 print ( "Input number : " , in_num) out_prod = geek.nanprod(in_num) print ( "product of array element : " , out_prod) |
Output :
Input number : 10 product of array element : 10
Code #2 :
# Python program explaining # numpy.nanprod function import numpy as geek in_arr = geek.array([[ 2 , 2 , 2 ], [ 2 , 2 , geek.nan]]) print ( "Input array : " , in_arr) out_prod = geek.nanprod(in_arr) print ( "product of array elements: " , out_prod) |
Output :
Input array : [[ 2. 2. 2.] [ 2. 2. nan]] product of array elements: 32.0
Code #3 :
# Python program explaining # numpy.nanprod function import numpy as geek in_arr = geek.array([[ 2 , 2 , 2 ], [ 2 , 2 , geek.nan]]) print ( "Input array : " , in_arr) out_prod = geek.nanprod(in_arr, axis = 1 ) print ( "product of array elements taking axis 1: " , out_prod) |
Output :
Input array : [[ 2. 2. 2.] [ 2. 2. nan]] product of array elements taking axis 1: [ 8. 4.]
Recommended Posts:
- Important differences between Python 2.x and Python 3.x with examples
- Python | Set 4 (Dictionary, Keywords in Python)
- Python | Sort Python Dictionaries by Key or Value
- gcd() in Python
- zip() in Python
- set add() in python
- bin() in Python
- chr() in Python
- Any & All in Python
- max() and min() in Python
- SHA in Python
- Python | a += b is not always a = a + b
- abs() in Python
- try and except in Python
- SQL using Python | Set 1
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.