Skip to content

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.

Advanced ~25 min6 steps

What runs in CodeSplash

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

Read first

01

The question

Read

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

02

Explore it

Explore

AExplorable · 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?

How sure?

Data

2
8
120
Simulating…

Commit your prediction above to see the result

Make your prediction first; the figure appears when you commit.

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.

03

Compute both scores

Explore

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

  1. Silhouette of k-means

    For each point, a is its mean distance to the other points of its own cluster and b its 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 clustering km in sil_km.

    Show a solution
    Python
    def 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)
    R
    silhouette <- 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)
  2. Silhouette of the density clusters

    Store the mean silhouette of the density clustering density in sil_density. Which of the two has the higher silhouette?

    Show a solution
    Python
    sil_density = silhouette(density)
    R
    sil_density <- silhouette(density)
  3. 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", in recovers.

    Show a solution
    Python
    recovers = "density" if ari(density, truth) > ari(km, truth) else "km"
    R
    recovers <- 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.

04

In CodeSplash

In the app

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

05

Check: the lab

Checkpoint

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

06

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