Dot product, cosine, and distance
Drag the tips of a and b. Three ways to compare two vectors update as you move them.
Dot product a · b6.50
Cosine similarity0.82
Angle35°
Distance ‖a − b‖1.80
Challenges
0 of 3 complete
- Get a dot product above 8 while the cosine similarity stays below 0.5. (not yet)
- Make the vectors point almost the same way (cosine above 0.95) but more than 3 apart. (not yet)
- Turn on Scale both to length 1 and set the cosine similarity to 0.50. Read the distance. (not yet)
Try this
- Make the angle 90 degrees. What happens to the dot product and the cosine?
- Choose Same direction, then drag b farther out. Which measures change?
- Turn on Scale both to length 1 and check that a smaller distance always means a larger cosine.
The three measures in NumPyPython with NumPy
import numpy as np
# The Similar example from the demo.
a = np.array([3.0, 1.0])
b = np.array([1.5, 2.0])
def compare(a, b):
dot = a @ b # multiply matching entries, add
cos = dot / (np.linalg.norm(a) * np.linalg.norm(b)) # divide out both lengths
angle = np.degrees(np.arccos(np.clip(cos, -1, 1)))
dist = np.linalg.norm(a - b) # straight-line distance
print(f"dot {dot:.2f}, cosine {cos:.2f}, angle {angle:.0f}°, distance {dist:.2f}")
compare(a, b)
compare(a, 3 * b) # b three times as long: same cosine, other numbers change
# Scaled to length 1, the dot product is the cosine and distance follows from it.
ua, ub = a / np.linalg.norm(a), b / np.linalg.norm(b)
compare(ua, ub)
print(f"2 - 2 cos = {2 - 2 * (ua @ ub):.4f}")
print(f"distance squared = {np.sum((ua - ub) ** 2):.4f}")dot 6.50, cosine 0.82, angle 35°, distance 1.80
dot 19.50, cosine 0.82, angle 35°, distance 5.22
dot 0.82, cosine 0.82, angle 35°, distance 0.60
2 - 2 cos = 0.3556
distance squared = 0.3556