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.
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
- 10 Ways to Automate Internal Linking with AI
- Different ways to Map Keywords to topics
- What Fuzzy Matching Is, and Where It Fits in SEO
- AI Search Optimisation & Agentic SEO course, lesson on how systems narrow choices.
- Semantic ML-enabled Keyword Research course, lab on query entity extraction.
Where Cosine Similarity shows up on MLforSEO
Learn it in a course
Introduction to Machine Learning for SEO
- Practical: Query Entity Extraction with Google NLP and Entity ML-enabled data analysis
- Marketing and SEO Implementations of fuzzy matching
- Practical: Clustering of page content with BERTopic
Semantic AI-powered SEO Keyword Research
- Practical/Lab Query Entity Extraction with Google NLP and Entity ML-enabled data analysis
- How to move from traditional to semantic keyword universe – Checklist, based on course tasks
Read about it on the blog
Try it yourself
- GuideComprehensive String Matching & Fuzzy Matching Reference Guide for SEO Applications: Use Cases, Approaches, Models
- Coding Script or NotebookMap Keywords to Topics: Supervised (sBERT + Fuzzy) vs. Unsupervised (BERTopic) (Notebook)
- Coding Script or NotebookAutomated Redirect Mapping with Triple Fuzzy Matching for Site Migrations (Notebook)
- Code on GitHubImage SEO + multimodal audit (2026)
Sources and further reading
Other terms from Model Evaluation
- Accuracy (ML metric)Accuracy is the share of all predictions a model gets right. It is the simplest evaluation metric, but it misl…
- Coherence ScoreA metric for how interpretable a topic model's topics are, based on how often each topic's top words appear to…
- Emotion ScoresPer-emotion scores (joy, sadness, fear, disgust, anger) an emotion analysis API assigns to a text, each from 0…
- Entity Salience ScoreA 0 to 1 score from Google's Natural Language API showing how central each extracted entity is to the analysed…
- F1 ScoreA single evaluation metric that balances precision and recall (their harmonic mean); useful when classes are i…
- LLM-as-a-JudgeUsing a large language model to score or compare outputs against a rubric, as a scalable stand-in for human re…
- OverfittingOverfitting is when a model learns its training data too closely, including noise, so it scores well in traini…
- Precision and RecallPrecision is the share of a model's positive results that are correct; recall is the share of all true positiv…
- Query DistanceHow far apart two search queries are, measured by spelling (fuzzy matching), by meaning (embeddings) or by the…




