numpy.cumsum() in Python Last Updated : 04 Dec, 2024 Comments Improve Suggest changes Like Article Like Report 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 result1. Cumulative Sum of a One-Dimensional ArrayTo 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 itself2. Cumulative Sum of a Two-Dimensional ArrayFor 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 rowUsing dtype to Specify Data Type in NumPy cumsumThe 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. Comment More infoAdvertise with us Next Article numpy.cumsum() in Python jana_sayantan Follow Improve Article Tags : Python Numpy AI-ML-DS Python-numpy Python numpy-Mathematical Function +1 More Practice Tags : python 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 NumPy Array in Python NumPy (Numerical Python) is a powerful library for numerical computations in Python. It is commonly referred to multidimensional container that holds the same data type. It is the core data structure of the NumPy library and is optimized for numerical and scientific computation in Python. Table of C 2 min read numpy.nansum() in Python numpy.nansum()function is used when we want to compute the sum of array elements over a given axis treating Not a Numbers (NaNs) as zero. Syntax : numpy.nansum(arr, axis=None, dtype=None, out=None, keepdims='no value') Parameters : arr : [array_like] Array containing numbers whose sum is desired. If 3 min read numpy.sum() in Python This function returns the sum of array elements over the specified axis.Syntax: numpy.sum(arr, axis, dtype, out): Parameters: arr: Input array. axis: The axis along which we want to calculate the sum value. Otherwise, it will consider arr to be flattened(works on all the axes). axis = 0 means along 3 min read Numpy MaskedArray.cumsum() function | Python numpy.MaskedArray.cumsum() Return the cumulative sum of the masked array elements over the given axis.Masked values are set to 0 internally during the computation. However, their position is saved, and the result will be masked at the same locations. Syntax : numpy.ma.cumsum(axis=None, dtype=None, o 3 min read Python | Pandas Panel.cumsum() In Pandas, Panel is a very important container for three-dimensional data. The names for the 3 axes are intended to give some semantic meaning to describing operations involving panel data and, in particular, econometric analysis of panel data. Panel.cumsum() function is used to returns a DataFrame 1 min read Like