Clustering
Кластеризация
Clustering groups similar data points without using predefined labels. K-means is one clustering algorithm: it groups points around centers learned from the data.
Also known as: k-means
What clustering does
Suppose you have a collection of support tickets but no topic tags. You can turn the text into numerical vectors and use clustering to group tickets with similar wording or meaning. The resulting groups might suggest topics such as billing or account access, but the algorithm does not name them. This is unsupervised learning: unlike a supervised classifier, a clustering algorithm does not learn from examples with known labels.
How k-means works
K-means is one way to cluster data, not another name for clustering. It takes a number of groups, k, and starts with k initial centers. It then alternates between two steps:
- Assign each point to the nearest center using Euclidean distance.
- Move each center to the mean of the points assigned to it. This mean is called a centroid.
The algorithm repeats until the centers barely move or it hits an iteration limit. Its objective is to minimize the sum of squared distances from points to their assigned centroids, a quantity called inertia in scikit-learn. A run can settle on a solution that is not the global best, so it is common to try several initializations and keep the run with the lowest inertia.
Example: reducing an image palette
Treat each pixel as a point with three features: its red, green, and blue channel values. To reduce an image to 16 colors, fit k-means with k=16, then replace each pixel with the color of its assigned centroid. The centroids are averages, so the resulting palette can contain colors that were not present in the original image. This groups pixels by color, not by location: two matching pixels on opposite sides of the image can belong to the same cluster.
Where it works and what can go wrong
Clustering is also used to explore customer segments and organize documents by their embeddings. The result depends on how you define similarity. If you cluster customers using order count and annual spending, spending may dominate Euclidean distance just because its values are on a larger numerical scale. Standardization is a common starting point, but it cannot tell you which features matter to the task.
K-means is a good candidate for compact, well-separated groups. Outliers can pull centroids away from typical points, and the method can split or merge curved groups in unintuitive ways. A density-based method such as DBSCAN can find irregularly shaped groups and leave isolated points unassigned as noise.
How it comes up in interviews
A common question is how to choose k without ground-truth labels. The elbow method plots inertia against k and looks for a bend where adding more clusters gives diminishing returns. The silhouette score compares how close points are to their own cluster with how close they are to the nearest other cluster. Neither gives a universal answer. A useful follow-up is to check whether the groups remain similar when you resample the data and whether they help with the task you started with.