Step-by-Step Explanation:
Importing NumPy:
import numpy as np- This imports the NumPy library, which provides support for working with arrays and performing mathematical operations like dot products.
Creating Arrays:
a = np.array([1, 2, 3, 4])b = np.array([4, 3, 2, 1])- Two 1D NumPy arrays a and b are created:
- a = [1, 2, 3, 4]
b = [4, 3, 2, 1]
- Two 1D NumPy arrays a and b are created:
Dot Product Calculation:
np.dot(a, b)The dot product of two 1D arrays is calculated as:
dot product=a[0]⋅b[0]+a[1]⋅b[1]+a[2]⋅b[2]+a[3]⋅b[3]Substituting the values of a and b:
dot product=(1⋅4)+(2⋅3)+(3⋅2)+(4⋅1)Perform the calculations:
dot product=4+6+6+4=20
Printing the Result:
print(np.dot(a, b))- The result of the dot product, 20, is printed to the console.
Final Output:
20
Key Points:
- The dot product of two vectors is a scalar value that represents the sum of the products of corresponding elements.
- In NumPy, np.dot() computes the dot product of two 1D arrays, 2D matrices, or a combination of arrays and matrices.
0 Comments:
Post a Comment