Cosine Similarity
Calculator to compute cosine similarity with formulas and examples
Cosine Similarity Calculator
What is calculated?
The cosine similarity measures the similarity between two vectors by the cosine of the angle between them. Values near 1 indicate high similarity, near 0 orthogonality.
Cosine Info
Properties
Cosine similarity:
- Range: [-1, 1]
- 1 = identical direction
- 0 = orthogonal vectors
- -1 = opposite direction
Direction-based: Vector magnitude is ignored — only direction matters.
Special cases
cos(0°) = 1 (maximum similarity)
cos(90°) = 0 (no similarity)
cos(180°) = -1 (opposite)
Related measures
→ Euclidean distance
→ Correlation coefficient
→ Manhattan distance
Formulas for Cosine similarity
Similarity formula
Distance formula
Dot product
Euclidean norm
Angle relation
Normalized vectors
Detailed calculation example
Example: compute Cosine([3,5], [0,3])
Given:
- x = [3, 5]
- y = [0, 3]
Step 1 - Dot product:
Step 2 - Norms:
Step 3 - Similarity:
Step 4 - Cosine distance:
Interpretation: The vectors have an angle of about 31° and are relatively similar (small distance).
Text analysis example
Example: Document similarity with TF-IDF
Document A:
"Cat sits on mat"
TF-IDF: [0.5, 0.3, 0.2, 0.0, 0.0]
Document B:
"Dog lies on sofa"
TF-IDF: [0.0, 0.0, 0.3, 0.4, 0.3]
Calculation:
Result: Low similarity due to few shared terms (only "on").
Geometric interpretation
Angle and similarity
0° (parallel)
Identical direction
45°
High similarity
90° (orthogonal)
No similarity
180° (antiparallel)
Opposite
Note: Cosine similarity ignores vector length and focuses only on direction.
Practical applications
Information Retrieval
- Document similarity
- Search engine ranking
- TF-IDF comparisons
- Semantic search
Recommender systems
- User-item matrices
- Collaborative filtering
- Product recommendations
- Netflix-style algorithms
Machine Learning
- Feature comparisons
- Clustering algorithms
- Neural networks
- Similarity learning
Mathematical properties
Similarity properties
- Range: [-1, 1]
- Symmetry: sim(x,y) = sim(y,x)
- Self-similarity: sim(x,x) = 1
- Direction-based: Ignores magnitude
Geometric properties
- Angle measure: Cosine of the enclosed angle
- Projection-based: Uses the dot product
- Normalization-invariant: Independent of vector lengths
- Linearity: Linear with respect to dot product
Important notes
Zero vectors: Cosine is undefined if one of the vectors is the zero vector
Scaling: Multiplying by positive scalars does not change similarity
Comparison: Cosine vs. Pearson correlation
For vectors [1,2,3] and [2,4,6]
Cosine similarity:
Identical direction (perfect similarity)
Pearson correlation:
Perfect linear correlation
Difference: Cosine ignores the mean while Pearson measures deviations from the mean.