Model Evaluation

Cosine Similarity

In shortA measure of how closely two vectors point in the same direction, used to score how similar two texts, keywords or images are once they are embedded.

Updated 24 Oct 2026· 2 min read

Cosine similarity measures how similar two items are by comparing the angle between their vectors. When text has been turned into numbers, for example as an embedding or a TF-IDF vector, cosine similarity tells you how closely the two point in the same direction, regardless of their length.

How it works

The formula divides the dot product of two vectors by the product of their lengths. The result is the cosine of the angle between them. A score of 1 means the vectors point the same way (very similar), 0 means they are unrelated (at right angles), and -1 means opposite. With TF-IDF vectors, which have no negative values, scores sit between 0 and 1. Because length is divided out, a long page and a short query can still score as highly similar if they cover the same ground.

Libraries do the maths for you: scikit-learn’s cosine_similarity or sentence-transformers’ util.cos_sim return a full similarity matrix in one line.

How it’s used in SEO and marketing

  • Mapping keywords to topics or pages by scoring every keyword against every topic embedding.
  • Finding internal link opportunities: score every page pair and keep similar pairs that are not yet linked.
  • Redirect mapping and duplicate detection, alongside fuzzy matching.
  • Comparing entities extracted from queries after vectorising them.
  • Spotting near-duplicate images with CLIP embeddings.
  • Understanding AI search: early retrieval scores content chunks by cosine similarity to the query embedding.

Example

In MLforSEO’s AI Search & Agentic SEO course, the query “What is the average cost of CRM implementation for a 50-person company?” is compared with three chunks. A specific, scoped chunk scores 0.91. A vague chunk on the same topic scores 0.87 and also passes the early filter. A credible chunk about a different subject scores 0.31 and is dropped. Cosine similarity measures closeness in meaning; it says nothing about accuracy or authority.

Limitations and things to watch

  • Scores are relative to the model. Different embedding models produce different ranges, so use a relative threshold (top N, or a percentile) rather than a fixed cut-off copied from someone else.
  • High similarity is not quality: a factually wrong chunk on the right topic can outscore a correct one on an adjacent topic.
  • With TF-IDF vectors it only sees shared words, so synonyms score zero unless you use embeddings.
  • Comparing every item with every other item grows quickly with dataset size. For large sets, use a vector database or approximate nearest-neighbour search.

Learn it on MLforSEO

Where Cosine Similarity shows up on MLforSEO

Sources and further reading

Other terms from Model Evaluation

See all 16 Model Evaluation terms →