BLOGLLM Logo
Go Pro
Guest
Not signed in
All BlogsCoursesPracticeInterview prepSeriesTagsBookmarksPricing
Log in
All BlogsCoursesPracticeTagsBookmarksPricing
My PrepProblemsStudy PlansResearch PapersRoadmapInterviewContestsLeaderboardGames
Articles
Practice / Unsupervised learning6 of 15 in topic

142. Pick k-means++ starting centers deterministically

MediumUnsupervised learning Pro
Asked atAnthropicMeta

Why it matters Embedding clustering for topic discovery and IVF index training starts from k-means++.

k-means++ spreads the starting centers out: each new center is chosen with probability proportional to its squared distance from the nearest center chosen so far.

Write kmeanspp_init(X, k, u). To make it testable, randomness is given: u is a list of k numbers in [0, 1). The first center is X[floor(u[0] * n)]. For each next center i, compute D2 (squared distance of every point to its nearest chosen center), form the cumulative sum c of D2 / sum(D2), and pick the first index where c > u[i]. Return the (k, d) array of centers.

Functiondef kmeanspp_init(X, k, u)

Example 1

X = [[0], [1], [10]], k = 2, u = [0.0, 0.5]: first center 0, D2 = [0, 1, 100], cumulative [0, 0.0099, 1], so the second center is [10].

Constraints

  • Python 3 with NumPy. Code runs in your browser.
  • Run checks the sample tests. Submit checks the full hidden test set (Pro).
Hint 1

np.searchsorted(c, u[i], side='right') finds the first index with c > u[i].

Hint 2

Distance of every point to every chosen center, then min over centers.

Hint 3

Normalise D2 and take a cumulative sum.

Hint 4

searchsorted with side='right' gives the first c > u.

Common mistakes
  • Using max instead of min over chosen centers.
  • Using side='left', which can pick a point with zero probability.
Understand the idea

Plain k-means can start with all centers bunched together and end up stuck. k-means++ picks each new center at random, but with probability proportional to the squared distance from the nearest center already chosen, so far-away uncovered points are likely picks. Here the randomness is passed in as numbers u so results are testable.

Worked example

X = [[0], [2], [5]], k = 2, u = [0.5, 0.3]. The first center is X[floor(0.5 * 3)] = X[1] = [2]. D2 = [4, 0, 9], sum 13, so the normalised weights are [0.308, 0, 0.692] and the cumulative sum is [0.308, 0.308, 1.0]. The first index where the cumulative sum exceeds 0.3 is 0, so the second center is [0]. With u[1] = 0.4 it would be [5].

first center X[floor(u[0] * n)]; then D2_j = min over chosen centers of squared distance, c = cumsum(D2 / sum(D2)), pick the first j with c_j > u[i]

Used in: the default initialisation in scikit-learn KMeans and in codebook training for vector quantisation.

Topics
Unsupervised learning
Companies2
  • Anthropic the topic appears in interview reports Coding Exercises & ML System Design: Real AI Interview Questions (landedjobs)
  • Meta a candidate reported this question Machine Learning interview questions, page 10 (KatKat)

From public interview reports and guides. Not affiliated with or endorsed by these companies.

Similar questions
  • Compute soft cluster memberships for a Gaussian mixtureMedium
  • Merge points bottom-up with single linkage clusteringMedium
  • Move points one mean-shift stepMedium
  • Run k-means to convergence and re-seed empty clustersMedium
All Unsupervised learning problems
Report
142 / 903
Python 3solution.pyPython idle
1
2
3
4
5
6
import numpy as np
​
def kmeanspp_init(X, k, u):
# your code here
pass
​

Run checks these cases in your browser, no sign-in needed. Submit checks the full hidden test set with Pro.

Ctrl/Cmd + Enter to run
© BlogLLM, Bengaluru, IndiaAboutTermsPrivacyRefundsDMCAContactRSS