Introduction to NumPy sum
In this article, NumPy sum in Python is defined as a Python library which is specially designed for working on the multi-dimensional array and matrices and NumPy sum() is a function provided by the NumPy Python library that is mainly used to calculate the total sum of the elements present in the given array or the total sum of elements in each row and sum of elements in each column if it in the given matrices or multi-dimensional array and the value returned by the sum() function results in the form of an array object.
Working of NumPy sum() Function in Python
This article will see the NumPy Python library’s function sum(). In Python, the NumPy sum() function is used for computing the summation of the total number of items present in the given array, which means the elements are taken within the NumPy array as an array object and sum up the items of a given array.
Numpy.sum(in_array, axis, dtype, out, keepdims, initial)
- in_array: This parameter is to specify the array name of the input array so that the elements are used to calculate the sum.
- axis: This value can be either none or int or tuple of ints, where this parameter is used for defining the axis for which the sum needs to be computed and the default value specified is none where it will compute the sum of all the elements of the given array.
- dtype: This parameter is used for defining the type of accumulator and to specify the returned data type of the output.
- out: This parameter is used to specify another extra array to store the result or output and the size of this array must be the same as the size of the input array.
- keepdims: This specifies the Boolean value where it is set to true where if the axes are reduced are left in the output result having the dimensions as size one.
- initial: It is used to specify the starting or initial value for calculating the sum.
Now lets see the demonstration of how to use the NumPy sum() function in the python program:
import numpy as np print("Program to demonstrate numpy sum() function: ") print("\n") in_arr = np.array([0,1,3,5,34,10]) print("The given array is as follows:") print(in_arr) print("\n") print("The sum of the given array is:") sum_res = np.sum(in_arr) print(sum_res)
In the above program, we can see we have imported NumPy module and then we have created an array using NumPy object np and then using sum() function and passing this given array as input array to the function and the returns the sum of the element values in the given input array. Usually, the sum of the empty array is a neutral value of 0. In the above, we saw how to add elements of the one-dimensional array.
Let us see how to add the elements of the two-dimensional array in the example below, along with other parameters of the sum() function such as axis and data type.
import numpy as np print("Program to demonstrate numpy sum() function for 2D array: ") print("\n") in_arr = np.array( [[8,1,9], [6,4,1]]) print("The given input 2-D array is as follows:") print(in_arr) print("\n") res_arr_1 = np.sum(in_arr, axis=1, dtype=float) print("Sum of elements at axis 1 row wise with the specified data type is") print(res_arr_1) print("\n") res_arr_2 = np.sum(in_arr, axis=0, dtype=int) print("Sum of elements at axis 0 column wise with the specified data type is") print(res_arr_2) print("\n") res = np.sum(in_arr, axis=1, keepdims = True) print("Keeping the dimensional of the output array same as input array") print(res) print("\n") print("Sum of the total elements in the 2-D array is as follows:") tot_res = np.sum(in_arr) print(tot_res)
In the above program, we declare a 2-D array using the NumPy object np. In this, we have two rows and 3 columns where we have specified axis value as 1 which means we are calculating the sum of the elements row-wise and data type as the float is specified where it gives the result in the floating value of the result. If we specify the axis value 0, it will calculate the summation of elements in column-wise having data type as an integer value and this can also be seen in the above screenshot. Therefore, we can see in the screenshot where the sum of the elements of the first row is 18 and second row is 11 and the result is printed in the form of another array row-wise and if the axis is 0 then result will be of the form of [14, 5, 10] in the floating value. We can also see in the output the sum of the total elements in the given array as 29 which is the total value of elements added together.
In the above section, we saw how to calculate the sum of elements row-wise and column-wise using the axis parameter. From the above example, we can notice that the elements will be reduced to lesser dimensions after calculating the sum() by the axis specified. In general, when we use the axis parameter then by default the function sum() collapses or reduces the input array dimensions ad results in the lesser or lower dimensional array. Suppose we don’t want the resulting array dimension to reduce to the lesser dimension of the input array then we need to use the keepdims parameter in the sum() function to keep the dimension of the output array the same as the input array. In the above example we can see in line 20 we have specified keepdims as “True” so only it displays the output array also as in a 2-D array.
In this article, we conclude that the NumPy module in Python is used in the data science field for working on a multidimensional array. In this article, we also saw how to calculate the sum of the elements in the given array. In this, we saw examples of a one-dimensional and two-dimensional array, and the use of sum() function parameters and how they can be used.
This is a guide to NumPy sum. Here we have discussed the introduction and working of NumPy sum() function in python with examples. You may also have a look at the following articles to learn more –