0
0
NumPydata~5 mins

np.cumsum() for cumulative sum in NumPy - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does np.cumsum() do in numpy?

np.cumsum() calculates the cumulative sum of array elements along a given axis. It adds each element to the sum of all previous elements.

Click to reveal answer
beginner
How does np.cumsum() behave on a 1D array like [1, 2, 3]?

It returns [1, 3, 6] because:

  • 1 (first element)
  • 1 + 2 = 3 (sum of first two)
  • 1 + 2 + 3 = 6 (sum of all three)
Click to reveal answer
intermediate
Can np.cumsum() work on multi-dimensional arrays?

Yes, it can compute cumulative sums along a specified axis (rows or columns). If no axis is given, it treats the array as flattened.

Click to reveal answer
intermediate
What is the output of np.cumsum(np.array([[1,2],[3,4]]), axis=0)?

The cumulative sum along rows (axis=0) is:

[[1, 2],
 [4, 6]]

Explanation: sums down each column: 1, then 1+3=4; 2, then 2+4=6.

Click to reveal answer
beginner
Why is np.cumsum() useful in real life?

It helps track running totals, like daily sales, distances traveled, or cumulative scores, making it easier to see progress over time.

Click to reveal answer
What does np.cumsum([1, 2, 3, 4]) return?
A[1, 2, 3, 4]
B[1, 3, 6, 10]
C[10, 9, 7, 4]
D[4, 7, 9, 10]
If arr = np.array([[1,2],[3,4]]), what does np.cumsum(arr, axis=1) compute?
ACumulative sum down each column
BDifference between elements
CSum of all elements
DCumulative sum across each row
What happens if you call np.cumsum() without specifying an axis on a 2D array?
AIt sums elements row-wise
BIt sums elements column-wise
CIt treats the array as flat and sums cumulatively
DIt throws an error
Which of these is a real-life example where np.cumsum() is useful?
ACalculating daily total sales over a month
BFinding the maximum value in a dataset
CSorting a list of names alphabetically
DCounting the number of unique items
What is the shape of the output of np.cumsum() compared to the input array?
ASame shape
BAlways larger
CAlways smaller
DDepends on the axis
Explain how np.cumsum() works on a simple list of numbers.
Think about adding numbers one by one and keeping track of the total.
You got /3 concepts.
    Describe how you would use np.cumsum() on a 2D array to get cumulative sums along rows.
    Remember axis=1 means left to right across each row.
    You got /3 concepts.