Find the sum and product of a NumPy array elements
Last Updated :
14 Dec, 2021
In this article, let’s discuss how to find the sum and product of NumPy arrays.
Sum of the NumPy array
Sum of NumPy array elements can be achieved in the following ways
Method #1: Using numpy.sum()
Syntax: numpy.sum(array_name, axis=None, dtype=None, out=None, keepdims=<no value>, initial=<no value>, where=<no value>)
Example:
Python3
import numpy as np
def main():
print ( 'Initialised array' )
gfg = np.array([[ 1 , 2 , 3 ], [ 4 , 5 , 6 ]])
print (gfg)
print (np. sum (gfg, axis = 1 ))
print (np. sum (gfg, axis = 0 ))
print (np. sum (gfg))
b = np.array([ 0 ])
print (np. sum (gfg, axis = 1 , out = b))
print (b)
print ( 'with axis parameter' )
print (np. sum (gfg, axis = 0 , keepdims = True ))
print (np. sum (gfg, axis = 1 , keepdims = True ))
print ( 'without axis parameter' )
print (np. sum (gfg, keepdims = True ))
print ( 'using initial parameter in sum function' )
print (np. sum (gfg, initial = 100 ))
print ( 'using where parameter ' )
print (np. sum (gfg, axis = 0 , where = [ True , False , False ]))
if __name__ = = "__main__" :
main()
|
Output:
Initialised array
[[1 2 3]
[4 5 6]]
[ 6 15]
[5 7 9]
21
[21]
[21]
with axis parameter
[[5 7 9]]
[[ 6]
[15]]
without axis parameter
[[21]]
using initial parameter in sum function
121
using where parameter
[5 0 0]
Note: using numpy.sum on array elements consisting Not a Number (NaNs) elements gives an error, To avoid this we use numpy.nansum() the parameters are similar to the former except the latter doesn’t support where and initial.
Method #2: Using numpy.cumsum()
Returns the cumulative sum of the elements in the given array.
Syntax: numpy.cumsum(array_name, axis=None, dtype=None, out=None)
Example:
Python3
import numpy as np
def main():
print ( 'Initialised array' )
gfg = np.array([[ 1 , 2 , 3 ], [ 4 , 5 , 6 ]])
print ( 'original array' )
print (gfg)
print (np.cumsum(gfg))
print (np.cumsum(gfg, axis = 1 ))
b = np.array([[ None , None , None ], [ None , None , None ]])
np.cumsum(gfg, axis = 1 , out = b)
print (b)
if __name__ = = "__main__" :
main()
|
Output:
Initialised array
original array
[[1 2 3]
[4 5 6]]
[ 1 3 6 10 15 21]
[[ 1 3 6]
[ 4 9 15]]
[[1 3 6]
[4 9 15]]
Product of the NumPy array
Product of NumPy arrays can be achieved in the following ways
Method #1: Using numpy.prod()
Syntax: numpy.prod(array_name, axis=None, dtype=None, out=None, keepdims=<no value>, initial=<no value>, where=<no value>)
Example:
Python3
import numpy as np
def main():
print ( 'Initialised array' )
gfg = np.array([[ 1 , 2 , 3 ], [ 4 , 5 , 6 ]])
print (gfg)
print (np.prod(gfg, axis = 1 ))
print (np.prod(gfg, axis = 0 ))
print (np.prod(gfg))
b = np.array([ 0 ])
print (np.prod(gfg, axis = 1 , out = b))
print (b)
print ( 'with axis parameter' )
print (np.prod(gfg, axis = 0 , keepdims = True ))
print (np.prod(gfg, axis = 1 , keepdims = True ))
print ( 'without axis parameter' )
print (np.prod(gfg, keepdims = True ))
print ( 'using initial parameter in sum function' )
print (np.prod(gfg, initial = 10 ))
print ( 'using where parameter ' )
print (np.prod(gfg, axis = 0 , where = [ True , False , False ]))
if __name__ = = "__main__" :
main()
|
Output:
Initialised array
[[1 2 3]
[4 5 6]]
[ 6 120]
[ 4 10 18]
720
[720]
[720]
with axis parameter
[[ 4 10 18]]
[[ 6]
[120]]
without axis parameter
[[720]]
using initial parameter in sum function
7200
using where parameter
[4 1 1]
Method #2: Using numpy.cumprod()
Returns a cumulative product of the array.
Syntax: numpy.cumsum(array_name, axis=None, dtype=None, out=None)axis = [integer,Optional]
Python3
import numpy as np
def main():
print ( 'Initialised array' )
gfg = np.array([[ 1 , 2 , 3 ], [ 4 , 5 , 6 ]])
print ( 'original array' )
print (gfg)
print (np.cumprod(gfg))
print (np.cumprod(gfg, axis = 1 ))
b = np.array([[ None , None , None ], [ None , None , None ]])
np.cumprod(gfg, axis = 1 , out = b)
print (b)
if __name__ = = "__main__" :
main()
|
Output:
Initialised array
original array
[[1 2 3]
[4 5 6]]
[ 1 2 6 24 120 720]
[[ 1 2 6]
[ 4 20 120]]
[[1 2 6]
[4 20 120]]
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...