Python numpy sum

The Python numpy sum() function helps compute the total or sum of all the elements in a given array along the specified axis.

Syntax

The syntax of this statistical Python numpy sum method is

numpy.sum(a, axis = None, dtype = None, out = None, keepdims = <no value>, initial = <no value>, where = <no value>)

Here, a is a ndarray that is mandatory, and all the remaining arguments are optional.

Python numpy sum Example

By default, it will display the total based on the ndarray data type. However, using the dtype argument, you can change the data type of the output. For instance, you can change the result from float to int or complex, and so on.

In this example, we declared a one-dimensional ndarray. Next, it calculates the sum of all the array items and uses the dtype argument to change the data type.

import numpy as np

a = np.array([2, 4, 6, 8.5, 10, 12.1])

b  = np.sum(a)
print(b)

c = np.sum(a, dtype = np.int8)
print(c)

d = np.sum(a, dtype = np.float32, initial = 50)
print(d)
42.6
42
92.6

b = 2 + 4 + 6 + 8.5 + 10 + 12.1 = 42.6

For the c variable, we used the dtype to change the data type to an 8-bit integer.

Python numpy sum Two-Dimensional example

This program will declare a two-dimensional square matrix and find the total.

The initial argument is the starting value. Use this argument to add a certain value to the whole sum or axis based.

import numpy as np

a = np.array([[10, 20], [30, 40]])

b  = np.sum(a)
print(b)

c = np.sum(a, initial = 100)
print(c)
100
200

b = 10 + 20 + 30 + 40 = 100

c = 100 (initial) + 10 + 20 + 30 + 40 = 200

If the axis is not provided, it will calculate the sum of all elements irrespective of the dimensions or size. As shown in the above example, it has returned the total of the two-dimensional array.

However, we can compute the total of row and column-wise by specifying the axis value. For instance, in the below example, axis = 0 calculates the total for each column, and axis = 1 finds each row.

Python numpy sum Column Wise

In this example, we used the random randint method to generate a 2D array of 5 * 4 size. Here, the c variable finds the total for each column, and the d variable adds 100 to the total for each column.

import numpy as np

a = np.random.randint(10, 50, size = (5, 4))
print(a)

b  = np.sum(a)
print(b)

c = np.sum(a, axis = 0)
print(c)

d = np.sum(a, axis = 0, initial = 100)
print(d)
[[41 11 44 27]
 [15 43 33 24]
 [46 39 45 21]
 [42 12 19 12]
 [15 19 29 14]]

551

[159 124 170  98]

[259 224 270 198]

Row Wise

To save the Python numpy sum() method result in an already defined array, use the out argument. Remember, the variable size has to match the output.

Here, axis = 1 will calculate the totals for each row in an array. 

  • np.sum(a, axis = 1, initial = 200) – Add 200 to each row total.
  • np.sum(a, axis = 1, initial = 500, out = x) – Saves the output in x variable.

The keepdims argument accepts boolean True or False, and the default value is false. Therefore, the result will reshape if you use the keepdims argument and assigned True value.

import numpy as np

a = np.random.randint(10, 50, size = (5, 4))
print(a)

x = np.arange(5)

b  = np.sum(a)
print(b)

c = np.sum(a, axis = 1)
print(c)

d = np.sum(a, axis = 1, initial = 200)
print(d)

np.sum(a, axis = 1, initial = 500, out = x)
print(x)

e = np.sum(a, axis = 1,  keepdims = True)
print(e)
[[36 43 27 11]
 [19 15 17 23]
 [45 37 37 46]
 [11 47 45 47]
 [41 41 42 41]]

671

[117  74 165 150 165]

[317 274 365 350 365]

[617 574 665 650 665]

[[117]
 [ 74]
 [165]
 [150]
 [165]]

where argument

The where argument in the Python numpy add helps to omit the row or column from the addition. In the below code, we used the where argument along the axis.

import numpy as np

#a = np.random.randint(20, 100, size = (3, 3))
a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print(a)

b  = np.sum(a)
print(b)

c = np.sum(a, where = [False, True, True])
print(c)

d = np.sum(a, where = [False, True, True], axis = 0)
print(d)

e = np.sum(a, where = [True, False, True], axis = 1)
print(e)
[[1 2 3]
 [4 5 6]
 [7 8 9]]

45

33

[ 0 15 18]

[ 4 10 16]

where = [False, True, True] – It will find the total of 2 and 3 columns and omits the first column values 1, 4, and 7.

where = [True, True, False], axis = 0 – It will return zero for the last or 3rd column because the where argument will not allow calculating the sum.

where = [True, False, True], axis = 1 – While finding the row based total, it will add the 2nd column values 2, 5, and 8.