Cosine Similarity

Compute cosine similarity between two vectors.

FreeNo accountOn your device

Runs in your browser. How to verify

Loading tool…

About this tool

Enter two vectors and get their cosine similarity: the cosine of the angle between them. 1 means the same direction, 0 means unrelated (orthogonal) and -1 means opposite. It also shows the cosine distance (1 minus the similarity).

How to use

1
Step 1
Enter Vector A and Vector B with commas between numbers.
2
Step 2
Make sure both have the same length.
3
Step 3
Press Calculate.

How it works

cos θ = (a · b) / (‖a‖ ‖b‖). It ignores the length of the vectors and compares direction only, which is why it is the usual measure for text embeddings. Vectors of different lengths give a dimension mismatch error.

cos θ = (a · b) / (‖a‖ ‖b‖)

Worked example

Two similar vectors

Input
A = 1, 0, 0, 1 B = 0.9, 0.1, 0, 1
Output
Cosine similarity 0.995871; cosine distance 0.004129
Walkthrough
a·b = 0.9 + 0 + 0 + 1 = 1.9, ‖a‖ = 1.4142 and ‖b‖ = 1.3491, so 1.9 / 1.9079 = 0.9959. The vectors (1, 0) and (0, 1) give exactly 0.

Limitations

  • Both vectors must have the same number of dimensions.
  • A zero vector has no direction, so its similarity is undefined.
  • Cosine similarity does not tell you about magnitude.

Questions

How do I calculate cosine similarity?
Enter two vectors of the same length and press Calculate.
What is a good cosine similarity score?
Closer to 1 means more similar. What counts as good depends on the embedding model.
What is cosine distance?
1 minus cosine similarity. The tool shows both.
Can it return a negative value?
Yes, down to -1 when the vectors point in opposite directions.

Features

  • Similarity and distance
  • Dimension check
  • Runs in your browser

Related resources

Guides and videos from around the web—opens on YouTube or the publisher’s site.

  • Mean, Variance and Standard Deviation

    StatQuest with Josh Starmer

    Stats foundations behind many ML metrics tools.

    Watch on YouTube
  • TensorFlow 2.0 Complete Course

    freeCodeCamp.org

    Practical ML stack context next to AI utilities.

    Watch on YouTube
  • DeepLearning.AI

    DeepLearning.AI

    Courses and explainers for modern AI tooling.

  • OpenAI Research

    OpenAI

    Research notes for people exploring AI tooling trends.