Vector similarity explorer
Two vectors can be close in three different ways. Their directions can match, their tips can sit near each other, or they can point the same way and both be long. Cosine similarity, Euclidean distance and the dot product each measure a different one of these.
a and b are 45° apart, so their cosine similarity is 0.707.
a compared with b
a · b = |a| |b| cos θ = 3.162 × 2.236 × 0.707 = 5. Stretch b and the dot product follows its length; the cosine does not move.
Vectors
Drag a round handle on the plane, or focus it and press the arrow keys (Shift for bigger steps). Each vector is compared with a.
Divides each vector by its length before comparing. Dot product and cosine then agree.
Ranking demo: query and candidates
Which candidate is closest to the query?
| Candidate | Length | Dot product (higher wins) | Cosine (higher wins) | Distance (lower wins) |
|---|---|---|---|---|
| 1: (4, 4) | 5.66 | 16 1st | 0.894 2nd | 3.16 3rd |
| 2: (3, 0.5) | 3.04 | 9.5 2nd | 0.988 1st | 0.5 1st |
| 3: (1, 2.5) | 2.69 | 5.5 3rd | 0.646 3rd | 2.5 2nd |
Dot product puts candidate 1 first, cosine puts candidate 2 first and distance puts candidate 2 first. Dot product grows with a vector’s length, cosine looks only at direction, and distance looks at where the tips are. Try making the candidates the same length: the rankings come together.
What it shows
Cosine similarity is the dot product divided by the two lengths. It ignores length and looks only at direction: 1 means the same direction, 0 a right angle and −1 opposite directions. The dot product keeps the lengths, so a longer vector scores higher. Euclidean distance is the straight-line gap between the tips.
Which measure ranks results, for example text embeddings in a search index, is a modelling choice, and the three can disagree. Scale every vector to length 1 and they stop disagreeing: the dot product becomes the cosine, and the squared distance is 2 − 2 × cosine.
How to read it
- Draw in 2D: drag a round handle, or focus it and press the arrow keys (Shift for bigger steps). Each other vector is compared with a.
- Cosine similarity is the headline number. The angle is the same information in degrees.
- Stretch b and the dot product follows its length while the cosine stays put.
- “Normalise to unit length” rescales each vector to length 1. Dashed arrows show where they were.
- In the ranking demo, badges give each candidate’s place under each measure. When they differ, the note below names each measure’s pick.
- Paste vectors compares two vectors of any length, up to 4,096 numbers.
Questions
When should I use cosine similarity instead of the dot product?
Use cosine when only direction should matter and vector length is an accident, such as raw word counts where a longer document has bigger counts. Use the dot product when length carries meaning that the model was trained to use. Check the documentation for your embedding model: many are trained for one measure and ranked best with it.
Is cosine similarity the same as Euclidean distance?
Not in general. They give the same ranking only when every vector has length 1, because then the squared distance is 2 − 2 × cosine. For vectors of different lengths, the nearest point and the most similar direction can be different candidates.
Can cosine similarity be negative?
Yes. It runs from −1 to 1. A negative value means the angle between the vectors is more than 90°, and −1 means they point in exactly opposite directions.
What happens with a zero vector?
The cosine and angle are undefined, because a vector of length 0 has no direction. The tool shows “Undefined” for them. The dot product is 0, and the distance is the length of the other vector.
Does normalising change the ranking?
It can. Cosine ranking is unchanged by normalising, since cosine already divides by the lengths. Dot product and distance rankings can change, and after normalising they all agree with the cosine ranking.
Learn it properly
Sources
Explore further
- Embedding ProjectorGoogle’s tool for exploring embeddings in 3D and finding nearest neighbours by cosine distance.
Last updated
More tools
All tools- explorerMatrix transform visualiserSee a 2×2 matrix stretch, turn, flip or squash the plane, with its determinant and eigenvectors.Open tool
- explorerProbability distribution explorerPlot six common distributions, with mean, variance, mode and the probability of any interval.Open tool
- simulatorBackprop step-throughStep through one forward and backward pass of a tiny network, with the chain rule and real numbers at every node.Open tool