The numpy package library in Python has a powerful function called cumsum that can be used to calculate the cumulative sum of elements along a specified axis of a numpy ndarray array. This function calculates the sum of the elements of an array in a cumulative way, from left to right.
Code Examples: 1. Simple example:
import numpy as np a = np.array([1,2,3,4]) print(a.cumsum())
Output: [ 1 3 6 10]
2. Multiple dimensions:
import numpy as np
arr = np.array([[1, 2], [3, 4]])
print(np.cumsum(arr))
Output: [ 1 3 6 10]
3. Axis parameter:
import numpy as np
arr = np.array([1, 2, 3, 4])
print(np.cumsum(arr, axis=0))
Output: [1 3 6 10]
4. Floats:
import numpy as np
arr = np.array([1.1, 2.2, 3.3, 4.4])
print(np.cumsum(arr))
Output: [ 1.1 3.3 6.6 11. ]
In summary, the numpy cumsum function is very useful when working with numpy ndarray arrays in Python. It can be used to calculate the cumulative sum of elements along a specified axis of the array.
Python ndarray.cumsum - 30 examples found. These are the top rated real world Python examples of numpy.ndarray.cumsum extracted from open source projects. You can rate examples to help us improve the quality of examples.