numpy.cumsum()
function is used to compute the cumulative sum of elements in an array. Cumulative sum refers to a sequence where each element is the sum of all previous elements plus itself.
For example, given an array [1, 2, 3, 4, 5], the
cumulative sum would be [1, 3, 6, 10, 15].
Let's implement this as well:
Python
import numpy as np
array = np.array([1, 2, 3, 4, 5])
cumulative_sum = np.cumsum(array)
print("Original array:", array)
print("Cumulative sum:", cumulative_sum)
Output:
numpy.cumsum() in PythonThe numpy.cumsum()
function computes the cumulative sum of array elements along a specified axis or across the entire flattened array. This function can handle both one-dimensional and multi-dimensional arrays, providing flexibility in how cumulative sums are calculated.
Syntax for numpy.cumsum()
is:
numpy.cumsum(array, axis=None, dtype=None, out=None)
where,
- array: The input array containing numbers whose cumulative sum is desired.
- axis: (Optional) The axis along which the cumulative sum is computed. If not specified, the array is flattened.
- dtype: (Optional) The data type of the returned array.
- out: (Optional) An alternative output array to place the result
1. Cumulative Sum of a One-Dimensional Array
To calculate the cumulative sum of a one-dimensional array:
Python
import numpy as np
array1 = np.array([1, 2, 3, 4, 5])
cumulative_sum = np.cumsum(array1)
print(cumulative_sum)
# Output: [ 1 3 6 10 15]
This example demonstrates how each element in the resulting array represents the sum of all preceding elements including itself
2. Cumulative Sum of a Two-Dimensional Array
For two-dimensional arrays, you can specify an axis:
Python
import numpy as np
array2 = np.array([[1, 2], [3, 4]])
cumulative_sum_flattened = np.cumsum(array2)
cumulative_sum_axis0 = np.cumsum(array2, axis=0)
cumulative_sum_axis1 = np.cumsum(array2, axis=1)
print(cumulative_sum_flattened)
print(cumulative_sum_axis0)
print(cumulative_sum_axis1)
Output[ 1 3 6 10]
[[1 2]
[4 6]]
[[1 3]
[3 7]]
- Flattened: Computes the cumulative sum as if the array was one-dimensional.
- Axis=0: Computes the cumulative sum down each column.
- Axis=1: Computes the cumulative sum across each row
Using dtype
to Specify Data Type in NumPy cumsum
The dtype
parameter allows you to specify the data type of the output:
Python
import numpy as np
array3 = np.array([1, 2, 3])
cumulative_sum_float = np.cumsum(array3, dtype=float)
print(cumulative_sum_float)
# Output: [1.0 3.0 6.0]
This ensures that the resulting cumulative sums are stored as floats.
Similar Reads
numpy.cumprod() in Python numpy.cumprod() function is used when we want to compute the cumulative product of array elements over a given axis. Syntax : numpy.cumprod(arr, axis=None, dtype=None, out=None) Parameters : arr : [array_like] Array containing numbers whose cumulative product is desired. If arr is not an array, a co
2 min read
numpy.nancumsum() in Python numpy.nancumsum() function is used when we want to compute the cumulative sum of array elements over a given axis treating Not a Numbers (NaNs) as zero. The cumulative sum does not change when NaNs are encountered and leading NaNs are replaced by zeros. Zeros are returned for slices that are all-NaN
3 min read
numpy.add() in Python NumPy, the Python powerhouse for scientific computing, provides an array of tools to efficiently manipulate and analyze data. Among its key functionalities lies numpy.add() a potent function that performs element-wise addition on NumPy arrays. numpy.add() SyntaxSyntax :Â numpy.add(arr1, arr2, /, out=
4 min read
numpy.bincount() in Python In an array of +ve integers, the numpy.bincount() method counts the occurrence of each element. Each bin value is the occurrence of its index. One can also set the bin size accordingly. Syntax : numpy.bincount(arr, weights = None, min_len = 0) Parameters : arr : [array_like, 1D]Input array, having p
2 min read
Numpy recarray.cumsum() function | Python In numpy, arrays may have a data-types containing fields, analogous to columns in a spreadsheet. An example is [(a, int), (b, float)], where each entry in the array is a pair of (int, float). Normally, these attributes are accessed using dictionary lookups such as arr['a'] and arr['b']. Record array
2 min read