What is the usage of the dot function in Python?
In Python, the dot function is typically used to perform dot product operations between two objects. Dot product is a common operation in vectors and matrices, used to calculate either the scalar product between two vectors or the dot product between two matrices.
In the NumPy library, the np.dot function can be used to perform dot product operations. It takes two arrays as inputs and returns the dot product result between them.
An example of calculating the dot product of two vectors using np.dot.
import numpy as np
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
dot_product = np.dot(a, b)
print(dot_product)
The output result is: 32
An example using np.dot to calculate the dot product of two matrices:
import numpy as np
A = np.array([[1, 2], [3, 4]])
B = np.array([[5, 6], [7, 8]])
dot_product = np.dot(A, B)
print(dot_product)
The output result is:
[[19 22]
[43 50]]
Note: In the latest version of NumPy, you can also use the @ symbol to perform dot product calculations, for example dot_product = A @ B.