Hello and welcome to this tutorial on Numpy cumsum. In this tutorial, we will be learning about the NumPy cumsum()
method and also seeing a lot of examples regarding the same. So let us begin!
What is NumPy cumsum?
The cumulative sum is a sequence of partial sums of a given sequence. If {a, b, c, d, e, f,…..} is a sequence then its cumulative sum is represented as {a, a+b, a+b+c, a+b+c+d,….}.
The cumsum()
method in NumPy returns the cumulative sum of the elements of the input array along the specified axis. Â It can be the cumulative sum of the flattened array, the cumulative sum of the array elements along the rows or the cumulative sum of the array elements along the columns.Â
We will see the examples for each of these in the upcoming section of this tutorial.
Syntax of NumPy cumsum
numpy.cumsum(a, axis=None, dtype=None, out=None)
Parameter | Description | Required/Optional |
a | Input array. | Required |
axis | Axis along which the cumulative sum of the array is to be calculated. It can be axis=0 i.e. along columns or axis=1 i.e. along rows or axis=None which implies that the cumulative sum of the flattened array is to be returned. | Optional |
dtype (data type) | The data type of the array to be returned. | Optional |
out | An alternative output array in which to place the result. It must have the same shape and length as the expected output. | Optional |
Returns:
A new array that contains the output. If out is mentioned, then a reference to it is returned.
Examples
Let’s now get right into using the numpy.cumsum method so we can understand the outputs.
The cumulative sum of a single element
import numpy as np
a = 5
ans = np.cumsum(a)
print("a =", a)
print("Cumulative sum =", ans)
Output:
a = 5
Cumulative sum = [5]
Numpy cumulative sum of an empty array
import numpy as np
a = []
ans = np.cumsum(a)
print("a =", a)
print("Cumulative sum =", ans)
Output:
a = []
Cumulative sum = []
Numpy cumulative sum of a 1-dimensional array
import numpy as np
a = [5, 3, 2, 8]
ans = np.cumsum(a)
print("a =", a)
print("Cumulative sum of the array =", ans)
Output:
a = [5, 3, 2, 8]
Cumulative sum of the array = [ 5 8 10 18]
Here, the cumulative sum is calculated as 5, 5+3, 5+3+2, 5+3+2+8 which results in 5, 8, 10, 18.
Numpy cumulative sum of a 2-dimensional array
import numpy as np
a = [[4, 3], [9, 10]]
ans = np.cumsum(a)
print("a =", a)
print("Cumulative sum of the array =", ans)
Output:
a = [[4, 3], [9, 10]]
Cumulative sum of the array = [ 4 7 16 26]
In the case of a 2-dimensional array, when no axis is mentioned, the array is first flattened and then its cumulative sum is calculated.
In the above example, the array is first flattened as [4, 3, 9, 10] i.e. row-wise and then its cumulative sum is calculated as [4, 4+3, 4+3+9, 4+3+9+10] which results in the array [4, 7, 16, 26] which is returned by the function.
Return Numpy.cumsum() of the array as float data type
This is the same as the above examples except that here the returned values are of float data type.
import numpy as np
a = [5, 3, 2, 8]
ans = np.cumsum(a, dtype=float)
print("a =", a)
print("Cumulative sum of the array =", ans)
Output:
a = [5, 3, 2, 8]
Cumulative sum of the array = [ 5. 8. 10. 18.]
Cumulative sum along the axis
axis = 0
import numpy as np
a = [[1, 5, 3], [7, 10, 4]]
# cumulative sum along axis=0
ans = np.cumsum(a, axis=0)
print("a =\n", a)
print("Cumulative sum of the array =\n", ans)
Output:
a =
[[1, 5, 3], [7, 10, 4]]
Cumulative sum of the array =
[[ 1 5 3]
[ 8 15 7]]
Here, the first row is as it is and the second row contains the cumulative sum calculated as 1+7, 5+10, and 3+4 resulting in 8, 15 and 7.
axis = 1
import numpy as np
a = [[1, 5, 3], [7, 10, 4]]
# cumulative sum along axis=1
ans = np.cumsum(a, axis=1)
print("a =\n", a)
print("Cumulative sum of the array =\n", ans)
Output:
a =
[[1, 5, 3], [7, 10, 4]]
Cumulative sum of the array =
[[ 1 6 9]
[ 7 17 21]]
Here, the first column is as it is and the second column contains the cumulative sum calculated as 1+5, 7+10 resulting in 6, 17 and the third column has the cumulative sum of 1+5+3, 7+10+4 i.e. 9 and 21.
Summary
That’s all! In this tutorial, we learned about the Numpy cumsum method and practiced different types of examples using the same.