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.
There is the following syntax of numpy.sum() function:
numpy.sum(arr, axis=None, dtype=None, out=None, keepdims=<no value>, initial=<no value>)
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.
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.
import numpy as np a=np.array([0.4,0.5]) b=np.sum(a) b
Output:
In the above code
In the output, the sum of all the elements of the array has been shown.
import numpy as np a=np.array([0.4,0.5,0.9,6.1]) x=np.sum(a, dtype=np.int32) x
Output:
In the above code
In the output, the sum only of integer numbers, not floating-point values has been displayed.
import numpy as np a=np.array([[1,4],[3,5]]) b=np.sum(a) b
In the above code
Output:
import numpy as np a=np.array([[1,4],[3,5]]) b=np.sum(a,axis=0) b
In the above code
In the output, the sum of the column elements has been calculated accordingly.
Output:
import numpy as np a=np.array([[1,4],[3,5]]) b=np.sum(a,axis=1) b
Output:
import numpy as np b=np.sum([15], initial=8) b
Output:
In the above code
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.