# Numpy sum()

Numpy

``numpy.sum(a,axis=None,dtype=None,out=None,keepdims, initial, where)``
Return sum of elements across given axis.
 `a` array, elements to get the sum value `axis` Int (optional ), or tuple, default is None, will sum all the elements. If axis given then across the axis is returned. `dtype` data-type( Optional ), Data Type of returned array or value. `out` Optional. If given then output to be stored. Must be of same time as of the output `keepdims` Bool ( Optional ), output matches to the input array dimension. `where` Optional, Elements to include for calculation of Sum `initial` Optional, int, Initial value of sum. This value will be added to our final output
We will use these parameters in our examples.

## Sample array

You can use randint() to create an array for our examples. Or can use fixed elements to create the array.
``````import numpy as np
# my_data=np.random.randint(2,high=7,size=(3,3),dtype='int16')
my_data=np.array([[6, 3, 2], [2, 6, 2], [6, 2, 3]])
print(my_data)``````
Output
``````[[6 3 2]
[2 6 2]
[6 2 3]]``````

## Axis

Sum of the elements across the axis.
``````print("sum()      : ", my_data.sum())
print("sum(axis=0):", my_data.sum(axis=0))
print("sum(axis=1):", my_data.sum(axis=1))``````
Output
``````sum()      :  32
sum(axis=0): [14 11  7]
sum(axis=1): [11 10 11]``````

## dtype

The data type of the output. By default the output will have the dtype of input array.
``````print("sum(axis=1,dtype=np.int8) : ", my_data.sum(axis=1,dtype=np.int8))
print("sum(axis=1,dtype=np.int32) : ", my_data.sum(axis=1,dtype=np.int32))
print("sum(axis=1,dtype=np.float64) : ", my_data.sum(axis=1,dtype=np.float64))
print("sum(axis=1,dtype=np.complex128) : ", my_data.sum(axis=1,dtype=np.complex128))``````
Output
``````sum(axis=1,dtype=np.int8) :  [11 10 11]
sum(axis=1,dtype=np.int32) :  [11 10 11]
sum(axis=1,dtype=np.float64) :  [11. 10. 11.]
sum(axis=1,dtype=np.complex128) :  [11.+0.j 10.+0.j 11.+0.j])``````

## keepdims

If it is set to True ( keepdims=True ) then it will take the dimension of input array.
``````print("sum(keepdims=True) : ", my_data.sum(keepdims=True))
print("sum(keepdims=False) : ", my_data.sum(keepdims=False))``````
Output
``````sum(keepdims=True) :  [[43]]
sum(keepdims=False) :  43``````

## out

Alternative output array, must be of same shape as expected output. Let us first check with axis.
``````x = np.zeros(3,dtype=int)
print(my_data.sum(axis=0,out=x))
print(x)``````
Output
``````[14 11  7]
[14 11  7]``````
Without using axis
``````y = np.array(1)
print(my_data.sum(out=y))
print(y)``````
Output
``````32
32``````

## Using where

By using where we can say which elements to use and which elements not to use ( by setting True or False ) .
``print(my_data.sum(where=[True, False,True]))``
Output
``21``
Using axis with where
``````print(my_data.sum(axis=1,where=[True, False,True]))
print(my_data.sum(axis=0,where=[True, False,True]))``````
Output
``````[8 4 9]
[14  0  7]``````

## initial

``print(my_data.sum(initial=10))``
Output
``42``
Subscribe to our YouTube Channel here

## Subscribe

* indicates required
Subscribe to plus2net

plus2net.com