Numpy – Sum of Values in Array

In this tutorial, we will look at how to get the sum of values of a numpy array. We will also look at specific use-cases like summing along an axis for higher dimensional arrays.

sum of values in numpy array

You can use the numpy sum() function to sum elements of an array. The following is the syntax for a range of different use-cases:

# arr is a numpy array
# sum of all values
arr.sum()
# sum of each row (for 2D array)
arr.sum(axis=1)
# sum of each column (for 2D array)
arr.sum(axis=0)
# sum along a specific axis, n
arr.sum(axis=n)

You can also specify the axis to sum the numpy array along with the axis parameter (see the examples below)

Let’s now look at some of the use-cases of using the numpy sum() function.

Use the numpy sum() function without any parameters to get the sum total of all values inside the array.

Let’s create a numpy array and illustrate its usage.

import numpy as np

# create an array
arr = np.array([2, 0, 1, 3])
# sum of array values
total = arr.sum()
print(total)

Output:

6

We get 6 as the output which is the sum of all values in the above array arr: 2+0+1+3

You can use the above syntax to sum values in higher dimensional numpy arrays as well. For example, let’s get the total of all elements in a 2D numpy array –

# create a 2D numpy array
arr = np.array([[1, 0, 0],
                [2, 1, 1]])
# sum of array values
total = arr.sum()
# display the array and the sum
print(arr)
print("Sum:", total)

Output:

[[1 0 0]
 [2 1 1]]
Sum: 5

Here, we created a 2D array and then calculated its sum. You can see that we get the sum of all the elements in the above 2D array with the same syntax. This can be extended to higher-dimensional numpy arrays as well.

To get the sum of each row in a 2D numpy array, pass axis=1 to the sum() function. This argument tells the function of the axis along which the elements are to be summed. Let’s use it to get the sum of each row in the array arr.

# create a 2D numpy array
arr = np.array([[1, 0, 0],
                [2, 1, 1]])
# sum of each row
row_totals = arr.sum(axis=1)
# display the array and the sum
print(arr)
print("Sum of each row:", row_totals)

Output:

[[1 0 0]
 [2 1 1]]
Sum of each row: [1 4]

We get the sum of each row with axis=1. The first row sums to 1 and the second-row sums to 4. The result is returned as a numpy array.

To get the sum of each column in a 2D numpy array, pass axis=0 to the sum() function. This argument tells the function of the axis along which the elements are to be summed. Let’s use it to get the sum of each column in the array arr.

# create a 2D numpy array
arr = np.array([[1, 0, 0],
                [2, 1, 1]])
# sum of each column
col_totals = arr.sum(axis=0)
# display the array and the sum
print(arr)
print("Sum of each column:", col_totals)

Output:

[[1 0 0]
 [2 1 1]]
Sum of each column: [3 1 1]

The resulting array [3, 1, 1] contains the sum of values in each column. That is, in the above example – 1+2, 0+1, and 0+1.

The numpy sum() function also has additional parameters, for example, to specify the data type of the output, etc. For more, refer to its documentation.

With this, we come to the end of this tutorial. The code examples and results presented in this tutorial have been implemented in a Jupyter Notebook with a python (version 3.8.3) kernel having numpy version 1.18.5


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.