Change language

Numpy.prod () in Python

|

numpy.prod () returns the product of array elements along a given axis. 
Syntax :

  numpy.prod (a, axis = None, dtype = None, out = None, keepdims =)  

parameters
a: array_like
This is the input. 
axis: none or int or a tuple of integers, it is optional
This is the axis or axes along which the product is produced. The default axis is None, it will calculate the product of all elements of the input array. If the axis is negative, it is counted from the last to the first axis. 
If the axis is a tuple of integers, the product is performed along all axes specified in the tuple, not along one axis or all axes, as before. 
dtype: dtype, optional
This is the type of the returned array as well as the accumulator that the elements are multiplied by. The d type for a is the default, unless a has an integer dtype with less precision than the platform’s default integer. In this case, if a is signed, then the platform integer is used, while if a is unsigned, then an unsigned integer of the same precision is used as the platform integer. 
out: ndarray, optional
Alternative output array for placing the result. It should have the same shape as the expected result, but the type of the output values ​​will be cast as needed. 
keepdims: bool, optional
If this parameter is set to True, axes that are downsized remain in the result as dimensions with size one. With this option, the result will translate correctly with respect to the input array.

Example 1

# Program Python illustrating
# working product ()

 

import numpy as geek 

array1 = [ 1 , 2 ]

  
# using the function

array2 = np.prod (array1)

 

 print ( "product" , array2)

Output:

 2.0 

Example 2
2d array

import numpy as geek 

array1 = [[ 1. , 2. ], [ 3. , 4. ]]

 
# function application

array2 = np.prod (array1)

  

print ( " product " , array2)

Output:

 24.0 

Example 3
The product of an empty array will be neutral element 1:

import numpy as geek 

array1 = []

 
# using the function

array2 = np.prod (array1)

 

print ( " product " , array2)

Exit :

 1 

Example 4
Specifying the axis along which we multiply

import numpy as geek 

array1 = [[ 1 , 2 ], [ 3 , 4 ]]

 
# applying the function

array2 = np.prod (array1, axis = 1  )

 

print ( " product " , array2)

Output:

 [2, 12] 

Example 5
If type x is unsigned, the output type will be an unsigned integer platform

import numpy as geek 

x = np.array ([ 1 , 2 , 3 ], dtype = np.uint8)

  
# function application

np .prod (x) .dtype = = np.uint

Output:

 True 

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

News


Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method