Generating Synthetic Clustering Datasets with make_blobs and make_moons
Synthetic data is generated on purpose, so you know the true grouping in advance.
Why a Correct Number of Clusters Is Not Enough
A common first impression of k-means is that it is a general-purpose grouping tool. You choose a number of clusters, the algorithm runs, and groups come out. Many learners carry an unstated assumption with them: if the number of clusters is right, k-means will recover the groups that are really present in the data. This article tests that assumption. The short answer is that k-means can miss the groups even when the number of clusters is correct, because the shape of its answer is fixed by how it measures distance to centers.
To test the idea safely, we use synthetic datasets. Synthetic data is generated on purpose, so you know the true grouping in advance. That lets you compare what k-means predicts against a structure you already understand. The two generators in this topic are make_blobs, which produces round or stretched groups, and make_moons, which produces two interleaved half-circles. Together they cover the failure situations this article examines.
Testing the Belief That k Guarantees Recovery
Suppose you generate a dataset with three real groups and set k to 3. It is tempting to think the algorithm must then find those three groups. The reasoning feels sound: the count matches, so the labels should match. But the count only controls how many regions the algorithm creates. It does not control the shape of those regions. If the true groups have shapes that the algorithm cannot draw, the correct count still produces a wrong grouping.
Centers, Convex Regions, and the Midpoint Boundary
k-means defines each cluster solely by its center. Points are assigned by their distance to the nearest center. Because of this, the region belonging to each cluster is convex: a cluster can only be a shape you could fill by drawing straight lines between any two of its points. The algorithm can therefore capture only relatively simple shapes.
The second assumption concerns size. k-means treats all clusters as having a similar diameter. It places the boundary between two clusters exactly in the middle between their centers. Written as a rule, the boundary between centers $c_A$ and $c_B$ is the set of points where the distance to each center is equal, which is the midpoint $\frac{c_A + c_B}{2}$ together with the line perpendicular to the segment joining the two centers. Every point on one side of that line is closer to one center, so it receives that center's label.
Because the boundary is always the midpoint, one rule explains many surprising results. The boundary does not look at how many points a cluster has or how spread out its points are. It only looks at where the centers are. A large, spread-out cluster and a tight cluster are treated identically, provided their centers are in the same places.
Tracing One Assignment by Hand
Assigning points to the nearer of two centers
Two centers are A = (0, 0) and B = (4, 0). Assign the points P = (2.5, 1) and Q = (1.8, 0.5) to the nearest center using squared distances.
- 1Compute the squared distance from P to A
The difference is (2.5 - 0, 1 - 0) = (2.5, 1). The squared distance is $2.5^{2} + 1^{2} = 6.25 + 1 = 7.25$.
- 2Compute the squared distance from P to B
The difference is (2.5 - 4, 1 - 0) = (-1.5, 1). The squared distance is $(-1.5)^{2} + 1^{2} = 2.25 + 1 = 3.25$.
- 3Assign P
Since 3.25 is smaller than 7.25, P is closer to B. It receives B's label, even though it sits on the A side of the plot's center-line at x = 2 only barely. The point lies at x = 2.5, which is beyond the midpoint x = 2.
- 4Compute the squared distances for Q
To A: $1.8^{2} + 0.5^{2} = 3.24 + 0.25 = 3.49$. To B: $(-2.2)^{2} + 0.5^{2} = 4.84 + 0.25 = 5.09$.
- 5Assign Q
Since 3.49 is smaller than 5.09, Q is closer to A and receives A's label. Q sits at x = 1.8, which is on A's side of the midpoint x = 2.
P is labeled with B and Q is labeled with A. The dividing line is the vertical line x = 2, and the assignment depends only on which side of that line a point lies.
Failure One: Clusters of Different Density
The first failure case involves clusters that differ in density. Imagine a dense region in the lower left, a dense region in the upper right, and a sparser region in the center. A human looking at this picture would likely name these three groups: the dense lower-left region, the dense upper-right region, and the less dense central region. The expected grouping has three clear parts.
The data for this case uses three blobs with different standard deviations, so some are tight and some are loose. The loose center group has points that drift outward toward its neighbors. When k-means runs, the midpoint rule places each boundary halfway between centers. The sparse group's outer points can end up closer to a neighboring center than to their own group's center, so they are pulled across the boundary.
The source reports this outcome directly: cluster 0 and cluster 1 both contain some points that are far from the other points in their own cluster and reach toward the center of the plot. The algorithm has pulled the edges of the dense regions into neighboring clusters. The mismatch is not caused by a wrong k. It is caused by the boundary landing in the wrong place for groups of unequal spread.
Failure Two: Stretched, Non-Spherical Clusters
The second failure involves clusters that are not round. k-means assumes that all directions are equally important for each cluster. Picture a dataset with three clearly separated parts, where each part is stretched along a diagonal rather than being a circle. The groups remain easy to see, but the stretching means that a point along the long axis of one group can be closer to the center of a neighboring group than to its own group's center.
The source generates this kind of data by creating a blob dataset and then applying a random linear transformation to the coordinates. That matrix multiplication stretches the round blobs into diagonal ellipses. The transformation changes the shape of each group but leaves the group membership of every point unchanged. The true grouping is still the same; only the geometry has changed.
The source states the reason plainly: k-means only considers the distance to the nearest cluster center, and it cannot handle this kind of data. A stretched group has no single center that represents all of its directions equally. The algorithm sees one center and one distance, so it cannot represent a group that extends far in one direction and little in another.
Failure Three: Interleaved Half-Moons
The third failure involves clusters with complex shapes. The source uses the two-moons dataset from Chapter 2, where two interleaved half-circles form two groups. Visually, the correct answer is obvious: each half-moon is one cluster. The two arcs curve around each other, so no straight midpoint line can separate one arc from the other without crossing both.
The source is direct about the outcome: here we would hope that the clustering algorithm can discover the two half-moon shapes, but this is not possible using the k-means algorithm. Because each cluster is convex and separated by a straight midpoint boundary, k-means cuts across the moons instead of separating them. The number of clusters is correct, so the failure has nothing to do with choosing k.
Mistakes When Reading a Poor Clustering
Common mistake Assuming the right k guarantees the right groups
What it looks likeThree true groups are present and k = 3 is used, yet a moon-shaped group is split across two clusters.
Why it failsk controls how many regions exist, not what shape each region can take.
Do this insteadCheck the shape of the expected groups before trusting the output, and treat a good k as necessary but not sufficient.
Common mistake Blaming the random seed for a stable wrong answer
Why it failsA boundary that cuts across a half-moon will cut across it on every run, because the failure comes from the midpoint rule rather than from randomness.
Do this insteadWhen the same wrong grouping repeats, look at the geometry of the data instead of rerunning with different settings.
Common mistake Judging a result only by cluster sizes
What it looks likeA sparse group loses its outer points to a neighbor, but the cluster sizes still look reasonable.
Why it failsBalanced sizes can hide points that sit on the wrong side of the boundary.
Do this insteadCompare individual points with the structure you expect, not just the counts per cluster.
Common mistake Treating the boundary as a property of the data instead of a property of the algorithm
Locating Where Predicted Labels Diverge
When a clustering result looks wrong, the useful question is where it departs from what you expected. A reliable way to find that spot is to plot predicted labels against the structure you believe is in the data, then check each region in turn. Start with the region that looks most mismatched. Ask whether the points there are close to their assigned center, and whether a neighboring center is nearly as close. In the varying-density case, the mismatch sits along the edges of the dense regions, where far-reaching points get pulled into a neighbor.
What do you think happens?
Centers A = (0, 0) and B = (4, 0). Which center does the point R = (2.2, 3) get assigned to?
Reveal answer
B
The squared distance to A is $2.2^{2} + 3^{2} = 4.84 + 9 = 13.84$. The squared distance to B is $(-1.8)^{2} + 3^{2} = 3.24 + 9 = 12.24$. Since 12.24 is smaller, R is closer to B. Its x-coordinate is beyond the midpoint x = 2, so it falls on B's side even though it is far from the center-line.
Take a predicted clustering of two interleaved half-moons. List the points whose label disagrees with the arc they sit on, then describe in one sentence where along the arcs the disagreements cluster. Explain whether those points are close to their assigned center and whether a neighboring center is nearly as close.
Hints
- Start with the points near the place where the two arcs come closest to each other.
- A point is suspicious when its distances to its own center and to a neighboring center are almost equal.
- The midpoint boundary is the line where those two distances are equal.
What the Divergence Tells You
Once you have located the divergent points, the pattern tells you which assumption failed. Mismatches along the edges of dense regions point to the equal-size assumption. Mismatches that extend along a diagonal stretch point to the spherical assumption. Mismatches that cut across a curved arc point to the convex-shape limit. Reading the pattern this way turns a wrong answer into information about the data's geometry.
Key Takeaways
- k-means defines each cluster by its center, so each cluster region is convex and the algorithm captures only relatively simple shapes.
- The boundary between two clusters is placed exactly midway between their centers, which assumes clusters have a similar diameter.
- Clusters with different densities, stretched or non-spherical clusters, and interleaved half-moons can all be mislabeled even when the number of clusters is correct.
- To find a failure, compare predicted labels with the structure you expect, check the points near the boundary, and ask whether a neighboring center is nearly as close.
- Choosing the right k controls how many groups appear, but it does not change the shape limits that k-means imposes.