K-Means Clustering
Python · Machine Learning (Scikit-Learn)
Theory
K-Means is an unsupervised algorithm that splits rows into k groups of similar points. It starts with k random centres, assigns every point to its nearest centre, moves each centre to the mean of its assigned points and repeats until the assignments stop changing. labels_ gives each row's cluster and cluster_centers_ the centres. n_init=10 runs the whole process from 10 different starts and keeps the best, because one bad start can give poor clusters.
You must choose k. The elbow method fits k = 1, 2, 3 ... and plots inertia_, the total squared distance from points to their centres. Inertia always falls as k grows, but the drop flattens after the natural number of groups, which forms an elbow in the curve. Scale features first, since K-Means uses distances, and remember it finds round, similar-sized clusters best.
Why we use it
Many datasets have no labels but hide natural groups, such as customer segments by visits and spend. K-Means finds them quickly, so marketing, pricing or product teams can treat each segment differently.
When to use it
Use K-Means for segmentation on scaled numeric features when you expect compact, roughly round groups. Try the elbow method and inspect the centres to name the segments. For odd-shaped clusters or noise, DBSCAN or hierarchical clustering fit better.
How it works
- Scaled numeric features
- Pick k and place k starting centres
- Assign every point to its nearest centre
- Move each centre to the mean of its points
- Did any assignment change? Yes: Repeat assign and move. No: Converged.
- labels_, cluster_centers_, inertia_
Syntax
from sklearn.cluster import KMeans
km = KMeans(n_clusters=3, n_init=10, random_state=0)
km.fit(X_scaled)
km.labels_ km.cluster_centers_ km.inertia_
km.predict(X_new_scaled)Example
km = KMeans(n_clusters=3, n_init=10, random_state=0).fit(X_scaled)
print(km.labels_[:10])
print(km.inertia_)Key points
- Unsupervised: groups rows without labels
- Assign to nearest centre, move centres, repeat
- Scale features first; it uses distances
- Pick k with the elbow in the inertia curve
- n_init runs several starts and keeps the best