Professional Documents
Culture Documents
2 - K-Mean
2 - K-Mean
Unsupervised learning
introduction
Andrew Ng
Supervised learning
Training set:
Andrew Ng
Unsupervised learning
Training set:
Andrew Ng
Applications of clustering
(drop convention)
Andrew Ng
K-means algorithm
}
Andrew Ng
K-means for non-separated clusters
T-shirt sizing
Weight
Height
Andrew Ng
Clustering
Optimization
objective
Machine Learning
K-means optimization objective
= index of cluster (1,2,…, ) to which example is currently
assigned
= cluster centroid ( )
= cluster centroid of cluster to which example has been
assigned
Optimization objective:
Andrew Ng
K-means algorithm
Repeat {
for = 1 to
:= index (from 1 to ) of cluster centroid
closest to
for = 1 to
:= average (mean) of points assigned to cluster
}
Andrew Ng
Clustering
Random
initialization
Machine Learning
K-means algorithm
Repeat {
for = 1 to
:= index (from 1 to ) of cluster centroid
closest to
for = 1 to
:= average (mean) of points assigned to cluster
}
Andrew Ng
Random initialization
Should have
Andrew Ng
Local optima
Andrew Ng
Random initialization
For i = 1 to 100 {
Andrew Ng
Clustering
Choosing the
number of clusters
Machine Learning
What is the right value of K?
Andrew Ng
Choosing the value of K
Elbow method:
Cost function
Cost function
1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8
Andrew Ng
Choosing the value of K
Sometimes, you’re running K-means to get clusters to use for some
later/downstream purpose. Evaluate K-means based on a metric for
how well it performs for that later purpose.
Weight
Weight
Height Height
Andrew Ng
Id Height weight
𝑦1 185 72
𝑦2 170 56 Divide the given sample data in two (2) clusters
𝑦3 168 60
using k-mean algorithm [Euclidean distance].
𝑦4 179 68
𝑦5 183 84 Solution:
K=2
𝑦6 180 67
initialize centroid 𝜇1 = 185,72 , 𝜇2 = (170, 56)
𝑦7 177 76
𝑦8 166 55
Andrew Ng
𝜇1 𝜇2
1 (𝟏𝟖𝟓 − 𝟏𝟖𝟓)2 +(𝟕𝟐 − 𝟕𝟐)2 = 0 (𝟏𝟖𝟓 − 𝟏𝟕𝟎)2 +(𝟕𝟐 − 𝟓𝟔)2 = 21.9
2 (𝟏𝟕𝟎 − 𝟏𝟖𝟓)2 +(𝟓𝟔 − 𝟕𝟐)2 = 21.9 (𝟏𝟕𝟎 − 𝟏𝟕𝟎)2 +(𝟓𝟔 − 𝟓𝟔)2 = 0
3 (𝟏𝟔𝟖 − 𝟏𝟖𝟓)2 +(𝟔𝟎 − 𝟕𝟐)2 = 20.8 (𝟏𝟔𝟖 − 𝟏𝟕𝟎)2 +(𝟔𝟎 − 𝟓𝟔)2 = 4.47
4 (𝟏𝟕𝟗 − 𝟏𝟖𝟓)2 +(𝟔𝟖 − 𝟕𝟐)2 = 7.2 (𝟏𝟕𝟗 − 𝟏𝟕𝟎)2 +(𝟔𝟖 − 𝟓𝟔)2 = 15
5 (𝟏𝟖𝟑 − 𝟏𝟖𝟓)2 +(𝟖𝟒 − 𝟕𝟐)2 = 16.4 (𝟏𝟖𝟑 − 𝟏𝟕𝟎)2 +(𝟖𝟒 − 𝟓𝟔)2 = 30.87
6 (𝟏𝟖𝟎 − 𝟏𝟖𝟓)2 +(𝟔𝟕 − 𝟕𝟐)2 = 7.07 (𝟏𝟖𝟎 − 𝟏𝟕𝟎)2 +(𝟔𝟕 − 𝟓𝟔)2 = 14.8
7 (𝟏𝟕𝟕 − 𝟏𝟖𝟓)2 +(𝟕𝟔 − 𝟕𝟐)2 = 8.9 (𝟏𝟕𝟕 − 𝟏𝟕𝟎)2 +(𝟕𝟔 − 𝟓𝟔)2 = 21.18
8 (𝟏𝟔𝟔 − 𝟏𝟖𝟓)2 +(𝟓𝟓 − 𝟕𝟐)2 = 25.4 (𝟏𝟔𝟔 − 𝟏𝟕𝟎)2 +(𝟓𝟓 − 𝟓𝟔)2 = 4.12
185+179+183+180+177 72+68+84+67+76
𝜇1 = ( , )= 180.8,73.4
5 5
Andrew Ng
New centroid= 𝜇1 = 180.8,73.4 , 𝜇2 = (168, 57)
𝜇1 𝜇2
1 4.427188724 22.6715681
2 20.4792578 2.236067977
3 18.53105502 3
4 5.692099788 15.55634919
5 10.82589488 30.88689042
6 6.449806199 15.62049935
7 4.604345773 21.02379604
8 23.61355543 2.828427125