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.
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)
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.
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.
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.
np.cumsum([1, 2, 3, 4]) return?It returns the running total: 1, 1+2=3, 3+3=6, 6+4=10.
arr = np.array([[1,2],[3,4]]), what does np.cumsum(arr, axis=1) compute?Axis=1 means sum across rows (left to right).
np.cumsum() without specifying an axis on a 2D array?Without axis, the array is flattened and cumulative sum is computed on the flat array.
np.cumsum() is useful?Running totals like daily sales are perfect for cumulative sums.
np.cumsum() compared to the input array?The output array has the same shape as the input.
np.cumsum() works on a simple list of numbers.np.cumsum() on a 2D array to get cumulative sums along rows.