numpy.sum() in PythonThe 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. SyntaxThere is the following syntax of numpy.sum() function: Parameters1) 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. ReturnsThis 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()Output: 0.9 In the above code
In the output, the sum of all the elements of the array has been shown. Example 2:Output: 6 In the above code
In the output, the sum only of integer numbers, not floating-point values has been displayed. Example 3:In the above code Output: 13 Example 4:In the above code
In the output, the sum of the column elements has been calculated accordingly. Output: array([4, 9]) Example 5:Output: array([5, 8]) Example 6:Output: 23 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. Next TopicNumpy.random() |