numpy.sum() function calculates the sum of all the elements in a numpy array or list along a given axis.
import numpy as np arr = [5,2,6,21,63] print("Our array: ", arr) print("Sum of array : ", np.sum(arr))
We can also calculate the sum across different axis such as rows or columns by specifying the axis parameter. Specifying axis = 0 means the sum will be calculated along the column and axis = 1 specifies to find the sum across the rows.
import numpy as np arr = [[1, 17, 12, 12, 36], [78, 16, 27, 70, 21], [98, 45, 42, 11, 45, ]] # sum along the columns print("sum of our array along the columns by setting axis = 0 : ", np.sum(arr, axis = 0)) # sum along the rows print("sum of our array along the rows by setting axis = 1 : ", np.sum(ar