Math

Cosine Similarity Calculator

Calculate the cosine similarity between two vectors instantly

Cosine Similarity Calculator

Cosine similarity measures the angle between two vectors in multi-dimensional space, returning a value between -1 and 1. A value of 1 means the vectors point in exactly the same direction, 0 means they are orthogonal (no similarity), and -1 means they point in opposite directions. It is one of the most commonly used similarity metrics in machine learning, NLP, and information retrieval because it is invariant to vector magnitude — only direction matters.

How to Use This Calculator

  1. Enter Vector A — comma-separated or space-separated numbers representing the first vector.
  2. Enter Vector B — the second vector. It must have the same number of dimensions as Vector A.
  3. The calculator instantly shows the cosine similarity, dot product, magnitudes, and the angle in degrees and radians.

Cosine Similarity Formula

For two vectors A and B:

cos(θ) = (A · B) / (|A| × |B|)

Where A · B is the dot product (sum of element-wise products) and |A|, |B| are the Euclidean magnitudes (L2 norms).

Common Interpretation Ranges

  • 0.9 to 1.0: Very high similarity — vectors are nearly co-directional.
  • 0.7 to 0.9: High similarity — commonly interpreted as semantically related in NLP tasks.
  • 0.5 to 0.7: Moderate similarity.
  • 0 to 0.5: Low similarity.
  • 0: Orthogonal — completely unrelated directions.
  • Negative: Vectors are more opposite than similar.

Real-World Applications

Word embeddings: Word2Vec and GloVe represent words as dense vectors. The cosine similarity between "king" and "queen" is high because they appear in similar contexts.

Document similarity: Two TF-IDF vectors can be compared with cosine similarity to find the most relevant documents for a search query.

Recommendation systems: User preference vectors are compared using cosine similarity to find users with similar taste.

Image retrieval: CNN feature embeddings are compared using cosine similarity to find visually similar images.

Cosine Similarity vs. Euclidean Distance

Euclidean distance is sensitive to vector magnitude — a short vector and a long vector pointing in the same direction have a large Euclidean distance. Cosine similarity ignores magnitude and only measures angular distance. This makes it ideal for text analysis where document length should not affect similarity scores.

Frequently Asked Questions

What does a cosine similarity of 1 mean?
A cosine similarity of 1 means the two vectors point in exactly the same direction. They may differ in magnitude but are perfectly aligned. In NLP, this indicates two documents or word vectors are as similar as possible.
Can cosine similarity be negative?
Yes. Cosine similarity ranges from -1 to 1. A value of -1 means the vectors point in exactly opposite directions. This is common in signed embedding spaces but rare in NLP applications where TF-IDF vectors are always non-negative.
What is cosine distance?
Cosine distance is defined as 1 minus cosine similarity. It converts the similarity score to a proper distance metric where 0 means identical direction and 2 means completely opposite. It is used when you need a distance rather than a similarity score.
Does cosine similarity care about vector length?
No. Cosine similarity is magnitude-invariant. Doubling one vector does not change the similarity score because both the dot product and the magnitudes scale proportionally. This is why it is preferred for document comparison where document length varies.
Why must the two vectors have the same number of dimensions?
The dot product and magnitude formulas require element-wise operations between vectors of equal length. If vectors have different dimensions, the operation is mathematically undefined. In practice, embed representations always share the same embedding dimension.
What is the difference between cosine similarity and Pearson correlation?
Pearson correlation is cosine similarity applied to mean-centered vectors. It measures linear relationship strength. Cosine similarity does not center the vectors, so it measures raw directional alignment. For zero-mean vectors they are equivalent.