tezvyn:

k-Means Clustering: Grouping Data Around Centroids

AI-drafted, machine-checkedSource: Wikipedia: K-means clusteringbeginner

k-Means automatically groups data into a specified number of clusters (`k`) by finding the center of each group. It's used for customer segmentation, document categorization, and image compression.

WHY IT EXISTS: We often have large datasets without labels and need to discover their underlying structure. k-Means provides a simple, computationally efficient way to automatically group similar data points together without prior knowledge of the groups.

THE MENTAL MODEL: Imagine you have a scatter plot of data and want to circle k groups. You randomly drop k pins (centroids) on the plot. First, you assign every data point to its nearest pin. Second, you move each pin to the average location (the mean) of all the points assigned to it. You repeat these two steps—assigning points, then moving pins—until the pins stop moving. The final groups of points around each pin are your clusters.

HOW IT WORKS: The algorithm is iterative. First, you choose k, the number of clusters. Second, you randomly initialize k centroids. Third, for each data point, you calculate its distance to every centroid and assign it to the closest one, forming k initial clusters. Fourth, you recalculate the centroid of each new cluster by taking the mean of all points within it. You repeat the assignment and recalculation steps until the cluster assignments no longer change or a maximum number of iterations is reached.

WHEN TO USE IT: Use k-Means when you have a numerical dataset and a reasonable idea of how many clusters (k) exist. It's very fast and scales well to large datasets. It works best when clusters are spherical, have similar density, and are well-separated. Common uses include customer segmentation, image color quantization, and identifying topics in documents.

WHEN NOT TO USE IT: Avoid k-Means if you can't make an educated guess for k. The algorithm struggles with non-spherical clusters, clusters of varying sizes, or different densities. Because it minimizes the sum of squared distances to the mean, it is very sensitive to outliers, which can drag a centroid far from its natural center. For these cases, algorithms like DBSCAN or hierarchical clustering may be more suitable.

ONE CANONICAL EXAMPLE: To segment customers, a company might plot them based on purchase frequency and average transaction value. Running k-Means with k=3 could reveal three distinct groups: 'low-value, infrequent shoppers', 'high-value, infrequent shoppers', and 'high-value, frequent shoppers'. The centroid of each cluster represents the average customer profile for that segment, which can be used to target marketing campaigns.

Read the original → en.wikipedia.org

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.