Python Numpy cumsum() - Cumulative Sum Calculation

Updated on 15 May, 2025
cumsum() header image

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

  1. Import the NumPy library.

  2. Create a one-dimensional array.

  3. Apply the cumsum() function.

    python
    import 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

  1. Define a multi-dimensional array.

  2. Apply the cumsum() function without specifying an axis.

    python
    import 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

  1. Understand that specifying an axis will restrict the cumsum() to that axis.

  2. Choose an axis (0 for columns, 1 for rows) for computing the cumulative sum.

  3. Apply the cumsum() function to the selected axis.

    python
    import 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]]. For cumulative_sum_axis1, it computes along each row, resulting in [[1, 3], [3, 7]].

Cumulative Product in NumPy with cumprod()

  1. Import the NumPy library.

  2. Create a one-dimensional array.

  3. Apply the cumprod() function.

    python
    import 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

  1. Import the NumPy library.

  2. Create a one-dimensional array.

  3. Define the window size.

  4. Use np.convolve() with a ones array to compute the rolling sum.

    python
    import 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]. Unlike cumsum(), which accumulates the entire sequence, np.convolve() can calculate sums over a sliding window. For more advanced rolling operations, consider using pandas.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.

Comments

No comments yet.