Model Evaluation
Euclidean Distance
In shortEuclidean distance is the straight-line distance between two points in any number of dimensions, used to compare numeric data and vectors.
Euclidean distance is the straight-line distance between two points, the “as the crow flies” distance you would measure with a ruler, extended to any number of dimensions. In machine learning it measures how far apart two data points are when each is described by numbers, such as customers described by spend and visit frequency, or texts described by embedding vectors. The smaller the distance, the more similar the two points.
How it works
For two points, subtract each coordinate from the other, square the differences, add them up and take the square root. In two dimensions, the points (1, 2) and (4, 6) are the square root of 3² + 4², which is 5, apart. The same formula works for embeddings with hundreds of dimensions; there are simply more differences to add up.
MLforSEO’s clustering lesson lists it alongside cosine similarity and Manhattan distance (the sum of absolute differences, like walking city blocks) as the metrics clustering algorithms use on numeric data. K-means uses it by default to assign each point to its nearest centroid, and Ward linkage in hierarchical clustering is built on it.
The two main metrics look at different things. Euclidean distance is sensitive to magnitude, the length of the vectors, while cosine similarity only compares their direction. Google’s clustering course notes that once vectors are normalised to the same length, Euclidean distance, cosine and dot product become proportional and effectively measure the same thing.
How it’s used in SEO and marketing
- Customer segmentation: clustering customers on scaled recency, frequency and monetary (RFM) features with K-means or Ward hierarchical clustering, as in our customer segmentation notebook.
- Clustering reduced embeddings: our BERTopic tutorial reduces embeddings with UMAP using a cosine metric, then runs HDBSCAN with Euclidean distance on the lower-dimensional output.
- Nearest-neighbour search: vector databases let you choose Euclidean (often called L2), cosine or dot product as the metric for vector search.
- Outlier checks: pages, accounts or customers that sit far from every cluster centre.
When to prefer cosine for text
For raw text vectors, such as TF-IDF scores or word counts, use cosine. A long page and a short page on the same topic point in a similar direction, but sit far apart in Euclidean terms because the long page has bigger counts. With sentence embeddings, cosine is the usual default in tools such as Sentence Transformers, and Euclidean gives the same ranking of neighbours if the vectors are normalised. Our clustering guide makes the same point: text similarity is normally measured with cosine in embedding space rather than plain Euclidean distance.
Limitations and things to watch
- Scale dominates. A revenue column in thousands swamps a visits column in single digits, so standardise features before measuring distance.
- In very high dimensions, distances between points become more alike, which weakens the contrast between near and far neighbours. Dimensionality reduction helps.
- It works only on numbers. Categories must be encoded first, and strings need a different measure such as edit distance.
- A distance is not a similarity score. Lower is closer, so convert it before mixing it with other scores.
Learn it on MLforSEO
- What Machine Learning Clustering Is, and Where It Fits in SEO
- Topic Modelling for SEO with BERTopic
- Customer Segmentation with Machine Learning Using Three Clustering Approaches (Notebook)
- Complete Guide to ML Clustering for Marketing and SEO: Use Cases and Model Choices
- Introduction to Machine Learning for SEO
Where Euclidean Distance shows up on MLforSEO
Read about it on the blog
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…
- Confusion MatrixA confusion matrix is a table comparing a classifier's predictions with the true labels, showing exactly which…
- Cosine SimilarityA measure of how closely two vectors point in the same direction, used to score how similar two texts, keyword…
- 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…
- Ground TruthGround truth is the set of correct answers, usually labelled by people, that a model is trained on or checked…
- LLM-as-a-JudgeUsing a large language model to score or compare outputs against a rubric, as a scalable stand-in for human re…
