K-Means Clustering
Python · Advanced Python & AI
Theory
K-Means is an unsupervised algorithm that groups unlabelled data into K clusters. It starts by placing K centroids, assigns every point to its nearest centroid, moves each centroid to the mean of the points assigned to it, and repeats the assign-and-update steps until the assignments stop changing. The result is a cluster label for every point and a centre for every cluster (cluster_centers_).
In the diagram, six points form two obvious groups; with n_clusters=2 the first three get label 0 (centre about (1.33, 3)) and the last three label 1 (centre about (8.33, 8)). The label numbers themselves are arbitrary. You must choose K: the elbow method plots inertia_ (total squared distance to centres) for several K values and looks for the bend. Scale features first, set random_state for repeatable results, and remember K-Means assumes roughly round, similar-sized clusters.
Why we use it
Often there is no label to predict, only data to understand. K-Means finds natural groups - customer segments, product types, behaviour patterns - so you can describe and act on each group.
When to use it
Use it for segmentation on numeric, scaled features when you expect compact, roughly spherical clusters. For odd shapes or noise points try DBSCAN; when you do have labels, use a classifier instead.
How it works
- Choose K and place K initial centroids
- Assign every point to its nearest centroid
- Move each centroid to the mean of its points
- Did any assignment change? Yes: Repeat assign and update. No: Stop: clusters are stable.
- Cluster labels and centroids
Syntax
from sklearn.cluster import KMeans
model = KMeans(n_clusters=3).fit(X)Example
model = KMeans(n_clusters=3, random_state=42)
labels = model.fit_predict(X)Key points
- Unsupervised - no labeled target needed
- The 'elbow method' helps choose a good value for K
- Scale features first - it is distance based
- Label numbers are arbitrary; only the grouping matters
- Assumes compact, roughly round clusters