Lab: a higher silhouette, the wrong groups
The clustering chapter's first lab. On two noisy moons, k-means scores the higher silhouette while a density method recovers the groups you meant. Explore it, then compute both scores yourself in Python or R.
What runs in CodeSplash
- UCL001K-means Runs in CodeSplash
- UCL021DBSCAN Runs in CodeSplash
- EVA001Silhouette coefficient Runs in CodeSplash
- EVA010Adjusted Rand index Runs in CodeSplash
- k-means clustering Verified in Python and R
- DBSCAN Verified in Python and R
- Spectral clustering Verified in Python and R
You will be able to
- Compute a silhouette by hand and say what geometry it rewards
- Use agreement with known labels only to evaluate, never to fit or tune
- Explain why the clustering with the better internal index can miss the intended groups
- Run the same comparison in Discover on a sample with curved groups
The question
ReadOn data with curved groups, does the clustering with the better silhouetteSilhouette scoreHow well each point sits in its cluster compared with the nearest other cluster, from −1 to 1. also find the groups you meant? The lab uses two interleaved half-moons, simulated so the true moon of every point is known. That label is used only to evaluate the clusterings, never to fit them or to choose a setting.
Two methods compete. K-meansk-meansSplits rows into k groups by repeatedly assigning each row to its nearest center and moving each center to its group's mean. draws compact, round groups around two centers. A density method links points that sit close together, so a group can follow a curve. Before you look, guess which one scores the higher silhouette.
Explore it
ExploreAExplorable · Spectral clustering: cut a neighbor graph
How can clustering follow curved shapes that k-means slices straight through?
Which recovers the planted groups better on this data: k-means on the points, or k-means on the graph's eigenvectors?
Data
Commit your prediction above to see the result
Make your prediction to see the result.
Set the data to two moons and compare the two agreement scores: one for a method that follows the neighbor graph, one for k-means on the raw points. Then press Break it and watch the graph lose the gap between the moons. Agreement with the true moons is the adjusted Rand indexAdjusted Rand indexAgreement between two clusterings corrected for chance; 0 is random, 1 is identical.: near 1 is a match, near 0 is chance.
Compute both scores
ExploreThe notebook generates the moons in Python or R with the same random numbers in both, runs k-means and a density clustering, and asks you to compute the silhouette by hand for each, then to check which one recovers the moons.
Notebook exercise · ~15 min
A higher silhouette, the wrong groups
Score two clusterings of two noisy moons by silhouette and by agreement with the true groups, and see the two disagree.
- Silhouette of k-means
For each point,
ais its mean distance to the other points of its own cluster andbits mean distance to the points of the other cluster; its silhouette is (b − a) / max(a, b). Store the mean silhouette of the k-means clusteringkminsil_km.Show a solution
Pythondef silhouette(l): s = [] for i in range(n): own = l == l[i] a = D[i, own].sum() / (own.sum() - 1) b = D[i, ~own].mean() s.append((b - a) / max(a, b)) return np.mean(s) sil_km = silhouette(km)Rsilhouette <- function(l) { mean(sapply(seq_len(n), function(i) { own <- l == l[i] a <- sum(D[i, own]) / (sum(own) - 1) b <- mean(D[i, !own]) (b - a) / max(a, b) })) } sil_km <- silhouette(km) - Silhouette of the density clusters
Store the mean silhouette of the density clustering
densityinsil_density. Which of the two has the higher silhouette?Show a solution
Pythonsil_density = silhouette(density)Rsil_density <- silhouette(density) - Which one finds the moons?
Use
ari(labels, truth)to see how well each clustering recovers the two moons. Store the name of the clustering that recovers them better,"km"or"density", inrecovers.Show a solution
Pythonrecovers = "density" if ari(density, truth) > ari(km, truth) else "km"Rrecovers <- if (ari(density, truth) > ari(km, truth)) "density" else "km"
In the app this opens as a Python or R notebook with the sample data, and its checks mark your progress.
In CodeSplash
In the appDiscover's Find segments runs the same comparison on the rings sample, two concentric rings with a known ring label. The walkthrough has you run k-means and DBSCAN with the ring as the label, and read the silhouette and the agreement for each.
Check: the lab
CheckpointCheck yourself
Why does k-means usually score the higher silhouette on two moons?
Show the answer
Answer: a
Silhouette compares each point's mean distance to its own group with the nearest other group. Round, compact halves score well even when they cut across the curves.
Check yourself
The true moon labels are known here. What may they be used for?
Show the answer
Answer: a
Tuning a setting on the labels you evaluate with turns the evaluation into a fit. Fix the settings in advance, or judge them on structure you'd have without labels.
Check yourself
On real data with no labels, what does this lab say about choosing between k-means and a density method?
Show the answer
Answer: a
The density method won on this simulated data, which isn't evidence that it's better in general. Internal indices measure geometry; whether that geometry matches your groups needs other evidence.
Recap
Recap- Silhouette rewards compact distance geometry, not the groups you meant.
- On curved groups, k-means can score higher and agree worse with the truth.
- Labels in a simulation are for evaluation only; never tune on them.
- Judge a clustering against the structure you need and outside evidence.
Concepts in this lesson