Graph
Cosine Similarity

📐Cosine Similarity

NEW

A similarity measure that computes the cosine of the angle between two non-zero vectors.

DESCRIPTION

Cosine similarity quantifies orientation rather than magnitude, making it effective when vector length is uninformative, such as TF-IDF text vectors or normalized embeddings. It ranges from −1 to 1, though many applications use the 0 to 1 range after non-negativity. It underpins nearest neighbor search, clustering, recommendation, and semantic retrieval. Angular distance is a related metric derived from the cosine.

ALTERNATIVE DEFINITIONS

Wikipedia

Cosine similarity is a measure of similarity between two non-zero vectors of an inner product space that measures the cosine of the angle between them.

Emphasizes inner product space and angular interpretation.

Source

scikit-learn

Compute cosine similarity between samples in X and Y.

Operational definition as implemented in a standard ML library.

Source

Stanford IR Book

Cosine similarity is the normalized dot product of two vectors, often used in the vector space model for text.

Connects cosine similarity to vector space IR and normalization.

Source

Dot Product Term Frequency–Inverse Document Frequency Embeddings K-Nearest Neighbors Vector Space Model Angular Distance Feature Scaling

COMPETING TERMS

Euclidean Distance Jaccard Index

KNOWLEDGE AREAS

AI Data Science Semantic & Knowledge Modeling

TARGET AUDIENCE

AI Engineer Data Scientist Data Engineer Software Developer

EXTERNAL SOURCES

TOP QUESTIONS

When should I use Cosine Similarity?

Use it when direction matters more than magnitude, such as TF-IDF vectors or normalized embeddings in semantic search.

Category: Guidance

How does Cosine Similarity compare to Euclidean distance?

Cosine focuses on angle (orientation), while Euclidean measures absolute distance; on L2-normalized vectors, they are monotonically related.

Category: Alternatives

Can Cosine Similarity be negative?

Yes, values range from −1 to 1 for centered vectors; with non-negative features like TF-IDF, it typically ranges 0 to 1.

Category: Explanation

What are common mistakes with Cosine Similarity?

Ignoring normalization or comparing unaligned vector spaces leads to misleading similarity scores.

Category: Mistakes

How is Cosine Similarity used in retrieval?

It powers nearest neighbor search over embeddings for semantic search, recommendations, and clustering.

Category: Application

Is Cosine Similarity a metric?

Cosine similarity itself is not a distance metric; angular distance derived from cosine satisfies metric properties.

Category: Explanation

Does dimensionality reduction affect Cosine Similarity?

Yes, projections can distort angles; techniques like PCA or SVD should be evaluated for downstream similarity preservation.

Category: Risks

How to scale Cosine Similarity to large datasets?

Use approximate nearest neighbor indexes (e.g., HNSW, FAISS) optimized for angular similarity.

Category: Best-Practices

What preprocessing helps Cosine Similarity on text?

Tokenization, stopword removal, TF-IDF weighting, and L2 normalization improve discriminative power.

Category: Guidance