In this article, we are going to understand How to Sum all elements of numpy array np. sum() function. sum operations on values of numpy array, sum of rows values, the sum of column values. We will learn about the Sum function in the Numpy Library that is used to do the summation operations on different values of the numpy array.
NumPy Sum() Function
NumPy.Sum() function returns the sum of elements over the specified axis of an array. It takes four agruments
Syntax:
numpy.sum(arr, axis, dtype, out)
Parameters :
- arr : The input array.
axis : axis along which we want to calculate the sum value. Otherwise, it will consider
arr to be flattened(works on all the axis). axis = 0 means along the column and axis = 1
means working along the row. - out : Different array in which we want to place the result. The array must have same dimensions as expected output. Default is None. initial : [scalar, optional] Starting value of the sum.
Return :
Sum of the array elements (a scalar value if the axis is none) or array with sum values along the specified axis.
1. How to Sum all elements of numpy array
In this example, we are going to see how we can calculate the sum of all elements of a 2D Numpy array. by using the np.sum() function. This will give us the result with the sum of column elements in the form of an array. Let us check this in the below code example.
import numpy as np
samplearray = np.array([[1, 2, 3, 4, 5],
[6, 7, 8, 9, 10],
[11, 12, 13, 14, 15,]])
columnSum = samplearray.sum()
print('\nsum of 2D Numpy array:',columnSum)
Output
sum of 2D Numpy array: 120
Also, we can specify the data type in the sum() function as an argument. This is helpful when we need to find sum of decimal or floating-point elements in a Numpy array. Here is an example
import numpy as np
samplearray = [1, 2, 2.2, 10, 4.5]
print(samplearray)
print("The Sum of samplearray(uint8) : ", np.sum(samplearray, dtype = np.uint8))
print("The Sum of samplearray(float32) : ", np.sum(samplearray, dtype = np.float32))
Output
[1, 2, 2.2, 10, 4.5]
The Sum of samplearray(uint8) : 19
The Sum of samplearray(float32) : 19.7
2. How to get sum of all columns in a NumPy array
In this example, we are going to see how we can calculate the sum of every column value in a Numpy array. To do this we can use the sum function by just setting the axis value to 0 in the sum() function. This will give us the result with the sum of column elements in the form of an array. Let us check this in the below code example.
import numpy as np
samplearray = np.array([[1, 2, 3, 4, 5],
[6, 7, 8, 9, 10],
[11, 12, 13, 14, 15,]])
print(samplearray)
columnSum = samplearray.sum(axis=0)
print('\nSum of numpy columns:',columnSum)
Output
[[ 1 2 3 4 5]
[ 6 7 8 9 10]
[11 12 13 14 15]]
Sum of numpy columns: [18 21 24 27 30]
3. NumPy sum specific Columns
In this example, we are finding the sum of specific 2D numpy array columns by using the numpy.sum() function and slicing [:,2]where the first colon(:) represents the rows and the second is for columns
import numpy as np
samplearray = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10],[11, 12, 13, 14, 15,]])
columnSum = samplearray[:,2].sum()
print('\nSum of numpy specifc column:',columnSum)
Output
Sum of numpy specifc columns: 24
4. How to get sum of all row in 2D NumPy array?
In this example, we are going to see how we can calculate the sum of every row value in a Numpy array. To do this we can use the sum function by just setting the axis value to 1 in the sum() function. This will give us the result with the sum of rows elements in the form of an array. Let us check this in the below code example.
import numpy as np
samplearray = np.array([[1, 2, 3, 4, 5],
[6, 7, 8, 9, 10],
[11, 12, 13, 14, 15,]])
print(samplearray)
rowSum = samplearray.sum(axis=1)
print('\nSum of numpy rows:',rowSum)
Output
[[ 1 2 3 4 5]
[ 6 7 8 9 10]
[11 12 13 14 15]]
Sum of numpy rows: [15 40 65]
5. NumPy Sum with condition
In this python program example, we will understand how to sum the elements of a 2D numpy array with the condition. We are finding the sum of numpy array elements which array less than 15.
import numpy as np
samplearray = np.array([[1, 2, 3, 4, 5],
[6, 7, 8, 9, 10],
[11, 12, 13, 14, 15,]])
conditionSUm= samplearray[samplearray<15].sum()
print('\nNumPy sum with condition:',conditionSUm)
Output
NumPy sum with condition: 105