It’s relatively easy to implement and applies to a wide range of problems — from image segmentation to customer segmentation in marketing. There are many algorithms (k-means, hierarchical, DBSCAN…), each with its strengths and weaknesses.
Starting with clustering: data preprocessing
Clustering is powerful, but very sensitive to how the data is prepared. Below are key preprocessing steps to consider before applying any algorithm.
1.Encoding categorical data
Most clustering algorithms work on numerical data. Categorical features (gender, location, product type…) have to be converted first. A simple option is label encoding, mapping each category to an integer. Another, more robust for many cases, is one-hot encoding which creates one binary column per category.
Original
| name | gender |
|---|---|
| John | male |
| Jane | female |
| Bob | male |
| Sarah | female |
Encoded
| name | gender_encoded |
|---|---|
| John | 1 |
| Jane | 0 |
| Bob | 1 |
| Sarah | 0 |
2.Scaling data
Clustering algorithms are sensitive to the scale of features, so all features should share a comparable scale. Standardization rescales the data to zero mean and unit variance; normalization rescales to a fixed range (typically [0, 1] or [−1, 1]).
Original
| name | age | income |
|---|---|---|
| John | 20 | 50000 |
| Jane | 30 | 80000 |
| Bob | 25 | 60000 |
| Sarah | 40 | 100000 |
Scaled (MinMax)
| name | age | income |
|---|---|---|
| John | 0 | 0 |
| Jane | 0.5 | 0.6 |
| Bob | 0.25 | 0.2 |
| Sarah | 1 | 1 |
3.Applying dimensionality reduction
With high-dimensional data, clustering becomes harder. Dimensionality reduction shrinks the feature space while preserving the most important information. Principal Component Analysis (PCA) and t-SNE are common techniques.
Original (4 dimensions)
| age | income | savings | debt |
|---|---|---|---|
| 28 | 45000 | 10000 | 5000 |
| 35 | 60000 | 20000 | 10000 |
| 45 | 80000 | 50000 | 20000 |
| 22 | 30000 | 5000 | 2000 |
| 38 | 70000 | 30000 | 15000 |
Reduced (2 PCs)
| PC1 | PC2 |
|---|---|
| -1.45 | -0.12 |
| 0.32 | -0.08 |
| 3.18 | 0.21 |
| -2.84 | 0.05 |
| 0.79 | -0.06 |
Choosing the right algorithm
The right algorithm depends on the problem and the data. A few popular ones: k-means (partitional, fast, needs k upfront), DBSCAN (density-based, robust to noise), hierarchical clustering (interpretable dendrograms), and many others. Below, the same blobs of points clustered two ways:
“No single algorithm wins every time. The shape of your data — its density, noise level, and number of natural groups — should guide your choice.”
Try it yourself
The tool below runs K-Means clustering entirely in your browser on a sample dataset of 200 customers. Select which features to include, adjust the number of clusters, and watch the algorithm encode categorical variables, scale the data, apply PCA when needed, and segment the customers — all in real time.
Elbow method — sum of squared distances vs number of clusters
The elbow chart shows how the total within-cluster variance (SSE) decreases as we add more clusters. The “elbow” — where the curve bends and adding more clusters stops giving meaningful improvement — suggests the optimal number of groups in the data. Here, the elbow points to k = 2.
Annual Income ($) vs Spending Score — scaled values
| Cluster | Count | Avg Annual Income ($) | Avg Spending Score |
|---|---|---|---|
| Cluster 1 | 38 | $25,084 | 19 |
| Cluster 2 | 44 | $53,306 | 48 |
| Cluster 3 | 37 | $98,722 | 81 |
| Cluster 4 | 35 | $100,299 | 19 |
| Cluster 5 | 46 | $25,896 | 75 |