K-means objective and updates
Quick answer
Clustering groups observations without using target labels. K-means partitions numeric vectors into k groups by minimizing the sum of squared distances to their assigned centers. A center is the arithmetic mean of its assigned vectors. Static examples tested locally with Python 3.13.5, scikit-learn 1.8.0 SciPy 1.17.0 and NumPy 2.3.5 where imported. ByteMind package execution is unverified.
One Lloyd update
x=[0,2,8,10]; c=[0,8]a=[min(range(2),key=lambda j:(xv-c[j])**2) for xv in x]new=[sum(v for v,g in zip(x,a) if g==j)/a.count(j) for j in range(2)]print(a,new)print(sum((v-new[g])**2 for v,g in zip(x,a)))Observed local output:
[0, 0, 1, 1] [1.0, 9.0]4.0Each center is the mean of two assigned points.
How it works
For observations x_i and k centers c_j, minimize J = sum_i min_j ||x_i-c_j||². Lloyd’s algorithm alternates nearest-center assignment and mean updates. For fixed assignments the mean minimizes squared error: differentiate the sum of squared deviations with respect to the center and set the derivative to zero. For fixed centers, assigning each point to the nearest center minimizes its contribution. These two steps cannot increase J when groups remain nonempty; a stopping tolerance or iteration cap terminates the search, but global optimality is not guaranteed. For points 0,2,8,10 with centers 0,8, the assignments are [0,0,1,1]; updating yields centers 1 and 9 and lowers J from 8 to 4. Empty groups require an explicit policy, such as reseeding a center; the arithmetic exercise does not update empty groups. K-means++ starts with a randomly chosen observation and favors later seeds far from existing centers (squared distance weighting; scikit-learn uses a greedy variant). Multiple starts reduce dependence on initialization. Specify n_init and random_state rather than rely on changing defaults. Inertia is scale dependent and typically decreases as k increases, so minimizing it across k alone favors excessive splitting. Nearest-center cells are convex, making curved clusters difficult. Scale features according to meaningful units; do not standardize automatically when units already express the desired geometry. For n rows, d features, k centers and t iterations, a basic dense implementation uses O(nkd*t) arithmetic.
Fit a native model
from sklearn.cluster import KMeansm=KMeans(n_clusters=2,n_init=10,random_state=7).fit([[0],[2],[8],[10]])print(sorted(m.cluster_centers_.ravel().tolist()),m.inertia_)Observed local output:
[1.0, 9.0] 4.0Sorting centers avoids depending on arbitrary cluster ID order.
Common mistakes
Do not compare integer cluster IDs with class labels directly: swapping cluster IDs does not change the partition. Do not promise that repeated starts find the global optimum or use lowest inertia alone to select k. For a downstream prediction pipeline, fit scaling and centers on training rows only.
Practice and next steps
Start with centers 0 and 8 on the four-point fixture. Calculate inertia before and after the mean update; change the first center to 2 and repeat. Explain why a distant point can pull a mean away from most observations. Complete the knowledge check and staged exercise. Next: Hierarchical linkage.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
K-means objective and updates — knowledge check
Apply k-means objective and updates to new scenarios.
Which quantity is minimized by K-means?
What is a centroid after a nonempty assignment?
What does k-means++ change?
Can inertia select k by itself?
What is preserved by permuting cluster IDs?