Clustering &
Segmentation
Learn how the K-Means algorithm groups data points into meaningful segments: the foundation of customer analytics and market research.
What Is Clustering?
Clustering is the process of dividing a dataset into groups (called clusters) so that data points within each group are more similar to each other than to points in other groups. It is an unsupervised learning technique: you don't need pre-labeled categories.
Why Clustering Matters in Business
Supervised vs Unsupervised Learning
In supervised learning, you train a model on labeled data (e.g., "this email is spam"). In unsupervised learning, you have no labels: the algorithm discovers hidden structure on its own. Clustering is the most common unsupervised technique.
| Supervised | Unsupervised | |
|---|---|---|
| Labels Needed? | Yes | No |
| Goal | Predict a known outcome | Discover hidden groups |
| Example | Spam vs Not Spam | Customer Segments |
| Typical Method | Regression, Classification | K-Means, Hierarchical |
The K-Means Algorithm
K-Means is the most widely used clustering algorithm. Given a number \( k \), it partitions \( n \) data points into \( k \) clusters by minimizing the total within-cluster distance.
Euclidean Distance
K-Means measures similarity using Euclidean distance: the straight-line distance between two points in space. For two points \( A = (x_1, y_1) \) and \( B = (x_2, y_2) \):
K-Means: Step by Step
How to Apply K-Means
Centroid Recalculation
After assigning each point to a cluster, the centroid is updated to the mean position of all points in that cluster:
Choosing \( k \): The Elbow Method
How do you pick the right number of clusters? The Elbow Method plots the total within-cluster sum of squares (WCSS) for different values of \( k \). Look for the "elbow" where adding more clusters stops significantly reducing WCSS.
Clustering vs Classification
These are often confused. Classification assigns data to known categories. Clustering discovers unknown groups. They solve fundamentally different problems.
Example: Customer Spending
A store has 6 customers with Annual Income and Spending Score. Let's cluster them into \( k = 2 \) groups.
Example: Computing Assignments
Suppose centroids are at \( \mu_1 = (2, 3) \) and \( \mu_2 = (8, 7) \). A new point is at \( P = (5, 6) \). Which cluster does \( P \) belong to?
Example: Centroid Update
After assignment, Cluster 2 now contains points \( (8, 7) \), \( (5, 6) \), and \( (9, 8) \). Compute the new centroid:
The centroid shifted from \( (8, 7) \) to \( (7.33, 7.0) \). This process repeats until the centroids stabilize. Each iteration, the total WCSS decreases or stays the same: it never increases.
Common Mistakes
Clustering Methods Compared
| K-Means | Hierarchical | DBSCAN | |
|---|---|---|---|
| Must choose k? | Yes | No (dendrogram) | No |
| Cluster shape | Spherical | Any shape | Any shape |
| Speed | Fast | Slow | Medium |
| Handles outliers | Poorly | Moderately | Well |
| Best for | Large, clean data | Small datasets | Irregular shapes |
Question 1 of 5
Clustering is a type of ________ learning because it does not require labeled data.
Question 2 of 5
In K-Means, the centroid of a cluster is updated by computing the:
Question 3 of 5
What is the Euclidean distance between points \( (1, 2) \) and \( (4, 6) \)?
Question 4 of 5
The Elbow Method helps you determine:
Question 5 of 5
A company has customers with income measured in dollars (0-100,000) and age measured in years (18-80). Before running K-Means, they should: