Sign in to save

Bookmark this page so you can find it later.

Sign in to save

Bookmark this page so you can find it later.

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.

Understanding AI & Machine Learning: K-Means Clustering Explained

The method begins with a set of measurements for every item. A shop dataset might include how often each customer visits and how much they spend. A science dataset might include temperature, pressure, and light level from each observation.

Each item becomes a point in a space built from these measurements. With two measurements, the points can be drawn on a flat graph.

With many measurements, the space cannot be pictured easily, but the same idea still works. Points that are close together have similar measurement values.

The starting positions of the centers matter more than they may seem. K-means often begins with randomly placed centers, so two runs on the same data can produce slightly different groups. A poor start can leave a center in an unhelpful location.

The method can settle into a result that is stable but not the best possible grouping. For this reason, software commonly runs the process several times with different starting positions and keeps the result with the smallest total spread within groups. A smarter starting method called k-means plus plus spreads the first centers apart, which often gives a more reliable result.

Choosing the number of groups needs judgment. More groups make each group tighter, since fewer points must fit around each center. That does not prove that more groups reveal a useful pattern.

Students may use an elbow graph, which compares the number of groups with the total squared distance from points to their assigned centers. The improvement usually becomes smaller after a certain point. That bend can suggest a sensible choice.

Subject knowledge matters too. A biologist may choose groups that match known habitats. A school may choose a small number of groups if it needs clear support plans rather than many tiny categories.

Before using K-means, check the scale and shape of the data. Distance gives more weight to features with larger numerical ranges. If age ranges from ten to eighty while yearly income ranges through many thousands, income can dominate the grouping.

Standardising features puts them on a comparable scale by measuring how far each value is from its usual value. K-means works best when groups are fairly compact and roughly round in the chosen measurement space. It struggles with curved groups, very unequal group sizes, or isolated extreme values.

An outlier can pull a center far away from the main pattern. Clusters are descriptions, not proof of natural categories. A useful result needs checking against real context, data quality, and the reason the grouping was made.

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. 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. 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. 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.