Hello and welcome to this tutorial on the Numpy sum method. In this tutorial, we will be learning about the NumPy sum method and also seeing a lot of examples regarding the same. So let us begin!
Also read: NumPy Cos – A Complete Guide
What is NumPy Sum?
The sum method in NumPy is a function that returns the sum of the array. It can be the sum of the whole array, sum along the rows or sum along the columns. We will see the examples for each of these in the upcoming section of this tutorial.
Also read: Numpy Sin – A Complete Guide
Syntax of NumPy sum
Let us first have a look at the syntax of the NumPy sum function.
numpy.sum(a, axis=None, dtype=None, out=None, keepdims=<no value>, initial=<no value>, where=<no value>)
Parameter | Description | Required/Optional |
a (array_like) | Elements that are to be summed. | Required |
axis | Axis along which the array is to be summed. It can be axis=0 i.e. along columns or axis=1 i.e. along rows or axis=None which implies that sum up the entire array. | 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 as the expected output. | Optional |
keepdims (bool) | If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the input array. | Optional |
initial | Starting value for the sum. | Optional |
where | Elements to include in the sum. | Optional |
Returns:
An array with the same shape as a which contains the sum along the axis given and the specified axis removed. If the axis=None, a scalar is returned which is the sum of the whole array.
Examples of Numpy.sum() method
Let’s now get right into using the numpy.sum method so we can understand the outputs.
Numpy.sum() of the entire array
1-dimensional array
import numpy as np
a = [2, 5, 3, 8, 4]
sum = np.sum(a)
print("a =", a)
print("Sum of the array =", sum)
Output:
a = [2, 5, 3, 8, 4]
Sum of the array = 22
Sum of the array = 2+5+3+8+4 = 17.
2-dimensional array
import numpy as np
a = [[2, 5, 4], [3, 2, 1]]
sum = np.sum(a)
print("a =", a)
print("Sum of the array =", sum)
Output:
a = [[2, 5, 4], [3, 2, 1]]
Sum of the array = 17
Sum of the array = 2+5+4+3+2+1 = 17
Numpy.sum() along the axis
Column-wise sum
import numpy as np
a = [[2, 5, 4],
[3, 2, 1]]
# sum along axis=0 i.e. columns
sum = np.sum(a, axis=0)
print("a =", a)
print("Sum of the array =", sum)
Output:
a = [[2, 5, 4], [3, 2, 1]]
Sum of the array = [5 7 5]
Column 0 sum = 2+3 = 5
Column 1 sum= 5+2 = 7
Column 2 sum = 4+1 = 5
Row-wise sum
import numpy as np
a = [[2, 5, 4],
[3, 2, 1]]
# sum along axis=1 i.e. rows
sum = np.sum(a, axis=1)
print("a =", a)
print("Sum of the array =", sum)
Output:
a = [[2, 5, 4], [3, 2, 1]]
Sum of the array = [11 6]
Row 0 sum = 2+5+4 = 11
Row 1 sum = 3+2+1 = 6
Numpy.sum() of an empty array
import numpy as np
a = []
b = [[]]
sum_a = np.sum(a)
print("a =", a)
print("Sum of the 1-d empty array =", sum_a)
sum_b = np.sum(b)
print("b =", b)
print("Sum of the 2-d empty array =", sum_b)
Output:
a = []
Sum of the 1-d empty array = 0.0
b = [[]]
Sum of the 2-d empty array = 0.0
The sum of an empty array is the neutral element i.e. 0.
Return Numpy.sum() 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.
Sum of the entire array
import numpy as np
a = [[3, 12, 4], [3, 5, 1]]
sum = np.sum(a, dtype=float)
print("a =", a)
print("Sum of the array =", sum)
Output:
a = [[3, 12, 4], [3, 5, 1]]
Sum of the array = 28.0
Column-wise sum
import numpy as np
a = [[3, 12, 4],
[3, 5, 1]]
# sum along axis=0 i.e. columns
sum = np.sum(a, dtype=float, axis=0)
print("a =", a)
print("Sum of the array =", sum)
Output:
a = [[3, 12, 4], [3, 5, 1]]
Sum of the array = [ 6. 17. 5.]
Row-wise sum
import numpy as np
a = [[3, 12, 4],
[3, 5, 1]]
# sum along axis=1 i.e. rows
sum = np.sum(a, dtype=float, axis=1)
print("a =", a)
print("Sum of the array =", sum)
Output:
a = [[3, 12, 4], [3, 5, 1]]
Sum of the array = [19. 9.]
Conclusion
That’s all! In this tutorial, we learned about the Numpy sum method and practiced different types of examples using the same.