Numpy sum function – Python NumPy sum() Function

NumPy sum() Function:

Numpy sum function: The sum() method in NumPy module is used to calculate the sum of array elements along a specified axis. By default, the function calculates the sum of all items in the array; otherwise, it calculates over the specified axis.

Syntax:

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

Parameters

a: This is required. It is the array given as input to compute the sum.

axis: This is optional. Indicates which axis or axes the sum will be computed along. The default, axis=None, sums all of the input array’s items. When an axis is negative, it counts backward from the last to the first axis.

dtype: This is optional. It is the data type of the array that will be returned. Unless “a” has an integer dtype with less precision than the default platform integer, the dtype of “a” is utilized by default.

out: This is optional. It is the output array for the result. None is the default value. It must have the same shape as output if it is provided.

keepdims: This is optional. The reduced axes are left in the output as dimensions with size one if this is set to True. The result will broadcast correctly against the input array if you use this option. The keepdims will not be passed through to the sum method of ndarray sub-classes if the default value is used, but any non-default value will. Exceptions will be thrown if the sub-class method does not implement keepdims.

initial: This is optional. It indicates the start value for the sum.

where: This is optional. It is an array of boolean to specify which element to include in the sum.

Return Value: 

Numpy sum: Returns an array that has the same shape as “a” but without the specified axis. A scalar is returned if “a” is a 0-d array or the axis is None. A reference to out is returned if an output array is given.

NumPy sum() Function in Python

Example1

Approach:

  • Import numpy module using the import keyword.
  • Pass some random list as an argument to the array() function to create an array.
  • Store it in a variable.
  • Print the above-given array.
  • Pass the given array as an argument to the sum() function of numpy module to get the sum of all elements in the given array.
  • Store it in another variable.
  • Print the sum of all elements in the given array.
  • The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
# Pass some random list as an argument to the array() function to
# create an array. 
# Store it in a variable.
gvn_arry = np.array([[15, 35],[5, 20]])              
# Print the above given array.
print("The above given array is:")
print(gvn_arry)
# Pass the given array as an argument to the sum() function of numpy module 
# to get the sum of all elements in the given array.
# Store it in another variable.
rslt = np.sum(gvn_arry)
# Print the sum of all elements in the given array
print("The sum of all elements in the given array = ",rslt)

Output:

The above given array is:
[[15 35]
 [ 5 20]]
The sum of all elements in the given array = 75

Example2

The sum is calculated over the given axes when the axis argument is specified.

Approach:

  • Import numpy module using the import keyword.
  • Pass some random list as an argument to the array() function to create an array.
  • Store it in a variable.
  • Print the above-given array.
  • Pass the given array, axis=0 as the argument to the sum() function of numpy module to get the sum of all elements in the given array along axis=0.
  • Store it in another variable.
  • Print the sum of all elements in the given array along axis=0.
  • Pass the given array, axis=1 as the argument to the sum() function of numpy module to get the sum of all elements in the given array along axis=1.
  • Store it in another variable.
  • Print the sum of all elements in the given array along axis=1.
  • The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
# Pass some random list as an argument to the array() function to
# create an array. 
# Store it in a variable.
gvn_arry = np.array([[15, 35, 10],
                     [5, 20, 30],
                     [25, 50, 100]])              
# Print the above given array.
print("The above given array is:")
print(gvn_arry)
# Pass the given array, axis=0 as the argument to the sum() function of numpy module 
# to get the sum of all elements in the given array along axis=0.
# Store it in another variable.
rslt_0 = np.sum(gvn_arry, axis=0)
# Print the sum of all elements in the given array along axis=0.
print("The sum of all elements in the given array along axis=0: ", rslt_0)
# Pass the given array, axis=1 as the argument to the sum() function of numpy module 
# to get the sum of all elements in the given array along axis=1.
# Store it in another variable.
rslt_1 = np.sum(gvn_arry, axis=1)
# Print the sum of all elements in the given array along axis=1.
print("The sum of all elements in the given array along axis=1: ", rslt_1)

Output:

The above given array is:
[[ 15 35 10]
[ 5 20 30]
[ 25 50 100]]
The sum of all elements in the given array along axis=0: [ 45 105 140]
The sum of all elements in the given array along axis=1: [ 60 55 175]

Example3

The dtype argument can be used to retrieve the selected data type for the result.

# Import numpy module using the import keyword
import numpy as np
# Pass some random list as an argument to the array() function to
# create an array. 
# Store it in a variable.
gvn_arry = np.array([[15, 35, 10],
                     [5, 20, 30],
                     [25, 50, 100]])              
# Print the above given array.
print("The above given array is:")
print(gvn_arry)
# Pass the given array, axis=1, dtype= complex as the arguments to the sum() 
# function of numpy module to get the sum of all elements in 
# the given array along axis=1 in complex format.
# Store it in another variable.
rslt_1 = np.sum(gvn_arry, axis=1, dtype=complex)
# Print the sum of all elements in the given array along axis=1 in complex format.
print("The sum of all elements in the given array along axis=1 in complex format:")
print(rslt_1)

Output:

The above given array is:
[[ 15 35 10]
 [ 5 20 30]
 [ 25 50 100]]
The sum of all elements in the given array along axis=1 in complex format:
[ 60.+0.j 55.+0.j 175.+0.j]