📜  Python中的 numpy.nancumprod()

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

Python中的 numpy.nancumprod()

当我们想要计算数组元素在给定轴上的累积乘积时,使用numpy.nancumprod()函数,将非数字 (NaN) 视为一个。当遇到 NaN 并且前面的 NaN 被替换为 1 时,累积乘积不会改变。为全 NaN 或空的切片返回一个。

代码#1:工作

# Python program explaining
# numpy.nancumprod() function
  
import numpy as geek
in_num = 10
  
print ("Input  number : ", in_num)
    
out_prod = geek.nancumprod(in_num) 
print ("cumulative product of input number : ", out_prod) 

输出 :

Input  number :  10
cumulative product of input number :  [10]


代码#2:

# Python program explaining
# numpy.nancumprod() function
  
import numpy as geek
  
in_arr = geek.array([[2, 2, 2], [2, 2, geek.nan]])
   
print ("Input array : ", in_arr) 
    
out_prod = geek.nancumprod(in_arr) 
print ("cumulative product of array elements: ", out_prod) 

输出 :

Input array :  [[  2.   2.   2.]
 [  2.   2.  nan]]
cumulative product of array elements:  [  2.   4.   8.  16.  32.  32.]


代码#3:

# Python program explaining
# numpy.nancumprod() function
  
import numpy as geek
  
in_arr = geek.array([[2, 2, 2], [2, 2, geek.nan]])
   
print ("Input array : ", in_arr) 
    
out_prod = geek.nancumprod(in_arr, axis = 1) 
print ("cumulative product of array elements taking axis 1: ", out_prod) 

输出 :

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