Skip to main content
Welcome back. In this lesson we’ll take a focused look at cosine similarity — a fundamental technique for comparing vectors in high-dimensional spaces. At a high level, cosine similarity answers the question: are two vectors pointing in the same direction? It measures the angle between them, not their lengths. A few key points:
  • Each vector can be thought of as an arrow in n-dimensional space.
  • Cosine similarity depends on the angle between vectors, so differences in magnitude (length) do not change the similarity if the direction remains the same.
  • When two vectors point in the same direction, their cosine similarity approaches 1. If they are orthogonal, the similarity is 0. Opposite directions yield -1.
Cosine similarity focuses on angular alignment between vectors. Differences in magnitude (length) are ignored, which makes it ideal for comparing semantic content across short and long texts.
Mathematical definition:
cos(θ) = (A · B) / (‖A‖ * ‖B‖)

Where:
- A · B is the dot product of vectors A and B
- ‖A‖ and ‖B‖ are the magnitudes (L2 norms) of A and B
- cos(θ) ∈ [-1, 1]
Canonical angle cases:
  • θ = 0° → cos = 1 : vectors point in exactly the same direction (highly similar).
  • θ = 90° → cos = 0 : vectors are orthogonal (no directional relationship).
  • θ = 180° → cos = -1 : vectors point in opposite directions (maximally different).
Practical reasons cosine similarity matters
  1. Robustness to document length differences
    Cosine similarity ignores vector magnitude. For search and retrieval, this means a short query can match a long document based on topic alignment rather than penalizing the longer document for having more words.
  2. Short vs. long text matching
    A short social post and a long article can still be recognized as semantically similar because cosine similarity compares direction (topic) rather than raw count.
  3. Widely adopted default for text embeddings and semantic search
    Many embedding models and vector databases default to cosine similarity because it captures topical alignment across documents of different lengths.
The image explains cosine similarity, focusing on measuring angles between vectors to determine similarity based on direction. It includes visual examples and highlights why cosine similarity matters in computing document similarity.
Example: news recommendation
Imagine a user’s short query about “inflation and interest rates” and a full-length economics article on the same topic. Despite a 20-word query and a 1,000-word article, cosine similarity will identify strong topical alignment because the direction of their embedding vectors is similar.
Industry adoption
OpenAI’s text embeddings, SentenceTransformers, and many vector databases prefer cosine similarity for semantic search.
They all default to cosine similarity for text-based search.
The image explains cosine similarity with a diagram showing vectors and examples of angles between them. It highlights that cosine similarity focuses on direction, not length, and provides practical applications.
When you build a semantic search system or a RAG (Retrieval-Augmented Generation) pipeline, cosine similarity is usually the first similarity metric to try — it answers “Are these documents about the same thing?” regardless of how much each document expresses that topic. Cosine similarity vs. Euclidean distance
MetricWhat it measuresWhen to use
Cosine similarityDirectional alignment (angle) between vectors; ignores magnitudeBest for semantic search, text embeddings, when document length varies
Euclidean distanceAbsolute distance in vector space (L2 norm)Useful when magnitude and absolute distances matter (e.g., low-dimensional feature spaces, clustering with scale-sensitive features)
Quick implementation note (pseudocode):
# Given vectors a and b:
dot = sum(ai * bi for ai, bi in zip(a, b))
norm_a = sqrt(sum(ai * ai for ai in a))
norm_b = sqrt(sum(bi * bi for bi in b))
cosine_similarity = dot / (norm_a * norm_b)
References and further reading That’s the essence of cosine similarity: a compact, direction-focused similarity measure that excels in semantic tasks. Euclidean distance is another common metric and can be preferable when absolute vector magnitudes and raw distances are meaningful for your application.

Watch Video