Cosine Similarity
Compute cosine similarity between two vectors.
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
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?
What is a good cosine similarity score?
What is cosine distance?
Can it return a negative value?
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 YouTubeTensorFlow 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.