Dot Product
The dot product is fundamental in linear algebra. Think of it like multiplying corresponding elements and summing them up. It's everywhere in machine learning and physics.
1D Arrays (Vectors)
For 1D arrays, dot product is sum of element-wise products.
import numpy as np
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
result = np.dot(a, b)
print(f"Dot product: {result}")
1*4 + 2*5 + 3*6 = 4 + 10 + 18 = 32. That's the dot product.
Using @ Operator
The @ operator is a more readable alternative.
import numpy as np
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
result = a @ b
print(f"Dot product with @: {result}")
Here is the thing - @ is just syntactic sugar for np.dot(). Use whichever you prefer.
Geometric Interpretation
Dot product relates to the angle between vectors.
import numpy as np
a = np.array([1, 0])
b = np.array([0, 1])
c = np.array([1, 1])
print(f"Perpendicular: {np.dot(a, b)}")
print(f"Parallel: {np.dot(c, c)}")
Perpendicular vectors have dot product 0. Parallel vectors have maximum dot product.
One thing that confused me at first was why dot product matters. It's used in similarity calculations, projections, and neural networks.
Try it Yourself →Key Takeaways
- np.dot() computes dot product of two arrays
- The @ operator is equivalent to np.dot()
- For 1D arrays: sum of element-wise products
- Dot product measures similarity between vectors