Move points one mean-shift step

Why it matters Mode-seeking like this is used to group near-duplicate embeddings without choosing k.

Mean shift finds cluster modes by moving every point to the kernel-weighted average of the data around it.

Write mean_shift_step(points, X, bandwidth). For each row p of points, weights are w_i = exp(-||p - X_i||^2 / (2 * bandwidth^2)) over all rows X_i of X. Return the array of new positions sum_i w_i X_i / sum_i w_i.

Signaturedef mean_shift_step(points, X, bandwidth)

Example

points [[0]], X [[-1], [1]], any bandwidth gives [[0.0]] by symmetry.

Understand the idea

Mean shift finds the peaks of data density. Every point looks at all data points, gives nearby ones a big weight through a Gaussian kernel, and moves to their weighted average. Repeating this walks points uphill to a mode. Like people in a hall drifting toward the busiest corner near them. Worked example: point p = 0, data X = [0, 2], bandwidth 1. Weights are exp(-0 / 2) = 1 and exp(-4 / 2) = exp(-2) = about 0.135. New position = (1 * 0 + 0.135 * 2) / (1 + 0.135) = about 0.238. The point moved a little toward 2, but stays near 0 because that data point is much closer. Rule: w_i = exp(-||p - X_i||^2 / (2 * bandwidth^2)), new p = sum(w_i X_i) / sum(w_i). The bandwidth is squared inside the exponent; dividing by 2h instead silently changes the kernel width. Do this for every row of points at once with a weight matrix. Used in: clustering without choosing k, image segmentation, and object tracking in video.

Shapes and data flow

P (m, 1, d) - X (1, n, d) -> d2 (m, n) -> W (m, n)
W @ X -> (m, d), / W.sum(1) -> new points (m, d)
solution.pyPython idle
Ctrl/Cmd + Enter runs sample tests

Run checks the first 2 tests. Submit checks the hidden tests with Pro.

  • Sample 1: examplemean_shift_step([[0]], [[-1], [1]], 1.0)
  • Sample 2: toward clustermean_shift_step([[0.0]], [[1.0], [1.2], [5.0]], 1.0)