
Introduction
The cumsum()
function in Python's NumPy library is vital for computing cumulative sums in Python across an array's elements. This NumPy cumsum()
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 in Python 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 in Python
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 Python Arrays
Calculate Cumulative Sum Across Entire Array in Python
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]]
.
Cumulative Product in NumPy with cumprod()
Import the NumPy library.
Create a one-dimensional array.
Apply the
cumprod()
function.pythonimport numpy as np array = np.array([1, 2, 3, 4]) cumprod = np.cumprod(array) print(cumprod)
This code produces a cumulative product for the array
[1, 2, 3, 4]
, resulting in[1, 2, 6, 24]
. Each position in the resulting array represents the product of all preceding elements including itself.
Rolling and Running Sums in NumPy
Import the NumPy library.
Create a one-dimensional array.
Define the window size.
Use
np.convolve()
with a ones array to compute the rolling sum.pythonimport numpy as np data = np.array([1, 2, 3, 4, 5]) window_size = 3 rolling_sum = np.convolve(data, np.ones(window_size, dtype=int), 'valid') print(rolling_sum)
This code computes a rolling sum with a window size of 3, producing the output
[6, 9, 12]
. Unlikecumsum()
, which accumulates the entire sequence,np.convolve()
can calculate sums over a sliding window. For more advanced rolling operations, consider usingpandas.Series.rolling().sum()
.
Conclusion
The np.cumsum()
function, also written as numpy.cumsum
, offers a straightforward method to compute the cumulative sum in NumPy. It provides 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.