Python Numpy cumsum() - Cumulative Sum Calculation

Updated on November 6, 2024
cumsum() header image

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

  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 Arrays

Calculate Cumulative Sum Across Entire Array

  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]].

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.