ML Models & Algorithms
K-Nearest Neighbours (KNN)
In shortK-nearest neighbours (KNN) labels or retrieves items by finding the k most similar examples to them, letting the closest points vote on the answer.
K-nearest neighbours (KNN) is a simple algorithm that makes a decision about a new item by looking at the k most similar items it already knows. For classification, as MLforSEO’s classification lesson puts it, it classifies a point based on the majority of its nearest neighbours, letting the closest points vote. The same idea, finding the nearest items to a query, also powers vector search in modern search and AI retrieval.
How it works
KNN has three ingredients:
- A representation: each item becomes a set of numbers, for text usually an embedding.
- A distance or similarity measure, such as Euclidean distance or cosine similarity.
- A value for k: how many neighbours to consult. A larger k smooths out noise but blurs the boundaries between classes, so the right value depends on your data.
For classification, the new item takes the most common label among its k neighbours, optionally weighting closer neighbours more heavily. KNN builds no general model during training; it simply stores the labelled examples and does its work at prediction time, which makes it the classic example of the “lazy” learners in the eager versus lazy distinction MLforSEO’s classification post flags. It is a supervised method, which makes it a different thing from k-means clustering, an unsupervised method that also uses a k.
In search, nearest neighbour search returns the k closest vectors to a query vector. Checking every vector is exact but slow on large indexes, so vector databases and engines such as Elasticsearch use approximate nearest neighbour (ANN) methods, such as HNSW graphs, that trade a little recall for much lower latency.
How it’s used in SEO and marketing
- Assigning new keywords to existing categories or tagging social posts, the uses MLforSEO’s course highlights.
- Finding the nearest match for every keyword, title or URL in a list, as MLforSEO’s fuzzy matching template does when it returns each item’s nearest neighbour and score.
- Internal linking and redirect mapping: suggesting the most similar page for each source page.
- Retrieval in AI search and RAG: the first stage of the retrieval funnel keeps the content chunks whose vectors sit closest to the query.
Example
You have 2,000 keywords already labelled with a topic and 300 new ones. Embed all of them with Sentence Transformers, then for each new keyword find its five nearest labelled neighbours by cosine similarity. If four of the five are labelled “trail running”, the new keyword gets that label, and a 3-2 split flags it for human review.
Limitations and things to watch
- Prediction gets slower as the dataset grows, because every query is compared with stored examples unless you use an index.
- In very high-dimensional data, distances become less meaningful, so the quality of the embeddings matters more than the algorithm.
- Features on different scales distort distances; normalise numeric data first.
- Imbalanced classes bias the vote towards the most common label.
- Closest is not the same as correct: a semantically near chunk can still be factually wrong.
Learn it on MLforSEO
Where K-Nearest Neighbours (KNN) shows up on MLforSEO
Learn it in a course
Sources and further reading
- What Machine Learning Classification Is, and Where It Fits in SEO MLforSEO
- How modern search actually works: the five-stage retrieval funnel MLforSEO
- What Fuzzy Matching Is, and Where It Fits in SEO MLforSEO
- scikit-learn: Nearest Neighbors
- Elastic: kNN search in Elasticsearch
- Malkov and Yashunin: Efficient and robust approximate nearest neighbor search using HNSW graphs
Other terms from ML Models & Algorithms
- BERT (Bidirectional Encoder Representations from Transformers)Google's 2018 transformer language model that reads text in both directions to produce context-aware embedding…
- BERTopicA modular topic modelling technique that clusters transformer embeddings and labels each cluster with class-ba…
- BIRCH (Balanced Iterative Reducing and Clustering using Hierarchies)BIRCH (Balanced Iterative Reducing and Clustering using Hierarchies) clusters very large numeric datasets by s…
- BM25A classic ranking function that scores documents by how often query words appear, adjusted for word rarity and…
- Boyer-MooreA fast exact string-search algorithm that compares a pattern from right to left and skips ahead after mismatch…
- c-TF-IDFClass-based TF-IDF: treats each cluster as one document to find the words that best describe it, used by BERTo…
- Centroid-based ClusteringA clustering family that groups data around central points (centroids), each the average of its cluster's memb…
- Convolutional Neural Network (CNN)A deep learning architecture that scans data with small learned filters to detect local patterns; the classic…
- DBSCANDensity-based clustering that groups closely packed points, finds the number of clusters itself and labels iso…