By specifying axis=1, NumPy will compute the sum of the numbers across each array.
Execute the summation for the following array along the first axis:
import numpy as np arr1 = np.array([1, 2, 3]) arr2 = np.array([1, 2, 3]) newarr = np.sum([arr1, arr2], axis=1) print(newarr) |
Returns: [6, 6] |
Cumulative sum involves incrementally adding array elements. For example, the cumulative sum of [1, 2, 3, 4] is [1, 3, 6, 10]. This can be done using the cumsum() function.
Calculate the cumulative summation for the following array:
import numpy as np arr = np.array([1, 2, 3]) newarr = np.cumsum(arr) print(newarr) |