K-means clustering is a machine learning method that finds groups in data without being told the correct answers first. It is used when a computer needs to discover patterns, such as grouping customers, organizing images, or finding similar science measurements. The word k means the number of groups you want the algorithm to find.
The result is a set of clusters, each with a center point called a centroid.
Key Facts
- K-means is an unsupervised learning algorithm, so it uses unlabeled data.
- k is the number of clusters chosen before the algorithm starts.
- Distance is often measured with the Euclidean distance formula: d = sqrt((x2 - x1)^2 + (y2 - y1)^2).
- Each data point is assigned to the nearest centroid.
- A centroid is updated by taking the mean of the points in its cluster: mean = sum of values / number of values.
- The algorithm repeats assignment and update steps until the centroids stop changing much.
Vocabulary
- Cluster
- A cluster is a group of data points that are more similar to each other than to points in other groups.
- Centroid
- A centroid is the center point of a cluster, found by averaging the coordinates of the points assigned to it.
- Unsupervised learning
- Unsupervised learning is a type of machine learning where the algorithm looks for patterns in data without using correct labels.
- Euclidean distance
- Euclidean distance is the straight-line distance between two points in a coordinate plane.
- Iteration
- An iteration is one repeated cycle of assigning points to clusters and updating the centroids.
Common Mistakes to Avoid
- Choosing k without thinking about the data, because the number of clusters strongly affects the final grouping.
- Assuming k-means always finds the best answer, because different starting centroids can lead to different final clusters.
- Forgetting to scale features, because a feature with large numbers can dominate the distance calculation.
- Using k-means on oddly shaped clusters, because k-means works best when clusters are compact and roughly circular.
Practice Questions
- 1 A point P is at (4, 6). Centroid A is at (1, 2) and centroid B is at (7, 6). Using Euclidean distance, which centroid is closer to P?
- 2 A cluster has points (2, 4), (6, 8), and (10, 2). Find the new centroid by averaging the x-values and y-values.
- 3 Explain why changing k from 3 to 5 could change the meaning of the clusters found by k-means, even if the same data points are used.