The numpy.cumsum()
function in NumPy is used to compute the cumulative sum of elements in a given input array over a given axis.
numpy.cumsum(a, axis=None, dtype=None, out=None)
The numpy.cumsum()
function takes the following parameter values:
a
(required): The input array containing numbers is to be computed.axis
(optional): The axis along which the product is determined.dtype
(optional): The data type of the output array.out
(optional): The alternate array where the result is placed.The numpy.cumsum()
function returns an output array holding the result.
import numpy as np# creating an arrayx = np.array([1, 2, 3])# Implementing the numpy.cumsum() functionmyarray = np.cumsum(x, axis=0)print(x)print(myarray)
Here is a line-by-line explanation of the above code:
numpy
module.x
, using the array()
method.np.cumsum()
function on the array. The result is assigned to a variable, myarray
.x
.myarray
.