Sum of Elements in NumPy Array - Examples


NumPy sum()

To get the sum of all elements in a NumPy array, you can use the numpy.sum() function.

In this tutorial, we shall learn how to use numpy.sum() with syntax and examples.


Syntax

The syntax of numpy.sum() is:

numpy.sum(a, axis=None, dtype=None, out=None, keepdims=, initial=)

We will explain the parameters in the function definition using examples below.


Examples

1. NumPy sum()

In this example, we will find the sum of all elements in a NumPy array using default parameters of the sum() function.

Python Program

import numpy as np

a = np.array([4, 5, 3, 7])
print('input\n', a)

b = np.sum(a)
print('sum\n', b)

Output

input
 [4 5 3 7]
sum
 19

Explanation

  1. The input array is [4, 5, 3, 7].
  2. Each element is added: 4 + 5 + 3 + 7 = 19.
  3. The result of np.sum(a) is 19, which is printed as the output.

2. NumPy sum() along axis

You can specify an axis to sum() to compute the sum of elements along that axis.

Python Program

import numpy as np

a = np.array([4, 5, 3, 7]).reshape(2, 2)
print('input\n', a)

b = np.sum(a, axis=0)
print('sum\n', b)

Output

input
 [[4 5]
  [3 7]]
sum
 [ 7 12]

Explanation

  1. The input array is reshaped to [[4, 5], [3, 7]].
  2. Summing along axis=0 (columns): [4+3, 5+7] = [7, 12].
  3. The result is [7, 12], which is printed as the output.

Now, let us calculate the sum along axis=1.

Python Program

import numpy as np

a = np.array([4, 5, 3, 7]).reshape(2, 2)
print('input\n', a)

b = np.sum(a, axis=1)
print('sum\n', b)

Output

input
 [[4 5]
  [3 7]]
sum
 [ 9 10]

Explanation

  1. The reshaped input array is [[4, 5], [3, 7]].
  2. Summing along axis=1 (rows): [4+5, 3+7] = [9, 10].
  3. The result is [9, 10], which is printed as the output.

3. Specify an initial value to the sum

You can also specify an initial value to the sum. By default, the initial value is 0. However, if you specify a value, it is added to the total sum.

Python Program

import numpy as np

a = np.array([4, 5, 3, 7])
print('input\n', a)

b = np.sum(a, initial=52)
print('sum\n', b)

Output

input
 [4 5 3 7]
sum
 71

Explanation

  1. The input array is [4, 5, 3, 7].
  2. The total sum of the array is 4 + 5 + 3 + 7 = 19.
  3. An initial value of 52 is added to the sum: 19 + 52 = 71.
  4. The result is 71, which is printed as the output.

4. Sum with keepdims=True

Use the keepdims parameter to retain the dimensions of the original array.

Python Program

import numpy as np

a = np.array([[4, 5], [3, 7]])
print('input\n', a)

b = np.sum(a, axis=1, keepdims=True)
print('sum\n', b)

Output

input
 [[4 5]
  [3 7]]
sum
 [[ 9]
  [10]]

Explanation

  1. The input array is [[4, 5], [3, 7]].
  2. Summing along axis=1 results in [9, 10].
  3. With keepdims=True, the dimensions are retained, producing [[9], [10]].

Summary

In this NumPy Tutorial, we learned how to use numpy.sum() to find the sum of elements in a NumPy array, along an axis, and with additional parameters like initial and keepdims. These examples demonstrate the flexibility of numpy.sum() in handling various use cases.


Python Libraries