# numpy.sum() in Python

## numpy.sum() in Python

`numpy.sum(arr, axis, dtype, out)` : This function returns the sum of array elements over the specified axis.

Parameters :
arr : input array.
axis : axis along which we want to calculate the sum value. Otherwise, it will consider arr to be flattened(works on all the axis). axis = 0 means along the column and axis = 1 means working along the row.
out : Different array in which we want to place the result. The array must have same dimensions as expected output. Default is None.
initial : [scalar, optional] Starting value of the sum.

Return : Sum of the array elements (a scalar value if axis is none) or array with sum values along the specified axis.

Code #1:

 `# Python Program illustrating ` `# numpy.sum() method` `import` `numpy as np ` `      ` `# 1D array ` `arr ``=` `[``20``, ``2``, .``2``, ``10``, ``4``]  ` `  ` `print``(``"\nSum of arr : "``, np.``sum``(arr)) ` `  ` `print``(``"Sum of arr(uint8) : "``, np.``sum``(arr, dtype ``=` `np.uint8)) ` `print``(``"Sum of arr(float32) : "``, np.``sum``(arr, dtype ``=` `np.float32))` `  ` `print` `(``"\nIs np.sum(arr).dtype == np.uint : "``, ` `       ``np.``sum``(arr).dtype ``=``=` `np.uint) ` ` ` `print` `(``"Is np.sum(arr).dtype == np.float : "``, ` `       ``np.``sum``(arr).dtype ``=``=` `np.``float``) `

Output:

```Sum of arr :  36.2
Sum of arr(uint8) :  36
Sum of arr(float32) :  36.2

Is np.sum(arr).dtype == np.uint :  False
Is np.sum(arr).dtype == np.uint :  True
```

Code #2:

 `# Python Program illustrating ` `# numpy.sum() method` `import` `numpy as np ` `      ` `# 2D array ` `arr ``=` `[[``14``, ``17``, ``12``, ``33``, ``44``],   ` `       ``[``15``, ``6``, ``27``, ``8``, ``19``],  ` `       ``[``23``, ``2``, ``54``, ``1``, ``4``,]]  ` `  ` `print``(``"\nSum of arr : "``, np.``sum``(arr)) ` `  ` `print``(``"Sum of arr(uint8) : "``, np.``sum``(arr, dtype ``=` `np.uint8)) ` `print``(``"Sum of arr(float32) : "``, np.``sum``(arr, dtype ``=` `np.float32))` `  ` `print` `(``"\nIs np.sum(arr).dtype == np.uint : "``, ` `                 ``np.``sum``(arr).dtype ``=``=` `np.uint) ` ` ` `print` `(``"Is np.sum(arr).dtype == np.uint : "``, ` `              ``np.``sum``(arr).dtype ``=``=` `np.``float``) `

Output:

```Sum of arr :  279
Sum of arr(uint8) :  23
Sum of arr(float32) :  279.0

Is np.sum(arr).dtype == np.uint :  False
Is np.sum(arr).dtype == np.uint :  False
```

Code #3:

 `# Python Program illustrating ` `# numpy.sum() method ` `      ` `import` `numpy as np ` `      ` `# 2D array  ` `arr ``=` `[[``14``, ``17``, ``12``, ``33``, ``44``],   ` `       ``[``15``, ``6``, ``27``, ``8``, ``19``],  ` `       ``[``23``, ``2``, ``54``, ``1``, ``4``,]]  ` `  ` `print``(``"\nSum of arr : "``, np.``sum``(arr)) ` `print``(``"Sum of arr(axis = 0) : "``, np.``sum``(arr, axis ``=` `0``)) ` `print``(``"Sum of arr(axis = 1) : "``, np.``sum``(arr, axis ``=` `1``))` ` ` `print``(``"\nSum of arr (keepdimension is True): \n"``,` `      ``np.``sum``(arr, axis ``=` `1``, keepdims ``=` `True``))`

Output:

```Sum of arr :  279
Sum of arr(axis = 0) :  [52 25 93 42 67]
Sum of arr(axis = 1) :  [120  75  84]

Sum of arr (keepdimension is True):
[[120]
[ 75]
[ 84]]```

Last Updated on October 28, 2021 by admin

## Multiplication of two Matrices in Single line using Numpy in PythonMultiplication of two Matrices in Single line using Numpy in Python

Multiplication of two Matrices in Single line using Numpy in Python Matrix multiplication is an

## numpy.percentile() in pythonnumpy.percentile() in python

numpy.percentile() in python numpy.percentile()function used to compute the nth percentile of the given data (array

## numpy.multiply() in Pythonnumpy.multiply() in Python

numpy.multiply() in Python numpy.multiply() function is used when we want to compute the multiplication of two

## Convert Python List to numpy ArraysConvert Python List to numpy Arrays

Convert Python List to numpy Arrays A list in Python is a linear data structure

## Numpy Meshgrid functionNumpy Meshgrid function

Numpy Meshgrid function The numpy.meshgrid function is used to create a rectangular grid out of

## Change data type of given numpy arrayChange data type of given numpy array

Change data type of given numpy array In this post, we are going to see

## numpy.subtract() in Pythonnumpy.subtract() in Python

numpy.subtract() in Python numpy.subtract() function is used when we want to compute the difference of two

## Numpy cheatsheetNumpy cheatsheet

This Numpy cheatsheet is a complete guide for learning Numpy, suitable for beginners to advanced