Mean Shift Clustering With Gaussian Kernel

Quick Overview

Implement Gaussian-kernel mean shift in NumPy, with a single update step, per-point convergence and greedy merging of converged modes into labeled clusters. It tests kernel density reasoning, numerical stability of exponential weights and careful handling of stopping and merge rules.

Mean Shift Clustering With Gaussian Kernel

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: medium

Interview Round: Other

Implement `mean_shift_step(points, X, bandwidth)`, which moves each row of `points` to the kernel-weighted mean of all of `X` with weights `exp(-||p - x||² / (2·bandwidth²))`. Then implement `mean_shift(X, bandwidth, max_iter=500, tol=1e-7, merge_radius=None)`, which returns `(labels, cluster_centers, modes)`. The functions must follow these rules: - Start every mode at its point and iterate up to `max_iter` times. A point stops updating once its shift norm is `<= tol`. - Merge in index order. A mode joins the nearest existing cluster representative (that cluster's first mode) if it is within `merge_radius` (inclusive); otherwise it starts a new cluster. The default `merge_radius` is `bandwidth`. - Labels are `0..k-1` in order of first appearance. `cluster_centers[k]` is the mean of the modes labeled `k`. - `modes` has shape `(n, d)` and `cluster_centers` has shape `(k, d)`. - Raise `ValueError` when `bandwidth <= 0`, in both functions. Example: ```python X = [[10.0], [0.0], [10.1], [20.0], [0.1], [20.1]] labels, centers, modes = mean_shift(X, bandwidth=0.5) # labels -> [0, 1, 0, 2, 1, 2] # centers -> approximately [[10.05], [0.05], [20.05]] ``` ```hint Numerical safety For a point far from every row of `X`, all the Gaussian weights can underflow to zero. Look for a per-row adjustment to the exponent that leaves the ratios between weights unchanged. ``` ```hint Points converge at different times Each point has its own stopping test. Keep track of which points are still moving and update only those. ``` ### What a Strong Answer Covers - A vectorized `mean_shift_step` that weights all of `X` with the Gaussian kernel and avoids underflow without changing the result. - Per-point convergence with the inclusive `tol` test and the `max_iter` cap. - Greedy merging in index order against each cluster's first mode, with first-appearance labels and an inclusive `merge_radius`. - Centers as means of member modes, the required output shapes, and `ValueError` for a non-positive bandwidth in both functions. - The per-iteration cost and why each step never lowers the kernel density estimate. ### Follow-up Questions - How does this scale, and how would you speed it up? - How would you choose the bandwidth, and what happens when it is too small or too large? - When would you pick mean shift over k-means, and when would you avoid it?

Overview: Implement Gaussian-kernel mean shift in NumPy, with a single update step, per-point convergence and greedy merging of converged modes into labeled clusters. It tests kernel density reasoning, numerical stability of exponential weights and careful handling of stopping and merge rules.

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

Implement mean_shift_step(points, X, bandwidth), which moves each row of points to the kernel-weighted mean of all of X with weights exp(-||p - x||² / (2·bandwidth²)). Then implement mean_shift(X, bandwidth, max_iter=500, tol=1e-7, merge_radius=None), which returns (labels, cluster_centers, modes).

The functions must follow these rules:

  • Start every mode at its point and iterate up to max_iter times. A point stops updating once its shift norm is <= tol .
  • Merge in index order. A mode joins the nearest existing cluster representative (that cluster's first mode) if it is within merge_radius (inclusive); otherwise it starts a new cluster. The default merge_radius is bandwidth .
  • Labels are 0..k-1 in order of first appearance. cluster_centers[k] is the mean of the modes labeled k .
  • modes has shape (n, d) and cluster_centers has shape (k, d) .
  • Raise ValueError when bandwidth <= 0 , in both functions.

Example:

X = [[10.0], [0.0], [10.1], [20.0], [0.1], [20.1]]
labels, centers, modes = mean_shift(X, bandwidth=0.5)
# labels  -> [0, 1, 0, 2, 1, 2]
# centers -> approximately [[10.05], [0.05], [20.05]]

What a Strong Answer Covers Guidance

  • A vectorized mean_shift_step that weights all of X with the Gaussian kernel and avoids underflow without changing the result.
  • Per-point convergence with the inclusive tol test and the max_iter cap.
  • Greedy merging in index order against each cluster's first mode, with first-appearance labels and an inclusive merge_radius .
  • Centers as means of member modes, the required output shapes, and ValueError for a non-positive bandwidth in both functions.
  • The per-iteration cost and why each step never lowers the kernel density estimate.

Follow-up Questions Guidance

  • How does this scale, and how would you speed it up?
  • How would you choose the bandwidth, and what happens when it is too small or too large?
  • When would you pick mean shift over k-means, and when would you avoid it?