np.cumsum:如何在Python中使用numpy cumsum()函数

1,431 阅读4分钟

np.cumsum - How to Use numpy cumsum() Function in Python

np.cumsum() 是一个numpy库函数,用于返回沿给定轴的元素的累积和。numpy cumsum()函数返回一个数组。该数组由原始数组的累积和组成。累积和与sum函数不相似。

语法

numpy.cumsum(arr, axis=None, dtype=None, out=None) 

参数

np.cumsum() 函数有一个必要参数和三个可选参数作为参数。

  1. arr:数组在这个参数中被传递。这个值是返回数组的累积和所需要的参数。这个数组在cumsum()函数中被给出,用于寻找这个数组的累积和。
  2. axis(轴)。计算累积和的坐标轴是作为这个坐标轴参数的值给出的。默认情况下,该轴保持为。但是,轴可以被改变。
  3. dtype。 dtype代表数据类型。这指定了输出的数据类型。输出的数据将以这个指定的数据类型返回。
  4. out。 这是存储输出数组的目的地。指定的这个数组大小必须与从函数中获得的输出相匹配。

返回值

np.cumsum() 函数返回一个数组作为输出。cumsum() 函数找出数组的累加和,并以数组格式返回累加和。

使用 np.cumsum() 函数从累积和中计算和的 Python 程序

# Importing numpy as np
import numpy as np

# Creating an numpy array called arr
arr = np.array([5, 6, 7, 8, 9, 10, 11, 12, 19, 25])

# Printing the array arr
print("The array arr is: ")
print(arr)

# Printing the shape of the array
print("The shape of the array arr is : ", arr.shape)

# Finding the sum from the cumulative sum function for the array
res = np.cumsum(arr)[-1]

# Printing the sum of the array
print("The sum of the array using the cumulative sum function is:")
print(res)

输出

The array arr is:
[ 5 6 7 8 9 10 11 12 19 25]
The shape of the array arr is : (10,)
The sum of the array using the cumulative sum function is:
112

我们导入了numpy包,在这个程序中提供了几个数组函数。然后,我们使用名为 np.array() 的函数创建了一个名为arr的numpy数组。这个arr数组由几个整数元素组成。

对于这个数组,使用累积和函数来计算总和。为了做到这一点,我们将数组传入 cumsum() 函数。 cumsum() 函数找到了累积和,然后我们通过-1的索引只返回了数组的最后一个值。它返回数组中的最后一个元素。

数组中的最后一个元素是数组中所有元素的总和。因此,我们使用 cumsum() 函数打印了所有元素的总和。

使用np.cumsum()函数计算累积和的Python程序

# Importing numpy as np
import numpy as np

# Creating an numpy array called a
a = np.array([5, 6, 7, 8, 9, 10, 11, 12, 19, 25])

# printing the array a
print("The array a is: ")
print(a)

# Printing the shape of the array a
print("The shape of the array arr is : ", a.shape)

# Finding the cumulative sum of the single dimensional array
op = np.cumsum(a)

# Printing the sum of the array
print("The cumulative sum of the array is: ")
print(op)

# Creating an numpy array called arr
arr = np.array([[1, 4, 0], [7, 0, 9], [5, 0, 3], [12, 0, 5]])

# printing the array arr
print("The array arr is: ")
print(arr)

# Printing the shape of the array
print("The shape of the array arr is : ", arr.shape)

# Finding the cumulative sum for the two dimensional array
res = np.cumsum(arr, axis=1)

# Printing the cumulative sum of the array
print("The cumulative sum of the array is: ")
print(res)

输出

The array a is:
[ 5 6 7 8 9 10 11 12 19 25]
The shape of the array arr is : (10,)
The cumulative sum of the array is:
[ 5 11 18 26 35 45 56 68 87 112]
The array arr is:
[[ 1 4 0]
[ 7 0 9]
[ 5 0 3]
[12 0 5]]
The shape of the array arr is : (4, 3)
The cumulative sum of the array is:
[[ 1 5 5]
[ 7 7 16]
[ 5 5 8]
[12 12 17]]

我们使用名为np.array() 的函数创建了两个名为a和arr的numpy数组。

a " 数组是一个单维数组。这个数组由一些整数值组成。我们把这个数组传给了np.cumsum()函数。

np.cumsum() 函数计算的是数组的累积和。累积和是当前元素的总和加上前一个元素的累积和。结果被显示出来。

arr 数组由4行组成,每行由3列组成。然后我们把这个数组传给 np.cumsum() 函数,轴为1,cumsum() 函数计算出累积和并返回数组。

numpy cumsum()函数就这样了。