numpy dot product
In this post, let's learn about the dot product of the Python programming language. Python provides the numpy.dot() function to return the dot product of two arrays.
NumPy is the fundamental package for scientific computing with Python. It is a highly optimised library for numerical operations. The support of NumPy makes the task easier. Several libraries, like OpenCV, SciPy, and Matplotlib, when combined with NumPy, increase the number of weapons in your arsenal.
Syntax of numpy.dot()
Here is the syntax of numpy.dot() method-
numpy.dot(x, y, out=None)
Here, x and y are two input arrays. Both arrays should be 1-D or 2-D. The out is the output argument for 1-D array scalar to be returned. It returns a dot product of two arrays, x and y. If both the arrays 'x' and 'y' are 1-D arrays, the dot() function performs the inner product of vectors and returns a scalar. If both the arrays 'x' and 'y' are 2-D arrays, the dot() function performs the matrix multiplication. If 'x' is an N-dimensional array and 'y' is a 1-dimensional array, then the dot() function performs the sum-product over the last axis of x and y. For N-dimensional arrays, it is a sum product over the last axis of x and the second-last axis of y.
Example1: Numpy Dot product of scalars
In the given example, we have taken two scalars and calculated their dot product using numpy.dot() function. It returns the multiplication of the two scalars.
import numpy as np
a = 30
b = 24
output = np.dot(a,b)
print(output)
Output of the above code-
720
Example2: numpy.dot() product of 1D array
In the given example, we have taken two numpy one-dimensional arrays and calculated their dot product using the numpy.dot() function. The output would be an inner product of these two vectors.
import numpy as np
#initialize arrays
A = np.array([5, 2, 9, 3])
B = np.array([6, 9, 1, 7])
#dot product
output = np.dot(A, B)
print(output)
Output of the above code-
78
Example3: numpy.dot() product of 1D array
Here is another example of numpy dot product of two one-dimensional arrays.
import numpy.matlib
import numpy as np
x = np.array([11,23])
y = np.array([31,25])
print(np.dot(x,y))
Output of the above code -
916
Example4: numpy.dot() product of 2D array
Here is an example of numpy.dot() of 2-D array. In this, we have taken two two-dimensional arrays and calculated their dot product using numpy.dot() function. The output would be a matrix multiplication of the two input arrays.
import numpy.matlib
import numpy as np
x = np.array([[11,23],[2,3]])
y = np.array([[31,25],[4,1]])
print(np.dot(x,y))
Output of the above code -
[[433 298]
[ 74 53]]
Example5: numpy.dot() product of 3D array
Here is an example of numpy.dot() of two 3-D arrays.
import numpy.matlib
import numpy as np
x = np.array([[2,4,6],[3,1,4],[8,5,3]])
y = np.array([[3,7,2],[2,4,7],[3,5,1]])
print(np.dot(x,y))
Output of the above code-
[[32 60 38]
[23 45 17]
[43 91 54]]
Example6: numpy.dot() product of 1D and 2D arrays
In this, we have taken one 1D and one 2D array and calculated their dot product using the numpy.dot() function.
import numpy.matlib
import numpy as np
x = np.array([5, 2])
y = np.array([[6, 2, -3],
[1, 1, 3]])
res = np.dot(x, y)
print(res)
Output of the above code-
[32 12 -9]
Example7: numpy.dot() product of complex vectors
In this, we have taken two complex vectors and calculated their dot product using the numpy.dot() function.
import numpy as np
vector_x = 5 + 2j
vector_y = 2 + 3j
product = np.dot(vector_x, vector_y)
print("Dot Product : ", product)
Output of the above code-
Dot Product : (4+19j)
Related Articles
Geometric Transformation OpenCV Python
Python convert dataframe to numpy array
Convert Python list to numpy array
Multiply all elements in list Python
Python NumPy: Overview and Examples
Python Numpy Array Shape
Convert Python list to numpy array
Python convert dataframe to numpy array
zip function in Python
NumPy program to copy data from a given array to another array
Multiply all elements in list Python
Remove element from list Python
Inverse of a matrix in Python
Python Contour Plot Examples
Python iterate list with index
Python add list to list
Prettytable in Python
Python dict inside list
Convert array to list Python
Python Matplotlib Bar Plot