01 / 21

Clustering &
Segmentation

Learn how the K-Means algorithm groups data points into meaningful segments: the foundation of customer analytics and market research.

Business Analytics

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

Customer Segmentation
Group customers by purchasing behavior to create targeted marketing campaigns.
Product Grouping
Organize inventory into categories based on sales patterns and features.
Fraud Detection
Identify outlier transactions that don't fit normal spending clusters.

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) \):

Spending Score Income
A \( (2, 3) \)
B \( (7, 8) \)
d ≈ 7.07 5 5

K-Means: Step by Step

Step 1 Choose k centroids Step 2 Assign each point Step 3 Recalculate centroids Repeat until centroids stop moving Iteration 1 ... Converged Cluster 1 Cluster 2

How to Apply K-Means

1
Choose \( k \): decide how many clusters you want (e.g., 3 customer segments).
2
Place centroids: randomly pick \( k \) starting positions in the feature space.
3
Assign points: for each data point, compute the distance to every centroid and assign it to the nearest one.
4
Recompute centroids: set each centroid to the mean of all points assigned to its cluster.
5
Repeat steps 3-4 until centroids stop moving (convergence).

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.

Number of Clusters (k) WCSS 1 2 3 4 5 6 Elbow
\( k = 3 \)

Clustering vs Classification

These are often confused. Classification assigns data to known categories. Clustering discovers unknown groups. They solve fundamentally different problems.

Clustering
No labels needed. Finds hidden patterns. Groups emerge from data similarity.
Classification
Requires labeled training data. Predicts a category for new observations.

Example: Customer Spending

A store has 6 customers with Annual Income and Spending Score. Let's cluster them into \( k = 2 \) groups.

Annual Income ($K) Spending Score Centroid 1 Centroid 2
\( (10, 20) \)
\( (15, 25) \)
\( (20, 30) \)
\( (65, 80) \)
\( (70, 75) \)
\( (80, 85) \)

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

✕ Wrong \( k \)
Picking too many or too few clusters without using the Elbow Method leads to meaningless segments.
✕ Unscaled Features
If income is in thousands and age in years, income dominates the distance. Always normalize features first.
✕ Assuming Causation
Clusters show correlation, not causation. Being in a "high-spend" cluster doesn't mean income causes spending.

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: