Bootstrap Cluster Stability via Jaccard Matching

Quick Overview

Implement bootstrap cluster stability in Python: recluster random subsamples, match each reference cluster one-to-one to subsample clusters by Jaccard index with the Hungarian algorithm, and average the per-cluster scores. Tests careful handling of noise labels, absent clusters and reproducible sampling.

Bootstrap Cluster Stability via Jaccard Matching

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: medium

Interview Round: Other

Implement `bootstrap_cluster_stability(X, cluster_fn, n_boot=20, fraction=0.8, seed=0)`. Cluster the full `X` once to get reference labels. Then, in each of `n_boot` rounds, recluster a subsample and score how well each reference cluster is recovered. Return `(stability, jac)` with shapes `(k,)` and `(n_boot, k)`, where `k` is the number of reference clusters. The function must follow these rules: - Call `cluster_fn` once on the full `X`, then once per round. Reference clusters are the sorted unique labels excluding `-1` (noise), and they define the column order. - Create `rng = np.random.default_rng(seed)` once. Each round draws `idx = rng.choice(n, size=floor(fraction*n), replace=False)`. - For each reference cluster present in the subsample, compute the Jaccard index between its members in `idx` and each non-noise subsample cluster. Match one-to-one, maximizing total Jaccard (Hungarian). - Present but unmatched clusters score `0.0`, as do all present clusters if the subsample has no clusters. Absent clusters get `NaN`. - `stability` is the mean over each column's non-`NaN` entries, or `NaN` if the cluster is never present. Example (labels are listed by original point index): ```text X = [[0], [1], [2], [3], [4], [5]], fraction = 1.0, n_boot = 4 reference labels = [0, 0, 0, 1, 1, 1] subsample labels = [0, 0, 1, 1, 1, 1] # every round stability -> [2/3, 3/4] jac -> [[2/3, 3/4]] * 4 ``` With `fraction = 1.0` every round samples all six points. `idx` comes back in shuffled order, which is why the subsample labels above are given per original point. ```hint Compare the same points Before comparing sets, make sure the reference labels and the subsample labels describe the same points in the same order. ``` ```hint Three different outcomes A cluster that is absent from the subsample, one that is present but gets no partner, and one that is matched each need a different value in `jac`. Check that your averaging treats them differently too. ``` ```hint Unequal cluster counts The subsample can produce more or fewer clusters than the reference. Make sure your matching step accepts a non-square score matrix. ``` ### Constraints & Assumptions - `cluster_fn(Z)` is provided and returns one integer label per row of `Z`, with `-1` for noise. - Label values from different calls are arbitrary: cluster `0` in a round has no fixed relation to cluster `0` in the reference. - `numpy` and `scipy` are available. ### What a Strong Answer Covers - Reproducible sampling: one generator seeded once, `floor(fraction*n)` indices drawn without replacement each round. - Jaccard scores computed only on the sampled points, between reference members in `idx` and non-noise subsample clusters. - A one-to-one maximum-weight assignment that works when the two cluster counts differ, with unmatched clusters scored 0. - Separate handling of absent clusters (`NaN`), unmatched clusters and all-noise subsamples (`0.0`), and a column mean that ignores `NaN`. - The cost per round and edge cases such as a reference that is all noise. ### Follow-up Questions - Why subsample without replacement instead of a classic bootstrap with replacement? - How would you interpret the stability values in practice? - How would you use this to choose the number of clusters `k`?

Overview: Implement bootstrap cluster stability in Python: recluster random subsamples, match each reference cluster one-to-one to subsample clusters by Jaccard index with the Hungarian algorithm, and average the per-cluster scores. Tests careful handling of noise labels, absent clusters and reproducible sampling.

PracHub logo
General
Nov 24, 2026
mediumMachine Learning EngineerOtherMachine Learning
0
0

Implement bootstrap_cluster_stability(X, cluster_fn, n_boot=20, fraction=0.8, seed=0). Cluster the full X once to get reference labels. Then, in each of n_boot rounds, recluster a subsample and score how well each reference cluster is recovered. Return (stability, jac) with shapes (k,) and (n_boot, k), where k is the number of reference clusters.

The function must follow these rules:

  • Call cluster_fn once on the full X , then once per round. Reference clusters are the sorted unique labels excluding -1 (noise), and they define the column order.
  • Create rng = np.random.default_rng(seed) once. Each round draws idx = rng.choice(n, size=floor(fraction*n), replace=False) .
  • For each reference cluster present in the subsample, compute the Jaccard index between its members in idx and each non-noise subsample cluster. Match one-to-one, maximizing total Jaccard (Hungarian).
  • Present but unmatched clusters score 0.0 , as do all present clusters if the subsample has no clusters. Absent clusters get NaN .
  • stability is the mean over each column's non- NaN entries, or NaN if the cluster is never present.

Example (labels are listed by original point index):

X = [[0], [1], [2], [3], [4], [5]], fraction = 1.0, n_boot = 4
reference labels = [0, 0, 0, 1, 1, 1]
subsample labels = [0, 0, 1, 1, 1, 1]   # every round
stability -> [2/3, 3/4]
jac       -> [[2/3, 3/4]] * 4

With fraction = 1.0 every round samples all six points. idx comes back in shuffled order, which is why the subsample labels above are given per original point.

Constraints & Assumptions

  • cluster_fn(Z) is provided and returns one integer label per row of Z , with -1 for noise.
  • Label values from different calls are arbitrary: cluster 0 in a round has no fixed relation to cluster 0 in the reference.
  • numpy and scipy are available.

What a Strong Answer Covers Guidance

  • Reproducible sampling: one generator seeded once, floor(fraction*n) indices drawn without replacement each round.
  • Jaccard scores computed only on the sampled points, between reference members in idx and non-noise subsample clusters.
  • A one-to-one maximum-weight assignment that works when the two cluster counts differ, with unmatched clusters scored 0.
  • Separate handling of absent clusters ( NaN ), unmatched clusters and all-noise subsamples ( 0.0 ), and a column mean that ignores NaN .
  • The cost per round and edge cases such as a reference that is all noise.

Follow-up Questions Guidance

  • Why subsample without replacement instead of a classic bootstrap with replacement?
  • How would you interpret the stability values in practice?
  • How would you use this to choose the number of clusters k ?