ML Models & Algorithms
K-Means Clustering
In shortAn unsupervised algorithm that splits data into a chosen number (k) of clusters by assigning each point to its nearest centre. Simple, fast and widely used.
K-means clustering is an unsupervised algorithm that divides a dataset into k groups, where you choose k in advance. Each group is defined by its centre (centroid), and each data point joins the group whose centre is closest. It is the most widely used clustering algorithm because it is simple, fast and easy to explain.
How it works
K-means is a centroid-based, hard clustering method. It repeats a short loop:
- place k starting centres (the common k-means++ method spreads them out sensibly),
- assign every point to its nearest centre,
- move each centre to the average position of the points assigned to it,
- repeat until the assignments stop changing.
The result minimises the spread of points within each cluster. Text has to become numbers first: MLforSEO’s keyword workflow uses TF-IDF vectors, and embeddings from a sentence-transformer model work well too. Because those vectors have many dimensions, PCA is used to reduce them to two dimensions so you can plot the clusters.
How it’s used in SEO and marketing
- Keyword clustering when you roughly know how many groups you want.
- Customer segmentation on order frequency, order value and behaviour from your CRM and GA4.
- Image clustering by colour, for example grouping product photos for colour filters.
- Competitor grouping by authority, traffic, keyword count and backlinks.
- As a step inside other tools, such as BERTopic, which can use K-means in place of HDBSCAN when you want a fixed number of topics.
Example
In MLforSEO’s keyword-to-topic mapping guide, a list of extracted entities is converted to TF-IDF vectors, grouped with K-means, then reduced with PCA to a 2D scatter plot coloured by cluster. K-means does not name the clusters, so the script highlights sample terms from each one to help you label them.
Limitations and things to watch
- You must choose k. Try a range and compare with the elbow method or silhouette scores, then read the clusters.
- It assumes roughly round clusters of similar size and struggles with irregular shapes.
- Every point is forced into a cluster, so outliers pull centres off course. Density-based methods such as HDBSCAN label noise instead.
- Results depend on the starting centres; set a random seed and run several initialisations.
- Scale numeric features first, or the feature with the largest numbers will dominate.
Learn it on MLforSEO
- Different ways to Map Keywords to topics – with Supervised and Unsupervised ML approaches
- What Machine Learning Clustering Is, and Where It Fits in SEO
- Customer Segmentation with Machine Learning Using Three Clustering Approaches
- Image Clustering by Color with Three Machine Learning Approaches for Visual Content Organization
- The Semantic AI-powered SEO Keyword Research course, which uses K-means for keyword and entity clustering
Where K-Means Clustering shows up on MLforSEO
Learn it in a course
Read about it on the blog
Try it yourself
- Coding Script or NotebookCustomer Segmentation with Machine Learning Using Three Clustering Approaches
- Coding Script or NotebookImage Clustering by Color with Three Machine Learning Approaches for Visual Content Organization
- Coding Script or NotebookAdvanced Entity Analysis with Multiple ML Techniques for Semantic Keyword Research
Sources and further reading
- Different ways to Map Keywords to topics – with Supervised and Unsupervised ML approaches MLforSEO
- What Machine Learning Clustering Is, and Where It Fits in SEO MLforSEO
- Customer Segmentation with Machine Learning Using Three Clustering Approaches MLforSEO
- K-means, scikit-learn clustering user guide
- Clustering algorithms (Google for Developers)
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…
