← Back to playgrounds
7 min read

Explore cosine similarity

Select word vectors, inspect the calculation, and see why direction matters more than length.

Cosine similarity compares the direction of two vectors. It is widely used in semantic search because two pieces of text can point in a similar direction even when their vector lengths differ.

This playground uses a small set of four-dimensional word vectors with named features such as natural versus artificial and safe versus dangerous. The vectors are hand-authored so the calculation is easy to inspect. A real embedding model learns hundreds or thousands of dimensions from data, and people usually cannot assign a simple label to each one.

The angle diagram is reconstructed from the cosine score across all four dimensions. It is not a two-dimensional projection that drops part of the calculation.

Try this:

  1. Start with puppy and dog. Their feature patterns point in nearly the same direction, so the cosine score is close to 1.
  2. Choose contrasting weather. Sunshine and blizzard disagree across several dimensions, producing a negative score and an angle greater than 90 degrees.
  3. Inspect each product. Positive products increase alignment; negative products pull the score down.
  4. Scale the second vector. The dot product and magnitude change, but cosine similarity remains fixed because the direction does not change.
  5. Open “A useful warning.” Wolf and storm score highly in this toy space even though they are not synonyms. Cosine compares vectors, so the embedding model must create useful vectors before the metric can produce useful rankings.

1. Choose two word vectors

Compare curated words, then inspect why their directions receive that score.

Cosine similarity0.000

Angle
Cosine distance

The diagram reconstructs the exact angle from all four dimensions. It is not a two-dimensional projection of the word data.

2. Follow the calculation

Multiply matching dimensions, add the products, then divide by both vector lengths.

DimensionABProduct

3. Change length without changing direction

Scale the second vector. Its magnitude and dot product change, but cosine similarity stays the same.

Scaled vector
Dot product
Magnitude
Cosine similarity