Vectors and similarity
Turn a geometric idea into a useful comparison between items.
- Compute a dot product
- Interpret cosine similarity
- Handle a zero vector
A vector is an ordered representation
A vector is a sequence of numbers whose positions have consistent meaning. A product vector might describe price, size, and weight. Reordering those coordinates changes the representation. Similarity is only meaningful when vectors share the same feature definitions and scaling.
The dot product multiplies matching coordinates and adds the results. It becomes large when two vectors point in similar directions and have large magnitudes. Cosine similarity divides by both magnitudes, emphasizing direction rather than size.
Similarity is not truth
A high similarity score means closeness under a chosen representation. It does not prove that two documents agree, that a passage answers a question, or that an embedding captures every relevant distinction.
Cosine is undefined for a zero vector. A retrieval system needs an explicit policy, such as returning no match. Negative coordinates can produce negative cosine values; the mathematical range is from -1 to 1, not always 0 to 1.
A small experiment you can run.
Parallel positive vectors have cosine 1 even when their lengths differ. Orthogonal vectors have cosine 0. The function returns None for an undefined comparison.
import math
def cosine(a, b):
if len(a) != len(b):
raise ValueError("Dimensions must match")
denominator = math.sqrt(sum(x*x for x in a) * sum(y*y for y in b))
return sum(x*y for x, y in zip(a, b)) / denominator if denominator else None
print(round(cosine([1, 2, 0], [2, 4, 0]), 3))
print(round(cosine([1, 0], [0, 1]), 3))
print(cosine([0, 0], [1, 2]))
Save the file, open your terminal in that folder, and run python vectors-and-similarity.py. Use python3 or py if required by your installation. Setup guide
The script prints 1.0, 0.0, and None.
Explore the effect of scale.
- Multiply every coordinate of one vector by 10.
- Compare the dot product before and after.
- Compare cosine similarity before and after.
Compare with a suggested solution
The dot product increases by a factor of 10, while cosine stays the same for positive scaling. Scaling just one coordinate changes direction and can change cosine, which is why feature units matter.
One idea to take with you.
Make it part of your progress.
Finish the practice and answer the knowledge check to mark this lesson complete.
Go deeper with primary documentation
Optional references for further study. This lesson and its examples were written for Artificials.
Python math module