What Is Cosine Similarity?
Cosine similarity measures how closely two vectors point in the same direction, ignoring their lengths. It equals the dot product of the vectors divided by the product of their magnitudes, and ranges from −1 (opposite) through 0 (perpendicular) to 1 (same direction).
The calculator also returns the angle between the vectors in degrees, the cosine distance (1 − similarity), the dot product, both magnitudes and the Euclidean distance for contrast. Because cosine similarity ignores magnitude, two vectors that differ only in scale score 1.
Below you will find the formula, a variables table, a worked example and answers to common questions such as cosine similarity versus Euclidean distance.
How to Use the Cosine Similarity Calculator
- Enter the numbers for vector A and vector B with the same number of values.
- Read the cosine similarity and the plain-English description.
- Use the component table to see which dimensions contribute most to the dot product.
- To compare documents, build term-frequency vectors over the same vocabulary first.
Cosine Similarity Formula
Cosine similarity
Cosine distance
Angle
Cosine Similarity Variables Explained
| Symbol | Meaning | Notes |
|---|---|---|
| A, B | The two vectors | Same number of dimensions |
| A·B | Dot product | Σ AᵢBᵢ |
| ‖A‖, ‖B‖ | Vector magnitudes | √(Σ Aᵢ²) |
| cos θ | Cosine similarity | A·B ÷ (‖A‖‖B‖) |
Cosine Similarity Worked Example
Using the example values (press “Load example” above to try them):
- Dot product A·B = 3×2 + 45×54 + 7×13 + 2×15 = 2557.
- Lengths: ‖A‖ = 45.6837, ‖B‖ = 57.5674.
- cos θ = 2557 ÷ (45.6837 × 57.5674) = 0.9722843.
- θ = arccos(0.9722843) = 13.521°.
Result: Cosine similarity = 0.9722843; Angle between vectors = 13.521°.
How to read the similarity score
What counts as “similar” depends on the data. With sparse text vectors, 0.3 can already be a strong match, while dense embeddings from language models often score 0.7 or more for unrelated texts. Always calibrate against known similar and dissimilar pairs.
| Cosine | Angle | Meaning |
|---|---|---|
| 1 | 0° | Same direction |
| 0.8 to 1 | under 37° | Very similar |
| 0.5 to 0.8 | 37° to 60° | Moderately similar |
| about 0 | 90° | Orthogonal, unrelated |
| below 0 | over 90° | Opposing directions |
| −1 | 180° | Exactly opposite |
Cosine vs Euclidean distance vs Pearson correlation
Pearson correlation is cosine similarity after subtracting each vector’s mean, so it ignores shifts as well as scale. If the vectors have different baselines, centre them first.
| Measure | Sensitive to length | Sensitive to shift | Typical use |
|---|---|---|---|
| Cosine similarity | No | Yes | Text and embedding similarity |
| Euclidean distance | Yes | Yes | Clustering in physical space |
| Pearson correlation | No | No | Linear relationship between variables |
Using cosine similarity for text
Represent each document as a vector of word counts or TF-IDF weights over a shared vocabulary, then compare vectors. Because document length scales the counts but not the direction, a long and a short document on the same topic score high. TF-IDF weighting further reduces the influence of very common words.
Where Cosine Similarity Is Used
Comparing text vectors
Scoring how similar two documents are when each is represented as a vector of term counts.
Comparing embeddings
Measuring closeness of two feature or embedding vectors by direction.
Finding the angle
Getting the angle between two vectors from their coordinates.
Linear algebra practice
Checking dot-product and magnitude calculations.
Common Cosine Similarity Mistakes to Avoid
- Both vectors must be in the same order and have the same dimensions.
- A zero vector has no direction, so similarity is undefined.
- Cosine similarity ignores magnitude; if length matters, also compare norms or use Euclidean distance.
- Mean-centre the vectors if you care about correlation rather than raw direction.
Cosine Similarity Terms and Definitions
- Dot product
- Sum of the products of matching components.
- Norm
- The length of a vector, the square root of the sum of squares.
- Orthogonal
- At a right angle; cosine similarity of zero.
Cosine Similarity FAQs
What is cosine similarity?
It is the cosine of the angle between two vectors. It measures how similar their directions are, independent of their lengths.
What range does cosine similarity take?
From −1 to 1. With non-negative data such as word counts it ranges from 0 to 1.
What is cosine distance?
One minus the cosine similarity. It is 0 for identical directions and 2 for opposite ones. It is not a true metric because it does not satisfy the triangle inequality.
Why use cosine similarity for text?
It ignores document length, so a long and a short document about the same topic can still be judged similar.
How is it different from correlation?
Pearson correlation is the cosine similarity of mean-centred vectors, so it also ignores shifts in the baseline.
Can I compare vectors of different lengths?
Not directly. Both vectors must live in the same space, with the same number of dimensions in the same order.