Pick spread-out starting centroids with farthest-first seeding
k-means is sensitive to where the centroids start. If two start in the same blob, one real cluster gets split and another gets missed. Farthest-first seeding, a deterministic cousin of k-means++, picks each new seed as the point farthest from all seeds chosen so far.
Write farthest_first(X, k). X has shape (n, d). The first seed is row 0. Then repeat until you have k seeds: for every point compute its squared distance to its nearest chosen seed, and pick the point with the largest such distance. Ties go to the lowest index.
Return the list of k chosen row indices, in the order they were picked. You can assume X has at least k distinct rows.
def farthest_first(X, k)Example
X = [[0, 0], [1, 0], [10, 0], [5, 0]], k = 3. Seed 0 first. Row 2 is farthest (100). Now nearest-seed distances are [0, 1, 0, 25], so row 3 is next. Result [0, 2, 3].
Understand the idea
k-means works badly if two starting centroids land in the same blob. Farthest-first seeding spreads them out: start with row 0, then repeatedly pick the point whose distance to its nearest already-chosen seed is largest. Using the nearest of all seeds, not just the latest one, is the key detail. Like placing new shops as far as possible from every existing shop. Worked example: X = [[0, 0], [0, 1], [0, 8], [6, 0]], k = 3. Seed row 0. Squared distances to it: [0, 1, 64, 36], so row 2 is next. Now nearest-seed squared distances: row 1 is min(1, 49) = 1, row 3 is min(36, 36 + 64 = 100) = 36. Pick row 3. Result [0, 2, 3]. If the latest seed alone were used, a point close to an old seed could be picked again. Rule: keep a min-distance array; after each new seed, update it with min(old, distance to new seed); pick argmax (lowest index on ties). k-means++ is the randomised version used in practice.