Python / Python Mathematical Intuition and Scikit Learn Interview Questions
What is the mathematical objective function K-Means optimises, and why can it converge to a local minimum?
K-Means seeks to partition n points into k clusters by minimising the within-cluster sum of squares (WCSS), also called inertia: J = Σₖ Σ_{x ∈ Cₖ} ‖x - μₖ‖², where μₖ is the centroid (mean) of cluster k. This is a non-convex combinatorial optimisation problem — finding the global optimum requires checking every possible partition of n points into k groups, which is computationally infeasible (NP-hard) for any realistic dataset size.
Lloyd's algorithm (the standard K-Means algorithm) solves this approximately through alternating optimisation: (1) assign each point to its nearest centroid, (2) recompute each centroid as the mean of its assigned points, repeat until convergence. Each step is guaranteed to never increase J, so the algorithm converges — but only to a local minimum that depends heavily on the random initial centroid positions.
from sklearn.cluster import KMeans import numpy as np # k-means++ initialization (default) spreads initial centroids apart # to reduce the chance of poor local minima â much better than random init km = KMeans(n_clusters=3, init='k-means++', n_init=10, random_state=42) km.fit(X) print('Inertia (WCSS):', km.inertia_) print('Cluster centers:', km.cluster_centers_) # n_init=10 runs the algorithm 10 times with different initializations # and keeps the result with lowest inertia â mitigates local minima # Elbow method to choose k: plot inertia vs k, look for the 'elbow' inertias = [] for k in range(1, 10): km = KMeans(n_clusters=k, n_init=10, random_state=42).fit(X) inertias.append(km.inertia_) # inertia always decreases with more clusters â look for diminishing returns
More Related questions...