📜  Python中的 numpy.cumsum()

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

Python中的 numpy.cumsum()

当我们想要计算给定轴上数组元素的累积和时,使用numpy.cumsum()函数。

代码#1:工作

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

输出 :

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


代码#2:

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

输出 :

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


代码#3:

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

输出 :

Input array :  [[2 4 6]
 [1 3 5]]
cumulative sum of array elements taking axis 1:  [[ 2  6 12]
 [ 1  4  9]]