## 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 NumPysum() Function in Python with Examples

In this article, we 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 sums up the items of a given array.

## Syntax and parameters taken by sum() function of NumPy in Python:

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.

## Code:

```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 that is 0. In the above we saw how to add elements of the one-dimensional array.

## Example #2

Let us see how to add the elements of the two-dimensional array in the below example along with different other parameters of the sum() function such as axis, data type.

### Code:

```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)
```

Apply now for Advanced Python Training Course