cosine similarity

**Cosine Similarity** is the **standard metric for measuring semantic similarity between text embeddings in NLP** — calculating the cosine of the angle between two vectors in high-dimensional space, where a value of 1.0 means the vectors point in exactly the same direction (semantically identical), 0.0 means they are orthogonal (unrelated), and -1.0 means they are opposite, used in every major AI application from semantic search to RAG retrieval to recommendation systems. **What Is Cosine Similarity?** - **Definition**: A measure of similarity between two non-zero vectors that calculates the cosine of the angle between them — producing a value between -1 and 1, independent of vector magnitude (length). - **Formula**: $ ext{Sim}(A, B) = frac{A cdot B}{|A| |B|} = frac{sum_{i=1}^{n} A_i B_i}{sqrt{sum_{i=1}^{n} A_i^2} cdot sqrt{sum_{i=1}^{n} B_i^2}}$ - **Key Property**: Cosine similarity measures direction, not magnitude — a 1,000-word article about "machine learning" and a 50-word tweet about "machine learning" can have high cosine similarity because their embedding vectors point in the same direction, even though the vectors have different magnitudes. **Interpretation** | Value | Angle | Meaning | Example | |-------|-------|---------|---------| | **1.0** | 0° | Identical direction | "I love cats" vs "I love cats" | | **0.8-0.99** | ~15-35° | Very similar | "I love cats" vs "I adore felines" | | **0.5-0.8** | ~35-60° | Somewhat related | "I love cats" vs "Pets are great companions" | | **0.0** | 90° | Unrelated | "I love cats" vs "The stock market crashed" | | **-1.0** | 180° | Opposite | Rare in practice with modern embeddings | **Why Cosine Similarity Over Euclidean Distance?** | Property | Cosine Similarity | Euclidean Distance | |----------|------------------|-------------------| | **Magnitude-invariant** | Yes — long and short documents compare fairly | No — penalizes different lengths | | **Range** | [-1, 1] — bounded and interpretable | [0, ∞) — unbounded | | **Use case** | Text similarity, embeddings, NLP | Physical distance, spatial data | | **High dimensions** | Works well in 384-1536D embedding space | Suffers from "curse of dimensionality" | **Python Implementation** ```python import numpy as np from sentence_transformers import SentenceTransformer model = SentenceTransformer('all-MiniLM-L6-v2') emb_a = model.encode("I love machine learning") emb_b = model.encode("AI and deep learning fascinate me") cosine_sim = np.dot(emb_a, emb_b) / (np.linalg.norm(emb_a) * np.linalg.norm(emb_b)) print(f"Similarity: {cosine_sim:.3f}") # ~0.85 ``` **Applications in AI** | Application | How Cosine Similarity Is Used | |-------------|------------------------------| | **Semantic Search** | Query embedding vs document embeddings → rank by similarity | | **RAG Retrieval** | Find most similar chunks to the question | | **Duplicate Detection** | Flag document pairs with similarity > 0.95 | | **Recommendation** | "This article is similar to articles you've read" | | **Clustering** | Group items by similarity threshold | **Cosine Similarity is the fundamental distance metric powering modern NLP and AI search** — providing a magnitude-invariant, bounded measure of semantic relatedness between text embeddings that enables every retrieval, search, and similarity application in production AI systems.

Go deeper with CFSGPT

Get AI-powered deep-dives, save terms, and run advanced simulations — free account.

Create Free Account