Numpy is one of the Powerful Python Data Science Libraries. It comes with a built-in robust Array data structure that can be used for many mathematical operations. The numpy library supports many methods and
 
  numpy.dot()
 
 is one of those. Using the numpy dot() method, we can calculate the dot product of two arrays. The numpy
 
  dot(array1,array2)
 
 method accepts two arrays as a parameter and returns their dot product or matrix multiplication.
Numpy dot() syntax
numpy.dot(array1, array2, out=None)
Parameters
 
  arrray1
 
 and
 
  array2
 
 represent the array-like structure. The
 
  out
 
 parameter represents the output argument. By default, its value is
 
  None,
 
 and if specified explicitly, it needs to be the exact kind of return output of the dot() method.
Return value
 The dot() product returns a
 
  
   ndarray.
  
 
Python numpy dot() method examples
Example1:
Python dot() product if both array1 and array2 are 1-D arrays.
>>> import numpy as np
>>> array1 = [1,2,3]
>>> array2 = [4,5,6]
>>> print(np.dot(array1, array2))
32
 If both the arrays are 1D, the
 
  dot()
 
 method performs the inner product between the arrays and returns the output as a number.
>>>1*4 + 2*5 + 3*6
>>>4+10+18
32
Example 2: Python dot() product if both array1 and array2 are 2-D arrays
>>> import numpy as np
>>> array1 = [[4,0], [1,-9]]
>>> array2 = [[8,0],[2,-18]]
>>> print(np.dot(array1, array2))
[[ 32 0]
[-10 162]]
If both arrays are 2D, the dot will perform the matrix multiplication between them.
>>>[[4*8 + 0*2, 4*0 + 0*-18 ]
 [1*8 + -9*2, 1*0 + -9*-18 ]
>>>[[32, 0]
    [-10, 162]]
 
  
   Note:
  
  As a matrix multiplication, the row size of
  
   array1
  
  must be equal to the column size of
  
   
    array2
   
  
  else, the dot() method throws a ValueError.
 
Example3:
Python dot() product if either of array1 or array2 is a 0-D(scalar) array
>>> import numpy as np
>>> array1 = 10
>>> array2 = [[8,0],[2,-18]]
>>> print(np.dot(array1, array2))
[[ 80 0]
[ 20 -180]]
 If one array of either is a 0-D array, the
 
  dot()
 
 multiply the 0-D array with the other array.
Summary
 The numpy
 
  dot()
 
 method finds out the product of two arrays based on their shape. Here are some important facts about
 
  dot(array1, array2)
 
 method of how it computes the product for different array shapes.
- 
  If
  
array1,andarray2are 1-D array, the dot() method performs inner product between both the arrays. - 
  If
  
array1andarray2are 2D arrays, the numpy dot() method performs matrix multiplication between them. - 
  If anyone between the
  
array1orarray2is a scalar or 1d array, the numpydot()method will multiply that 1d or scalar number with another array. - 
  If
  
array1is an N-D array andarray2is a 1-D array, then the numpydot()method will calculate the sum-product over the last axis ofarray1andarray2. - 
  If
  
array1is an N-D array andarray2is an M-D array (M>=2 ), then thedot()method will calculate the sum-product over the last axis ofarray1and second to the last axis ofarray2. 
People are also reading: