📜  Python中的 numpy.nancumsum()

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

Python中的 numpy.nancumsum()

numpy.nancumsum()函数用于计算给定轴上数组元素的累积和,将非数字 (NaN) 视为零。
当遇到 NaN 并且前导 NaN 被零替换时,累积和不会改变。对于全 NaN 或空的切片,将返回零。

代码#1:工作

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

输出 :

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


代码#2:

# Python program explaining
# numpy.nancumsum() function
  
import numpy as geek
  
in_arr = geek.array([[2, 4, 6], [1, 3, geek.nan]])
   
print ("Input array : ", in_arr) 
    
out_sum = geek.nancumsum(in_arr) 
print ("cumulative sum of array elements: ", out_sum) 

输出 :

Input array :  [[  2.   4.   6.]
 [  1.   3.  nan]]
cumulative sum of array elements:  [  2.   6.  12.  13.  16.  16.]


代码#3:

# Python program explaining
# numpy.nancumsum() function
  
import numpy as geek
  
in_arr = geek.array([[2, 4, 6], [1, 3, geek.nan]])
   
print ("Input array : ", in_arr) 
    
out_sum = geek.nancumsum(in_arr, axis = 0) 
print ("cumulative sum of array elements taking axis 0: ", out_sum) 

输出 :

Input array :  [[  2.   4.   6.]
 [  1.   3.  nan]]
cumulative sum of array elements taking axis 0:  [[ 2.  4.  6.]
 [ 3.  7.  6.]]