📜  Python中的 numpy.nanprod()

📅  最后修改于: 2022-05-13 01:54:38.618000             🧑  作者: Mango

Python中的 numpy.nanprod()

当我们想要计算数组元素在给定轴上的乘积时,使用numpy.nanprod()函数,将NaNs视为一个。对于全为 NaN 或空的切片返回一个。

代码#1:工作

# 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) 

输出 :

Input  number :  10
product of array element :  10


代码#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) 

输出 :

Input array :  [[  2.   2.   2.]
 [  2.   2.  nan]]
product of array elements:  32.0


代码#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) 

输出 :

Input array :  [[  2.   2.   2.]
 [  2.   2.  nan]]
product of array elements taking axis 1:  [ 8.  4.]