Lecture 13: Clustering
An introduction to unsupervised learning and clustering, covering the K-means algorithm, optimization objective, random initialization, and techniques for selecting the optimal number of clusters.
Unsupervised Learning - Introduction
- Talk about clustering (learning from unlabeled data)
- Unsupervised learning
- Useful to contrast with supervised learning
- Compare and contrast:
- Supervised learning:
- Given a set of labels, fit a hypothesis to it
- Unsupervised learning:
- Try and determine structure in the data
- Clustering algorithm groups data together based on data features
- Supervised learning:
- What is clustering good for?
- Market segmentation: Group customers into different market segments
- Social network analysis: Facebook "smartlists"
- Organizing computer clusters: Data centers layout and location optimization
- Astronomical data analysis: Understanding galaxy formation
K-Means Algorithm
- Want an algorithm to automatically group the data into coherent clusters
- K-means is by far the most widely used clustering algorithm
Overview
- Take unlabeled data and group into two clusters:

-
Algorithm overview:
-
Randomly allocate two points as the cluster centroids:
- Have as many cluster centroids as clusters you want to find ( cluster centroids)
- In our example, we just have two clusters ()
-
Cluster assignment step:
- Go through each example and, depending on whether it is closer to the red or blue centroid, assign each point to one of the two clusters
- To demonstrate this, we color each point red or blue based on proximity:

-
Move centroid step:
- Take each centroid and move it to the average (mean) of the correspondingly assigned data points:

- Repeat steps (2) and (3) until convergence.
-
-
More formal definition:
-
Input:
- (number of clusters in the data)
- Training set
- Note: (by convention in clustering, drop the intercept term)
-
Algorithm:

- Randomly initialize cluster centroids
- Repeat:
-
for i = 1 to m- index (from to ) of cluster centroid closest to
- i.e., take the -th example, measure squared distance to each cluster centroid, assign to closest:

-
for k = 1 to K- average (mean) of points assigned to cluster
-
-
K-Means for Non-Separated Clusters
- Often K-means is applied to data that does not have well-separated, distinct clusters
- e.g., T-shirt sizing:

- Take height and weight features of a population
- Clusters may not look distinctly separated, but K-means can still group them into Small (S), Medium (M), and Large (L) sizes to guide product design.
K-Means Optimization Objective
-
Most optimization algorithms in supervised learning minimize a cost function . K-means also has an optimization objective function.
-
Why is this useful?
- Helps debug K-means (verify that decreases monotonically with each iteration)
- Helps K-means avoid local optima and find better clusters
-
Notation:

-
index of cluster to which example is currently assigned
-
cluster centroid ()
-
cluster centroid of cluster to which example has been assigned
-
Using this notation, we can write the optimization objective (i.e. squared distance between and ):

- This is just what we have been doing visually:

-
The red line shows the distance between the example and the cluster centroid to which that example has been assigned.
-
When the example is very close to the cluster centroid, this distance value is small.
-
When the cluster centroid is far away from the example, the distance value is large.
-
This function is sometimes called the distortion (or distortion cost function).
-
So we are finding the parameter values that minimize this cost function:

- If we consider how the K-means algorithm operates:
- The cluster assignment step minimizes with respect to (holding fixed).
- i.e., find the centroid closest to each example
- Doesn't change the centroids themselves
- The move centroid step minimizes with respect to (holding fixed).
- So, we partition the optimization process into two alternating steps:
- First step minimizes with respect to the cluster assignment variables
- Second step minimizes with respect to the centroid location variables
- The cluster assignment step minimizes with respect to (holding fixed).
- We can use this knowledge to help debug our K-means implementation (verify that cost decreases monotonically).
Random Initialization
-
How to initialize K-means centroids and how to avoid local optima.
-
Initialization method:
- Have the number of centroids set to less than the number of training examples (). (If , we cannot run K-means properly).
- Randomly pick training examples.
- Set equal to these randomly chosen training examples.
-
K-means can converge to different solutions depending on the initial random setup:
- Risk of local optima:

- Local optima are valid convergence points, but they are local minima rather than global minima.
-
Mitigating local optima with multiple random initializations:
- We can perform multiple random initializations.
- See if we arrive at the same result repeatedly — getting the same result many times strongly indicates a global optimum.
-
Algorithm:

-
A typical number of times to initialize K-means is between 50 and 1000.
-
For i = 1 to 100(or 50-1000):- Randomly initialize K-means.
- Run K-means to convergence to obtain .
- Compute the distortion cost .
-
Pick the clustering configuration that yielded the lowest distortion cost .
-
Guideline for :
- If running K-means with clusters, multiple random initializations significantly help find a better global optimum.
- If , multiple random initializations are less likely to be necessary (the first random initialization usually gives a good enough clustering due to finer granularity).
How Do We Choose the Number of Clusters?
- Choosing :
- There is no automated, universally standard way to choose .
- Normally, visualizations or manual domain analysis are used to determine .
- Why is this hard?
- Data structure is often ambiguous (e.g. could be viewed as 2 clusters or 4 clusters — neither is inherently wrong).
- There is not necessarily one single correct answer.
Elbow Method
- Vary and compute the cost function across a range of values.
- As increases, the minimum value of should decrease (since higher granularity allows centroids to better fit data points).
- Plot vs. .
- Look for the "elbow" point on the graph:

- Choose the number of clusters corresponding to the "elbow".
- If you get a clear plot, this is a reasonable method for selecting .
- Limitations / Risks:
- In practice, plots often show a smooth curve without a clear, distinct elbow.
- Not always helpful or decisive.
Another Method for Choosing K
-
Using K-means for a downstream application (e.g., market segmentation):
- Evaluate how well different choices of serve downstream business or algorithmic needs.
-
e.g., T-shirt Sizing Example:
- Decide whether to produce 3 sizes (S, M, L) vs. 5 sizes (XS, S, M, L, XL).
- Run K-means for and :

- Choose based on trade-offs:
- Compare the cost of manufacturing/stocking extra sizes against customer fit and satisfaction.
- The downstream application requirements guide the selection of .
Lecture 12: Support Vector Machines
A comprehensive guide to Support Vector Machines (SVMs), including optimization objectives, large margin classification intuition and mathematics, kernel methods, and practical implementation advice.
Lecture 14: Dimensionality Reduction
A comprehensive guide to dimensionality reduction techniques, focusing on Principal Component Analysis (PCA), data compression, visualization, mathematical formulation, algorithm implementation, reconstruction, and practical guidance.