numpy.sum() in Python

The numpy.sum() function is available in the NumPy package of Python. This function is used to compute the sum of all elements, the sum of each row, and the sum of each column of a given array.

Essentially, this sum ups the elements of an array, takes the elements within a ndarray, and adds them together. It is also possible to add rows and column elements of an array. The output will be in the form of an array object.

numpy.sum()

Syntax

There is the following syntax of numpy.sum() function:

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

Parameters

1) arr: array_like

This is a ndarray. This is the source array whose elements we want to sum. This parameter is essential and plays a vital role in numpy.sum() function.

2) axis: int or None or tuple of ints(optional)

This parameter defines the axis along which a sum is performed. The default axis is None, which will sum all the elements of the array. When the axis is negative, it counts from the last to the first axis. In version 1.7.0, a sum is performed on all axis specified in the tuple instead of a single axis or all axis as before when an axis is a tuple of ints.

3) dtype: dtype(optional)

This parameter defines the type of the accumulator and the returned array in which the elements are summed. By default, the dtype of arr is used unless arr has an integer dtype of less precision than the default platform integer. In such a case, when arr is signed, then the platform integer is used, and when arr is unsigned, then an unsigned integer of the same precision as the platform integer is used.

4) out: ndarray(optional)

This parameter defines the alternative output array in which the result will be placed. This resulting array must have the same shape as the expected output. The type of output values will be cast, when necessary.

5) keepdims: bool(option)

This parameter defines a Boolean value. When this parameter is set to True, the axis which is reduced is left in the result as dimensions with size one. With the help of this option, the result will be broadcast correctly against the input array. The keepdims will not be passed to the sum method of sub-classes of a ndarray, when the default value is passed, but not in case of non-default value. If the sub-class method does not implement keepdims, then any exception can be raised.

6) initial: scalar

This parameter defines the starting value for the sum.

Returns

This function returns an array of the same shape as arr with the specified axis removed. When arr is a 0-d array, or when the axis is None, a scalar is returned. A reference to out is returned, when an array output is specified.

Example 1: numpy.array()

snippet
import numpy as np
a=np.array([0.4,0.5])
b=np.sum(a)
b

Output:

Output
0.9

In the above code

  • We have imported numpy with alias name 'np'.
  • We have created an array 'a' using np.array() function.
  • We have declared variable 'b' and assigned the returned value of np.sum() function.
  • We have passed the array 'a' in the function.
  • Lastly, we tried to print the value of b.

In the output, the sum of all the elements of the array has been shown.

Example 2:

snippet
import numpy as np
a=np.array([0.4,0.5,0.9,6.1])
x=np.sum(a, dtype=np.int32)
x

Output:

Output
6

In the above code

  • We have imported numpy with alias name 'np'.
  • We have created an array 'a' using np.array() function.
  • We have declared variable 'x' and assigned the returned value of np.sum() function.
  • We have passed the array 'a' and data type of int32 in the function.
  • Lastly, we tried to print the value of x.

In the output, the sum only of integer numbers, not floating-point values has been displayed.

Example 3:

snippet
import numpy as np
a=np.array([[1,4],[3,5]])
b=np.sum(a)
b

In the above code

Output:

Output
13

Example 4:

snippet
import numpy as np
a=np.array([[1,4],[3,5]])
b=np.sum(a,axis=0)
b

In the above code

  • We have imported numpy with alias name np.
  • We have created an array 'a' using np.array() function.
  • We have declared variable 'b' and assigned the returned value of np.sum() function.
  • We have passed the array 'a' and axis=0 in the function.
  • Lastly, we tried to print the value of b.

In the output, the sum of the column elements has been calculated accordingly.

Output:

Output
array([4, 9])

Example 5:

snippet
import numpy as np
a=np.array([[1,4],[3,5]])
b=np.sum(a,axis=1)
b

Output:

Output
array([5, 8])

Example 6:

snippet
import numpy as np
b=np.sum([15], initial=8)
b

Output:

Output
23

In the above code

  • We have imported numpy with alias name np.
  • We have declared variable 'b' and assigned the returned value of np.sum() function.
  • We have passed the number of elements and initial value in the function.
  • Lastly, we tried to print the value of b.

In the output, the initial value has been added to the last element in the sequence of elements and then performed the sum of all the elements.

Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +