How does the k-means clustering algorithm work, and what are its limitations?
K-means is an unsupervised algorithm that partitions data into k clusters. You choose k, initialize k centroids, then iterate two steps until convergence: assign each point to its nearest centroid (by Euclidean distance), then recompute each centroid as the mean of its assigned points. This minimizes within-cluster variance. Limitations: you must specify k in advance (often chosen via the elbow method or silhouette score); results depend on initialization, so k-means++ is used for smarter seeding; it assumes spherical, similarly sized clusters and struggles with elongated or varying-density shapes; it is sensitive to outliers and feature scaling, so standardize features first. For non-spherical clusters, DBSCAN or Gaussian mixture models are better alternatives. It converges to a local optimum, so multiple restarts are common.