
Introduction
The cumsum()
function in Python's NumPy library is a vital tool for computing cumulative sums across an array's elements. This function simplifies the accumulation of values, which is especially useful in statistical computations, data analysis, and anytime you need to keep a running total of sequential data.
In this article, you will learn how to efficiently use the cumsum()
function to perform cumulative sum calculations on arrays. Explore various examples covering one-dimensional arrays, multi-dimensional arrays, and specific axes in multi-dimensional scenarios.
Using cumsum() with One-dimensional Arrays
Calculate Cumulative Sum in a Single Array
Import the NumPy library.
Create a one-dimensional array.
Apply the
cumsum()
function.pythonimport numpy as np one_d_array = np.array([1, 2, 3, 4, 5]) cumulative_sum = np.cumsum(one_d_array) print(cumulative_sum)
This code produces a cumulative sum for the array
[1, 2, 3, 4, 5]
, resulting in[1, 3, 6, 10, 15]
. Each position in the resulting array represents the sum of all preceding elements including itself.
Using cumsum() with Multi-dimensional Arrays
Calculate Cumulative Sum Across Entire Array
Define a multi-dimensional array.
Apply the
cumsum()
function without specifying an axis.pythonimport numpy as np two_d_array = np.array([[1, 2], [3, 4]]) cumulative_sum = np.cumsum(two_d_array) print(cumulative_sum)
This setup treats the array as if it were flattened, resulting in a cumulative sum array of
[1, 3, 6, 10]
.
Specify an Axis for Cumulative Sum
Understand that specifying an axis will restrict the
cumsum()
to that axis.Choose an axis (0 for columns, 1 for rows) for computing the cumulative sum.
Apply the
cumsum()
function to the selected axis.pythonimport numpy as np two_d_array = np.array([[1, 2], [3, 4]]) cumulative_sum_axis0 = np.cumsum(two_d_array, axis=0) cumulative_sum_axis1 = np.cumsum(two_d_array, axis=1) print("Cumulative sum along axis 0 (columns):", cumulative_sum_axis0) print("Cumulative sum along axis 1 (rows):", cumulative_sum_axis1)
For
cumulative_sum_axis0
, the function computes the cumulative sum down each column, producing[[1, 2], [4, 6]]
. Forcumulative_sum_axis1
, it computes along each row, resulting in[[1, 3], [3, 7]]
.
Conclusion
The cumsum()
function from NumPy offers a straightforward way to compute cumulative sums, providing both holistic and axis-specific insights into data accumulation trends within arrays. Whether working with simple lists or complex multi-dimensional arrays, cumsum()
enables efficient and clear summation of numerical data. Utilize this function to enhance your data analysis and ensure your computations are concise and accurate.
No comments yet.