Angles and Cosine Similarity
The dot product combines two ideas:
- how long the vectors are
- how much they point in the same direction
For two nonzero vectors and :
Here, is the angle between them.
This formula says the dot product is large when the vectors are long and point in a similar direction.
Direction Without Length
Sometimes we want to compare direction without rewarding length.
Cosine similarity does that:
For nonzero real vectors, the result is between -1 and 1.
1means the vectors point in the same direction.0means they are perpendicular.-1means they point in opposite directions.
Drag the vectors. When they point more similarly, the dot product tends to grow. Cosine similarity turns that into a direction comparison by dividing out both lengths.
If and , what is ?
Compute it first, then check your number.
HintMultiply matching entries
Compute .
SolutionZero dot product
For these two nonzero vectors, the zero dot product means they are perpendicular.
Same Direction, Different Length
Consider:
a = [1, 0]
b = [5, 0]
The dot product is:
The lengths are:
So:
Cosine similarity says they point in the same direction, even though one vector is longer.
What is the cosine similarity between and ?
Compute it first, then check your number.
HintSame ray
Both vectors point along the positive horizontal axis.
SolutionLength cancels
First compute the dot product and the two lengths:
The longer vector has length 5, but the denominator also contains that
length. After division, only direction remains. Both vectors point along
the positive horizontal axis, so the cosine similarity is 1.
Opposite Direction
If:
a = [1, 0]
b = [-2, 0]
then points exactly opposite to .
The dot product is -2, and the product of the lengths is 2, so the cosine
similarity is:
What is the cosine similarity between and ?
Compute it first, then check your number.
HintCompute the sign
.
SolutionOpposite direction
The vectors lie on the same line, but they point in opposite directions. The numerator keeps that sign:
Lengths are never negative, so the negative result comes from the dot
product. A cosine similarity of -1 means exactly opposite direction.
Why This Matters
Embeddings are often compared by direction.
Two sentence embeddings can have different lengths but still point in a similar direction. Cosine similarity helps compare the pattern of coordinates rather than only their size.
This does not mean length is useless. Some systems may use vector length to store confidence, frequency, or another signal. Cosine similarity is useful when direction is the signal we want to compare.
Enter 1 if cosine similarity is designed to compare direction while reducing
the direct effect of vector length.
Compute it first, then check your number.
HintLook at the denominator
The denominator is .
SolutionLength normalization
Enter 1. Cosine similarity computes:
Dividing by the lengths makes the result focus on direction.
Code Mirror
import numpy as np
a = np.array([1, 0])
b = np.array([0, 1])
cosine = np.dot(a, b) / (np.linalg.norm(a) * np.linalg.norm(b))
print(cosine)
The output is:
0.0
The vectors are perpendicular.
Next, we ask how much of one vector lies along another.