What is Cosine Similarity
Cosine Similarity - Similarity measure
Cosine similarity measures how closely two vectors point in the same direction: their dot product divided by the product of their lengths, from -1 to 1. It is the most common way to compare embeddings in vector search, alongside dot product and Euclidean (L2) distance.
How It Works
Cosine similarity measures the cosine of the angle between two vectors, providing a metric for similarity based on direction rather than magnitude. It's commonly used in vector space models to compare document similarity.
Technical Details
Calculated as the dot product of two vectors divided by the product of their magnitudes. Values range from -1 (completely dissimilar) to 1 (completely similar), with 0 indicating orthogonality.
Cosine vs Dot Product vs Euclidean (L2) vs Manhattan (L1)
- Cosine similarity: dot(a, b) / (|a| × |b|); compares direction only, from -1 to 1
- Dot product: Σ a_i × b_i; direction and length together, so longer vectors score higher
- Euclidean (L2) distance: √Σ (a_i - b_i)²; straight-line distance, lower is closer
- Manhattan (L1) distance: Σ |a_i - b_i|; sum of per-dimension differences, less sensitive to one large difference
- Example with a = (1, 0, 1) and b = (0, 1, 1): dot = 1, cosine = 1 / (1.414 × 1.414) = 0.5, L2 = 1.414, L1 = 2
- For vectors normalized to length 1, cosine, dot product and L2 rank results in the same order, since L2² = 2 - 2 × cosine
- Use the metric the embedding model was trained with; most text and image embedding models specify cosine or dot product
Distance Metrics in Mixpeek
Each vector index in a Mixpeek collection has one distance metric. Built-in extractors set it for you; a custom extractor declares it as `distance_metric` in its manifest, with `"cosine"`, `"euclid"` or `"dot"`. Manhattan (L1) is not one of the options.
Best Practices
- Normalize vectors before computing similarity
- Use for high-dimensional data comparisons
- Combine with other metrics for comprehensive analysis
- Optimize vector storage for performance
- Regularly assess similarity thresholds
Common Pitfalls
- Ignoring vector normalization
- Over-relying on cosine similarity alone
- Inadequate threshold tuning
- Poor vector storage optimization
- Lack of comprehensive similarity analysis
Advanced Tips
- Implement hybrid similarity measures
- Use dimensionality reduction for efficiency
- Consider domain-specific similarity adjustments
- Optimize for specific use cases
- Regularly review similarity performance
Put multimodal search to work
Connect a bucket and Mixpeek runs the whole multimodal search pipeline for you: extraction, indexing, and search over your own objects. No models to wire up, nothing to host.
Start with ManagedAlready have vectors?
Keep your embeddings on your own cloud and run dense, sparse, and BM25 search directly on object storage. From $25/mo.
Start with MVS