Skip to content
Tutorials Center
My learning

Topics

Lesson 30 of 39 ยท Clustering

Evaluating Clusters

Judge a clustering without guessing: clustering tendency (Hopkins), internal indices (silhouette, Calinski-Harabasz, Davies-Bouldin), external indices (ARI, NMI, purity) and stability checks.

Every clustering algorithm returns clusters, even from random noise. So before trusting a result you have to ask three questions: is there any cluster structure at all, how good are the clusters, and how many clusters are justified. This lesson answers them with measures you can compute yourself.

1. Is there anything to cluster? (clustering tendency)

The Hopkins statistic compares nearest-neighbor distances of real records with those of random points in the same space. Values near 0.5 mean the data is uniformly random; values near 1 mean it is strongly clustered. Here is a compact version.

Python Hopkins statistic

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

2. Internal measures: judge with the data alone

Internal indices use only the data and the cluster labels. They reward clusters that are compact (records close to their own center) and separated (centers far apart).

  • Silhouette score: per record s = (b - a) / max(a, b) where a is the mean distance to its own cluster and b the mean distance to the nearest other cluster. Ranges -1 to 1, higher is better; negative values mean a record probably sits in the wrong cluster.
  • Calinski-Harabasz (variance ratio): between-cluster spread divided by within-cluster spread. Higher is better.
  • Davies-Bouldin: average similarity between each cluster and its most similar neighbor. Lower is better, and 0 is the minimum.
Python Three internal indices across k

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

All three agree on the best k: highest silhouette, highest Calinski-Harabasz and lowest Davies-Bouldin. Agreement across indices is reassuring; disagreement is a sign the structure is ambiguous.

The silhouette per record

Averages hide the weak spots. Look at the silhouette of each cluster and the share of negative values.

Python Per-cluster silhouette

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Internal indices favor compact, round clusters. A density-based clustering of two crescents can score worse than a poor k-means split although it is the correct answer, so do not apply them blindly to non-convex shapes.

3. External measures: compare with known groups

If you happen to have ground-truth labels (for a benchmark, or an expert's partial labeling), compare them with the clusters:

  • Adjusted Rand Index (ARI): counts pairs of records grouped the same way in both partitions, corrected for chance. 1 is identical, 0 is random, it can be negative.
  • Normalized mutual information (NMI): how much knowing the cluster tells you about the true label, from 0 to 1.
  • Purity: for each cluster take the share of its majority true class; average weighted by size. It is simple but grows trivially with more clusters.
Python External indices on iris

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

4. Stability: would another sample give the same clusters?

A real structure survives resampling. Cluster two random 80% subsamples and compare the labels on their shared records with ARI. High values mean stable clusters; values that jump around mean the clusters are an artifact.

Python Stability by resampling

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Stability stays near 1 up to the true number of groups (4) and then falls, because larger k splits blobs in arbitrary ways that change from sample to sample. Stability alone cannot reject a too-small k, so combine it with the internal indices.

From numbers to meaning

Metrics do not replace interpretation. For each cluster, list the average value of every attribute and the cluster size, name the segment, and ask whether someone could act on it. A "perfect" silhouette with clusters no one can describe is less useful than a slightly lower score with a clear story. The customer segmentation project does this end to end.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. A Hopkins statistic near 0.5 suggests the data is:

    Values near 0.5 mean real points are no closer to each other than random points, so there is little cluster structure.
  2. 2. Which direction is better for the Davies-Bouldin index?

    Davies-Bouldin measures cluster similarity; smaller values mean more separated, more compact clusters.
  3. 3. When can you use ARI?

    ARI is an external index: it compares a clustering with a known partition.
  4. 4. What does a negative silhouette value for a record indicate?

    A negative value means b < a: the record is nearer to a neighboring cluster than to its own.

Summary

  • Check clustering tendency first (Hopkins), because algorithms cluster noise too.
  • Internal indices (silhouette, Calinski-Harabasz, Davies-Bouldin) score compactness and separation; external ones (ARI, NMI, purity) compare to known labels.
  • Stability under resampling and interpretability decide whether clusters are real and useful.

Next: Outlier Detection: Statistical Methods