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.

01

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

namegender
Johnmale
Janefemale
Bobmale
Sarahfemale

Encoded

namegender_encoded
John1
Jane0
Bob1
Sarah0

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

nameageincome
John2050000
Jane3080000
Bob2560000
Sarah40100000

Scaled (MinMax)

nameageincome
John00
Jane0.50.6
Bob0.250.2
Sarah11

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)

ageincomesavingsdebt
2845000100005000
35600002000010000
45800005000020000
223000050002000
38700003000015000

Reduced (2 PCs)

PC1PC2
-1.45-0.12
0.32-0.08
3.180.21
-2.840.05
0.79-0.06
02

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.”

K-means partitions by centroid proximity — works best with convex, similarly-sized clusters.
DBSCAN finds dense regions and marks outliers as noise — no need to specify k.
03

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.

Key insight
Start with just Annual Income and Spending Score to see the classic customer segmentation pattern. Then add more features and watch how PCA reduces the dimensionality while preserving cluster structure.
Select features for clustering

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

ClusterCountAvg Annual Income ($)Avg Spending Score
Cluster 138$25,08419
Cluster 244$53,30648
Cluster 337$98,72281
Cluster 435$100,29919
Cluster 546$25,89675