Today’s Roadmap
Introduction to Clustering
K-Means Clustering
Minimizing Inertia
Agglomerative Clustering
Picking K
1
Based on Data 100, Berkeley
Review: Taxonomy of Machine Learning
2
Labeled Data
Supervised Learning
Regression
Classification
Categorical Response
Quantitative Response
Unlabeled Data
Unsupervised Learning
Clustering
Dimensionality Reduction
Data 8: Nearest Neighbors
Earlier: Logistic Regression
Tuesday: Decision Trees
Today: KMeans Clustering
Agglomerative Clustering
Review: Taxonomy of Machine Learning
In “Supervised Learning”:
3
Labeled Data
Supervised Learning
Regression
Classification
Categorical Response
Quantitative Response
Review: Taxonomy of Machine Learning
In “Unsupervised Learning”:
Problem: Dimensionality Reduction.
Problem: Clustering.
4
Unlabeled Data
Unsupervised Learning
Clustering
Dimensionality Reduction
Clustering Example
Consider the figure shown from Fall 2019 Midterm 2.
Goal of clustering: Assign each point to a cluster.
This is an unsupervised task.
5
Clustering Example
Consider the figure shown from Fall 2019 Midterm 2.
Goal of clustering: Assign each point to a cluster.
This is an unsupervised task.
6
Clustering Example 1: Netflix
Suppose you’re Netflix and have information on customer viewing habits.
Clustering is different from classification.
7
Clustering Example 1: Netflix
8
Note: I’m not certain that Netflix actually uses ML clustering to identify these categories, but they could in principle.
Clustering Example 2: Clustering Students
In 2018, as a tiny part of a project working to understand factors that affect 61B student success, we tried clustering students based on:
Clustering algorithm automatically identified procrastinating students.
This result wasn’t particularly useful, but it was somewhat interesting.
9
Clustering Example 3: Reverse Engineering Biology
In plot to the right:
Green indicates that the gene was ~off.
Clustering brings similar observations together.
10
Note: There is also red in this image indicating genes which were especially “on”. Sorry if you can’t differentiate red from green by eye! Blame the 1990s bioinformatics community.
Before and After Clustering
11
From: Cluster analysis and display of genome-wide expression patterns by Michael Eisen, et. al
(Recolored by me)
12
K-Means Clustering
Lecture 24, Data 100 Spring 2022
Introduction to Clustering
K-Means Clustering
Minimizing Inertia
Agglomerative Clustering
Picking K
13
K-Means Clustering
Most popular clustering approach. The algorithm:
14
Raw data →
K-Means Clustering
Most popular clustering approach. The algorithm:
15
Initial random placement of two centers
Iteration: 0
K-Means Clustering
Most popular clustering approach. The algorithm:
16
Data colored by closest center
Iteration: 0
K-Means Clustering
Most popular clustering approach. The algorithm:
17
Where should the centers go next?
K-Means Clustering
Most popular clustering approach. The algorithm:
18
Centers moved to their new homes
Iteration: 1
K-Means Clustering
Most popular clustering approach. The algorithm:
19
Data colored by closest center (in new position)
Iteration: 1
K-Means Clustering
Most popular clustering approach. The algorithm:
20
Centers moved to new position
Iteration: 2
K-Means Clustering
Most popular clustering approach. The algorithm:
21
Data colored by closest center (in new position)
Iteration: 3
K-Means Clustering
Most popular clustering approach. The algorithm:
22
Centers moved to new position
Iteration: 4
K-Means Clustering
Most popular clustering approach. The algorithm:
23
Data colored by closest center (in new position)
Iteration: 4
K-Means Clustering
Most popular clustering approach. The algorithm:
24
Centers moved to new position
Iteration: 5
K-Means Clustering
Most popular clustering approach. The algorithm:
25
Data colored by closest center (in new position)
Iteration: 5
K-Means Clustering
Above we see the results after iteration 4 and 5:
26
Iteration: 5
Iteration: 4
K-Means Clustering
Above we see the results after iteration 4 and 5:
27
Iteration: 5
Iteration: 4
K-Means vs. K-Nearest Neighbors
Quick note: K-Means is a totally different algorithm than “K-Nearest Neighbors”.
The names may be similar, but there isn’t really anything in common.
28
Minimizing Inertia
Introduction to Clustering
K-Means Clustering
Minimizing Inertia
Agglomerative Clustering
Picking K
29
K-Means Clustering for K = 4
Below is an example of an output for K=4:
30
K-Means Clustering for K = 4
Each time you run K-Means, you get a different output, depending on where centers started.
� random.seed(25) random.seed(29) random.seed(40)
Which is best?
31
K-Means Clustering for K = 4
Each time you run K-Means, you get a different output, depending on where centers started.
� random.seed(25) random.seed(29) random.seed(40)
Which is best?
Goal: Come up with a loss function for clustering.
32
K-Means Clustering for K = 4 (Your Ideas for a Clustering Loss Function)
Each time you run K-Means, you get a different output. Define a loss to decide which is best.
� random.seed(25) random.seed(29) random.seed(40)
Come up with a loss function for clustering -- your ideas:
33
K-Means Clustering for K = 4
To evaluate different clustering results, we need a loss function
Two common loss functions:
34
Example:
0.47
0.25
0.36
0.44
0.19
0.34
0.58
K-Means Clustering for K = 4
Each time you run K-Means, you get a different output:
random.seed(25) random.seed(29) random.seed(40)
Among these three choices, our inertia loss function says that the leftmost clustering is best (inertia: 44.96) and rightmost clustering (inertia: 54.35) is worst.
35
Inertia: 44.96
Inertia: 45.95
Inertia: 54.35
K-Means and Inertia
It turns out that the function K-Means is trying to minimize is inertia…
… but often fails to find global optimum. Why? Sketch below
Can think of K-means as a pair of optimizers that take turns:
36
Optimizing Inertia
Hard problem: Give an algorithm that optimizes inertia FOR A GIVEN K. K is picked in advance.
This is a bit of a CS61B/CS70/CS170 problem. It may be far too hard for some of you
37
Optimizing Inertia
Hard problem: Give an algorithm that optimizes inertia FOR A GIVEN K. K is picked in advance.
Algorithm:
No better algorithm has been found for solving the problem of minimizing exactly.
38
A “coloring” is just a choice of color for every point, e.g. point 1 = red, point 2 = green, point 3 = orange, point 4 = blue
For those who know what this means: � K-Means is known to be an NP-hard problem
Agglomerative Clustering
Introduction to Clustering
K-Means Clustering
Minimizing Inertia
Agglomerative Clustering
Picking K
39
K-Means
Which clustering result do you like better?
40
K-Means
Which clustering result do you like better?
K-Means likes the one on the right better. It has lower inertia.
41
Inertia: 94.41
Inertia: 87.28
K-Means
Which clustering result do you like better?
K-Means likes the one on the right better because it has lower inertia: sum of squared distances from each data point to its center.
42
Inertia: 94.41
Inertia: 87.28
Agglomerative Clustering
As with regression and classification, there are many ways to do clustering.
So far we’ve seen K-Means, which attempts to minimize inertia.
Let’s discuss an alternate idea known as agglomerative clustering.
Basic idea:
Let’s see an example for K = 2.
43
Agglomerative Clustering Example
When the algorithm starts, every data point is in its own cluster.
44
Agglomerative Clustering Example
When the algorithm starts, every data point is in its own cluster.
45
Two points in the same cluster
Agglomerative Clustering Example
Next two closest are 0 and 4, so merge them.
46
Two points in the same cluster
Agglomerative Clustering Example
Next two closest are 0 and 4, so merge them
47
Merged
Agglomerative Clustering Example
At this point we have 10 clusters:
48
Agglomerative Clustering Example
Tricky question:
49
Agglomerative Clustering Example
Tricky question:
50
Would be perfectly reasonable to do something else, e.g. average or minimum.
Agglomerative Clustering Example
Next two closest clusters are 1 and 5.
51
Agglomerative Clustering Example
Next two closest clusters are 1 and 5.
52
Merged
Agglomerative Clustering Example
Next two closest clusters are 7 and 9.
53
Agglomerative Clustering Example
Next two closest clusters are 7 and 9.
54
Merged
1
1
Agglomerative Clustering Example
Next closest are 6 and 8.
55
Agglomerative Clustering Example
Next closest are 6 and 8.
56
Merged
Agglomerative Clustering Example
Now 0 and 3 are closest. Merge them next.
57
Merged
Agglomerative Clustering Example
Now 0 and 3 are closest. Merge them next.
58
Merged
Agglomerative Clustering Example
Next up are 1 and 10.
59
Merged
Agglomerative Clustering Example
Next up are 1 and 10.
60
Merged
Agglomerative Clustering Example
Next up are 0 and 7. Why?
61
Blue line is shorter than gold line.
Note: Doesn’t look visually shorter due to different scales for x and y axes.
Agglomerative Clustering Example
Next up are 0 and 7. Why?
62
Merged
Agglomerative Clustering Example
Next up are 0 and 6.
63
Agglomerative Clustering Example
Next up are 0 and 6.
64
Merged
Agglomerative Clustering Example
Next up are 0 and 2.
65
Agglomerative Clustering Example
Next up are 0 and 2.
66
Merged
Agglomerative Clustering Example
On the full dataset, our agglomerative clustering algorithm gets the “correct” output.
67
Clustering and Dendrograms
Agglomerative clustering is one form of “hierarchical clustering.”
68
Clustering Algorithms: Many More We Haven’t Seen
69
K Means
Agglomerative
Quick Sidetrack: Grading
Some professors (not me) use agglomerative clustering for grading bins.
70
Inertia: 87.28
Inertia: 94.41
Picking K
Lecture 24, Data 100 Spring 2022
Introduction to Clustering
K-Means Clustering
Minimizing Inertia
Agglomerative Clustering
Picking K
71
Picking K
The algorithms we’ve discussed today require us to pick a K before we start.
Often, best K is subjective:
72
Picking K: Elbow Method
For K-Means, one approach is to plot inertia versus many different K values.
73
Silhouette Scores
To evaluate how “well clustered” a specific data point is, we can use the “silhouette score”, a.k.a. the “silhouette width”.
74
Low score point
High score point
For a data point X, score S is:
Silhouette Scores
For a data point X:
75
What is the highest possible S?
Low score point
High score point
Silhouette Scores
For a data point X:
76
What is the highest possible S?
Highest possible S is 1.
Low score point
High score point
Silhouette Scores
For a data point X:
77
Can S be negative?
Low score point
High score point
Silhouette Scores
For a data point X:
78
Can S be negative?
Low score point
High score point
Silhouette Scores
For a data point X:
79
Can S be negative?
Low score point
High score point
Silhouette Plot
We can plot the Silhouette Scores for all of our data points.
80
Low score point
High score point
Low score point
High score point
Blue Cluster
Orange Cluster
Silhouette Scores, K = 2 vs. K = 3
81
Average silhouette score is lower.
Blue
Orange
Green
Blue Cluster
Orange Cluster
Picking K: Real World Metrics (Example via Andrew Ng)
Sometimes you can rely on real world metrics to guide your choice of K.
Perform 2 clusterings:
To pick K:
82
Summary
Today we discussed a new machine learning goal: Clustering
Saw two solutions:
Our version of these algorithms required a hyperparameter k.
83
Bigger Picture Summary
There are many Machine Learning problems.
�
Many solution technique can be used for multiple problem types.
�
We’ve only scratched the surface. Haven’t discussed many important ideas.
84
Taxonomy of Machine Learning
85
Labeled Data
Supervised Learning
Regression
Classification
Categorical Response
Quantitative Response
Unlabeled Data
Unsupervised Learning
Clustering
Dimensionality Reduction
Reward
Reinforcement Learning