Clustering with k-means
Learn clustering with k-means for groups in this Applied AI engineering tutorial — hands-on steps, troubleshooting, and what to study next.
Focus: clustering with k-means for groups
You have rows of customer purchase data, log entries, or sensor readings, but no labels telling you which group each row belongs to. Plotting the data by hand is impossible at scale, and guessing groups by eyeballing a few samples leads to inconsistent, untrustworthy segments. Clustering with k-means for groups solves exactly this problem: it automatically partitions your data into distinct, coherent groups based purely on how similar the points are to one another — no labels required.
The problem this lesson solves
Real-world data rarely arrives with group labels attached. A marketing team might have customer transaction histories but no customer segments. A security engineer might have network flow logs but no known attack patterns. In both cases, you need a way to discover structure in unlabeled data — and that's precisely the job of unsupervised learning.
Without clustering, you face three unpleasant choices:
- Manually label every data point, which is expensive and error-prone.
- Use simple rules (e.g., "if total spend > $500, call them premium"), which oversimplify complex patterns.
- Skip grouping altogether and treat every point as unique, losing the power of aggregation.
The pain becomes acute when you have millions of rows and dozens of features. k-means is the workhorse algorithm that turns this chaos into order, giving you a label for every point and a centroid that represents the 'average' of each group.
Why now? As a data-driven engineer, you'll be asked to segment customers, detect anomalies, or compress large datasets. k-means is the foundational tool you'll reach for first.
Core concept / mental model
Think of k-means as the group photo organizer. Imagine you have 50 people at a party and want to split them into 3 groups for a photo. You'd ask 3 people to stand as 'anchors', then everyone walks to the nearest anchor. Once everyone is positioned, you move each anchor to the center of its group, and repeat until no one changes group. That's k-means in a nutshell.
Key definitions:
- k: the number of groups (clusters) you want to find — you choose it upfront.
- Centroid: the center (mean) of a cluster, updated each iteration.
- Assignment step: each point is assigned to the nearest centroid (using Euclidean distance).
- Update step: each centroid moves to the mean of its assigned points.
- Convergence: when assignments stop changing, the algorithm stops.
The algorithm minimizes the within-cluster sum of squares (WCSS), which measures how compact each cluster is. Lower WCSS means tighter, more coherent groups.
Here's a visual mental model in words:
Iteration 1: Random centroids → points assigned by distance
Iteration 2: Centroids move to cluster means → assignments update
... Repeat until stable
Final: Stable clusters + final centroids
Key intuition: k-means finds clusters that are roughly spherical and similar in size. If your data has very different densities or non-spherical shapes, other algorithms (like DBSCAN) might be a better fit.
How it works step by step
Let's trace the exact steps the algorithm follows, because this mental model will help you debug everything later.
- Choose k — the number of clusters. You'll often decide this using the elbow method or domain knowledge.
- Initialize centroids — randomly pick k points from the dataset (k-means++) is the default in scikit-learn.
- Assign each point to the nearest centroid using Euclidean distance.
- Update centroids — compute the mean of all points in each cluster, and move the centroid there.
- Repeat steps 3–4 until centroids stop moving significantly or a maximum number of iterations is reached.
- Return cluster labels and final centroids.
The euclidean distance formula for two points (a) and (b) in n-dimensional space is:
[ \text{dist}(a, b) = \sqrt{\sum_{i=1}^{n} (a_i - b_i)^2} ]
Because k-means uses distance, feature scaling is critical — otherwise a feature measured in dollars (0–10,000) will dominate one measured in units (0–10).
Convergence: the algorithm reaches a local optimum, not necessarily the global one. That's why initialization matters — different random starts can produce different clusterings.
Hands-on walkthrough
Let's implement clustering with k-means for groups on a synthetic dataset. We'll use scikit-learn and matplotlib, which are standard in any Python AI environment.
Setup and data creation
First, install the dependencies if you haven't already:
pip install scikit-learn matplotlib numpy
Now create a dataset with well-separated blobs — this makes it easy to see if clustering works:
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import make_blobs
from sklearn.cluster import KMeans
from sklearn.preprocessing import StandardScaler
# Generate 300 points in 2D with 4 true centers
X, y_true = make_blobs(n_samples=300, centers=4, cluster_std=0.60, random_state=42)
# Standardize the features (optional for 2D equal-scale data, but good practice)
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)
print(f"Data shape: {X_scaled.shape}")
Expected output:
Data shape: (300, 2)
Running k-means
Now apply k-means with k=4 (we know the true number, but you'll learn how to guess it next):
kmeans = KMeans(n_clusters=4, init='k-means++', n_init=10, random_state=42)
kmeans.fit(X_scaled)
# Get cluster labels and centroids
labels = kmeans.labels_
centroids = kmeans.cluster_centers_
print("Cluster labels for first 10 points:", labels[:10])
print("Centroids:\n", centroids)
Expected output (labels will be 0–3, order might differ):
Cluster labels for first 10 points: [3 2 0 1 2 0 0 3 1 2]
Centroids:
[[-0.14 -0.83]
[-0.97 0.66]
[ 1.03 0.62]
[ 0.34 -1.02]]
Visualizing the result
A quick plot shows how well the algorithm found the natural groups:
plt.scatter(X_scaled[:, 0], X_scaled[:, 1], c=labels, cmap='viridis', alpha=0.7, s=50)
plt.scatter(centroids[:, 0], centroids[:, 1], marker='x', c='red', s=200, label='Centroids')
plt.title("K-Means Clustering (k=4)")
plt.legend()
plt.grid(True)
plt.show()
You'll see four distinct clusters, each with a red X near its center. The clustering matches your intuition perfectly.
Finding the right k
What if you don't know how many groups exist? Use the elbow method: plot WCSS (inertia) against k and look for a bend.
inertias = []
K_range = range(1, 11)
for k in K_range:
km = KMeans(n_clusters=k, init='k-means++', n_init=10, random_state=42)
km.fit(X_scaled)
inertias.append(km.inertia_)
plt.plot(K_range, inertias, 'bo-')
plt.xlabel('Number of clusters (k)')
plt.ylabel('Inertia (WCSS)')
plt.title('Elbow Method for Optimal k')
plt.show()
In this plot, the inertia drops sharply until k=4, then levels off — that's your elbow, and it points to the true number of groups.
Pro tip: The elbow method is subjective. Combine it with domain knowledge and maybe the silhouette score for a data-driven second opinion.
Compare options / when to choose what
k-means is fast and easy, but it isn't the only clustering algorithm. Here's how it stacks up against two common alternatives:
| Algorithm | Best for | Pros | Cons |
|---|---|---|---|
| K-Means | Well-separated, spherical clusters | Fast, scalable, simple to interpret | Requires choosing k, sensitive to outliers, assumes equal size |
| DBSCAN | Arbitrary shapes, clusters of varying density | No k needed, finds outliers | Sensitive to eps parameter, struggles with high dimensions |
| Hierarchical | Nested clusters, small datasets | Dendrogram provides insight | Computationally heavy for big data |
When to choose k-means:
- You have a large dataset (millions of rows) and need speed.
- Your clusters are roughly spherical and similar in size.
- You want a simple, interpretable result.
When to avoid it: - Your clusters are elongated, crescent-shaped, or have wildly different sizes. - You have heavy outliers that could skew centroids. - You don't know k and can't justify a guess.
Alternatives to consider: k-medoids (more robust to outliers), Gaussian Mixture Models (allow soft assignments and varying cluster shapes), and Mini-Batch K-Means (even faster for huge datasets).
Troubleshooting & edge cases
Even when everything seems correct, k-means can fail in subtle ways. Here are the issues you're most likely to hit — and how to fix them.
| Symptom | Likely cause | Fix |
|---|---|---|
| Empty clusters after training | One centroid never attracts any points | Use n_init=10 plus k-means++ initialization; try different random seeds |
| Clusters look mangled or merged | Features are on different scales | Apply StandardScaler before clustering |
| Massive differences in cluster sizes | k doesn't match the data structure | Re-evaluate k with the elbow method or silhouette score |
| Centroids don't converge after many iterations | Very high-dimensional data or too many clusters | Increase max_iter, use Mini-Batch K-Means, or reduce dimensions with PCA |
| Results differ every run | Random initialization | Set random_state for reproducibility |
Common mistakes and the exact fix:
- Forgetting to scale features: Without scaling, a feature like 'income' (0–100k) will dominate the distance calculation. Fix: always run
StandardScaler(orMinMaxScaler) before fitting. - Choosing k arbitrarily: Don't guess. Use the elbow method and silhouette score together.
- Using k-means on non-spherical data: If your clusters are interlocking crescents, k-means will cut them incorrectly. Use DBSCAN instead.
- Ignoring outliers: An extreme outlier can pull a centroid toward itself, mangling the clustering. Clip or remove outliers first.
What you learned & what's next
You've covered the essentials:
- Core concept: k-means groups unlabeled data into k clusters by minimizing within-cluster distance.
- Step-by-step mechanics: assignment → update → repeat until convergence.
- Hands-on practice: you applied k-means to synthetic blobs, visualized clusters, and used the elbow method to find k.
- Comparison skills: you know when k-means is the right tool and when to prefer DBSCAN or hierarchical clustering.
- Troubleshooting confidence: you can diagnose scaling issues, empty clusters, and convergence problems.
Next lesson in the track: you're building toward a full applied AI toolkit. After mastering k-means, you'll likely learn dimensionality reduction with PCA — a perfect companion for visualizing high-dimensional clusters or speeding up training. Keep that momentum going!
Ready to practice? Re-run the code with a different number of true centers (e.g., centers=5) and see if the elbow method finds it.
Practice recap
Revisit the hands-on notebook: create a synthetic dataset with make_blobs using centers=5 and run k-means with the elbow method to see if it finds the right k. Then, apply k-means to a real dataset like the Iris dataset (without labels) and compare your cluster assignments to the true species — expect some overlap since Iris has non-spherical clusters, which shows the algorithm's limits.
Common mistakes
- Forgetting to scale features: k-means uses Euclidean distance, so a feature like salary (0–100k) will dominate a feature like age (0–100). Standardize first.
- Choosing k arbitrarily: pick k with the elbow method plus silhouette score, not vibes.
- Using k-means on non-spherical or imbalanced clusters: if your groups are crescents or have wildly different sizes, use DBSCAN or hierarchical clustering instead.
- Ignoring outliers: a single extreme value can pull a centroid toward itself, distorting the entire clustering. Remove or clip outliers before fitting.
- Not setting random_state: 'k-means++' is random, so your results will differ between runs unless you set a seed — set it for reproducible experiments.
Variations
- Mini-Batch K-Means (e.g.,
MiniBatchKMeansin scikit-learn) is a faster, stochastic variant that processes data in small batches — ideal for huge datasets. - K-medoids (like PAM) uses actual data points as cluster centers instead of means, making it far more robust to outliers.
- Gaussian Mixture Models (GMM) allow probabilistic (soft) assignments and can model clusters with different shapes and orientations, unlike k-means' strictly spherical assumption.
Real-world use cases
- Customer segmentation: grouping users by purchase history and browsing behavior to tailor marketing campaigns.
- Anomaly detection: clustering normal network traffic, then flagging points that fall far from any centroid.
- Image compression: quantizing image colors by clustering pixel RGB values and replacing each pixel with its cluster centroid.
Key takeaways
- k-means is an unsupervised learning algorithm that partitions data into k clusters by minimizing within-cluster variance.
- The algorithm iterates between assigning points to the nearest centroid and updating centroids to the cluster mean until convergence.
- Feature scaling is mandatory; k-means uses distance, and unscaled features will dominate the clustering.
- Choosing k is a key step: use the elbow method or silhouette score, and combine with domain knowledge.
- k-means works best for spherical, similarly sized clusters; for other shapes, consider DBSCAN or hierarchical clustering.
- Set a random_state and use multiple initializations (n_init) to get stable, reproducible results.
Keep learning
Related tutorials, quizzes, and articles for this topic.
Discussion
Questions, corrections, and tips help everyone reading this page.
0 comments
Add a comment
No comments yet — start the thread.