Machine Learning Engineeringexplorer

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.

Input
a compared with b

a and b are 45° apart, so their cosine similarity is 0.707.

Cosine similarity0.707a and b
ab45°
Vector a: (3, 1)Vector b: (1, 2)

a compared with b

Cosine similarity0.7071 means the same direction, 0 at right angles, −1 opposite.
Dot product5Lengths 3.16 and 2.24
Euclidean distance2.236Straight-line gap between the tips.
Angle45°Between the two vectors.
How the three numbers relate

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

Vector a
Vector b

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

Query
Candidate 1
Candidate 2
Candidate 3

Which candidate is closest to the query?

Candidates ranked against the query by dot product, cosine and distance
CandidateLengthDot product (higher wins)Cosine (higher wins)Distance (lower wins)
1: (4, 4)5.6616 1st0.894 2nd3.16 3rd
2: (3, 0.5)3.049.5 2nd0.988 1st0.5 1st
3: (1, 2.5)2.695.5 3rd0.646 3rd2.5 2nd
The rankings disagree

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

Your first senior-level
lesson is free.

Choose a course, open a free lesson, and explore an idea you’ve wanted to understand.