Download presentation
Presentation is loading. Please wait.
1
Clustering Chapter 7
2
Clustering Problem Definition
Partitioning a set of objects into subsets according to some desired criterion.
3
Motivation CS Students? Given a collection of friendship relations among people, we want to identify any tight-knit groups (subsets) that exist. Friendship relation can be whether they are friends, or whether they studied in the same school etc… Math Students?
4
Center-based clusters
Clustering can be defined by 𝑘 “center points” 𝑐 1 , , 𝑐 𝑘 , with each data point assigned to whichever center point is closest to it (by chosen metric). We need an objective (cost) function to define what is a good center. Three standard objectives often used are k- center, k-median, and k-means clustering.
5
Metric Space A set X and a function d:X×X→ℝ s.t: d x,y ≥0 d x,y =0⇔x=y
d x,y =d y,x d x,y ≤d x,z +d z,y Examples L 2 :d x,y = x 1 − y x 2 − y 2 2 L 1 :d x,y = x 1 − y 1 +| x 2 − y 2 | L ∞ :d x,y =max x 1 − y 1 ,| x 2 − y 2 | Before we can give a specific definition of clustering problems we need to define what is a relationship between two points/objects in the data set.
6
k-center Clustering
7
k-center clustering Find a partition 𝐶={ 𝐶 1 , , 𝐶 𝑘 } of our data into 𝑘 clusters, with corresponding centers { 𝑐 1 , , 𝑐 𝑘 }, to minimize the maximum distance between any data point and the center of its cluster: 𝜙 𝑘𝑐𝑒𝑛𝑡𝑒𝑟 𝐶 = max 𝑗=1 𝑘 max 𝑎 𝑖 ∈ 𝐶 𝑗 | 𝑎 𝑖 − 𝑐 𝑗 | Many times called the “firehouse location problem”. problem of locating k fire-stations in a city so as to minimize the maximum distance a fire-truck might need to travel to put out a fire.
8
k-center clustering Suppose 𝑘 = 2:
9
k-center clustering Suppose 𝑘 = 2:
10
Any ideas for such an algorithm?
11
Farthest-first traversal algorithm
Pick any data point to be the first cluster center. At time 𝑡, for 𝑡=2,3,…,𝑘, pick the farthest data point from any existing cluster center; make it the 𝑡 𝑡ℎ cluster center.
12
Example Step t=0
13
Example Step t=1
14
Example Step t=2
15
Example Step t=3
16
Example Step t=4 Which point and cluster have the maximum distance?
17
Example Step t=4 What point and cluster have the maximum distance? 𝒓
18
What can we say about this?
Theorem 7.3 If there is a k-clustering of radius 𝑟 2 , then the above algorithm finds a k-clustering with radius at most 𝑟. 𝒓
19
Proof Distance of 𝑝 from all other centers ≥𝑟 𝒑 𝒓 ≥𝒓
20
Proof Distance between centers ≥𝒓, since otherwise we could have picked 𝑝 before one of the existing centers. In this example the red center. 𝒑 𝒓 ≥𝒓 <𝒓
21
Proof Distance between centers ≥𝒓, since otherwise we could have picked 𝑝 before one of the existing centers. 𝒑 ≥𝒓
22
Proof Distance between centers and p ≥𝒓. 𝒑 ≥𝒓
23
Proof We have 𝑘+1 points (𝑘 centers + point 𝑝), each pair is of distance ≥𝑟. In 𝑂𝑃𝑇 solution at least 2 of these points are assigned to the same center (in the optimal solution). This center must be of distance ≥ 𝑟 2 from at least one of them. ≥𝒓 𝑪 𝑶𝑷𝑻
24
K-means Clustering
25
k-means clustering 𝜙 𝑘𝑚𝑒𝑎𝑛𝑠 𝑋 = 𝑥∈𝑋 min 𝑐∈𝐶 𝑥−𝑐 2
Given integer 𝑘 and 𝑛 data points 𝑋⊂ 𝑅 𝑑 . We wish to choose 𝑘 centers 𝐶 so as to minimize the objective function 𝜙 𝑘𝑚𝑒𝑎𝑛𝑠 𝑋 = 𝑥∈𝑋 min 𝑐∈𝐶 𝑥−𝑐 2 The k-means criterion is more often used when data consists of points in Rd, whereas k-median is more commonly used when we have a finite metric, that is, data are nodes in a graph with distances on edges.
26
Motivation for using k-means (1)
Suppose that the data originates from an equal weight mixture of 𝑘 spherical well-separated Gaussian densities centered at 𝜇 1 , 𝜇 2 , , 𝜇 𝑘 , each with variance 1 in every direction. The density of the mixture is 𝑃 𝑥 = 1 2𝜋 𝑑 1 𝑘 𝑖=1 𝑘 𝑒 − 𝑥− 𝑢 𝑖 2 Denote by 𝜇(𝑥) the center nearest to 𝑥. Since the exponential function falls off fast we can approximate 𝑖=1 𝑘 𝑒 − 𝑥− 𝑢 𝑖 2 by 𝑒 − 𝑥−𝜇(𝑥) 2 . Thus 𝑃 𝑥 ≈ 1 2𝜋 𝑑 𝑘 𝑒 − 𝑥−𝜇(𝑥) 2 We can approximate because every u_i that is far from x is very small relatively to u(x).
27
Motivation for using k-means (2)
The likelihood of drawing the sample points 𝑥 1 , 𝑥 2 ,…, 𝑥 𝑛 from the mixture, if the centers were 𝜇 𝑥 1 ,𝜇 𝑥 2 ,…,𝜇( 𝑥 𝑛 ) is approximately 𝑃 𝑥 1 𝑃 𝑥 2 …𝑃 𝑥 𝑛 ≈ 1 𝑘 𝑛 1 2𝜋 𝑑 𝑖=1 𝑛 𝑒 − 𝑥 𝑖 −𝜇 𝑥 𝑖 2 =𝑐 𝑒 − 𝑖=1 𝑛 𝒙 𝒊 −𝝁 𝒙 𝒊 𝟐 Minimizing the sum of squared distances to cluster centers finds the maximum likelihood 𝜇 1 , 𝜇 2 , , 𝜇 𝑘 .
28
The k-means objective (1)
Suppose we have already determined the clustering into 𝐶 1 , , 𝐶 𝑘 . What are the best centers for the clusters? Lemma 7.1 Let 𝑎 1 , 𝑎 2 ,…, 𝑎 𝑛 be a set of points, if we set 𝑐= 1 𝑛 𝑖=1 𝑛 𝑎 𝑖 (centroid) then, 𝑖 𝑎 𝑖 −𝑥 2 = 𝑖 𝑎 𝑖 −𝑐 2 𝐴 𝑐𝑜𝑛𝑠𝑡𝑎𝑛𝑡 + 𝑛 𝑐−𝑥 2 𝑥=𝑐→𝑡𝑒𝑟𝑚=0 &𝑎𝑛𝑑 𝑡𝑒𝑟𝑚≥0 + 𝒄 𝒙 𝒂 𝒊 Intuition: Pitagoras.
29
The k-means objective (2)
Proof. 𝑖 𝑎 𝑖 −𝑥 2 = 𝑖 (𝑎 𝑖 −𝑐)+(𝑐−𝑥) 2 = 𝑖 ( 𝑎 𝑖 −𝑐 2 )+2 𝑐−𝑥 ⋅ 𝑖 𝑎 𝑖 −𝑐 =0 𝑠𝑖𝑛𝑐𝑒 𝑐 𝑖𝑠 𝑐𝑒𝑛𝑡𝑟𝑜𝑖𝑑 +𝑛 𝑐−𝑥 2 = 𝑖 𝑎 𝑖 −𝑐 2 +𝑛 𝑐−𝑥 2 Proof
30
Lloyd’s Algorithm 1. Start with 𝑘 centers.
2a. Associate each point with the center nearest to it. 2b. Find the centroid of each cluster and replace the set of old centers with the centroids. Repeat Step 2 until the centers converge (according to some criterion, such as the k- means score no longer improving). By Lemma 7.1, converges to a local minimum of the objective, since we are always minimizing the sum of internal cluster squared distances. (How to pick the centers?) Show example of 2 clusters on board. Converges to a local minimum.
31
Lloyd’s Algorithm Show example of 2 clusters on board. Converges to a local minimum.
32
Lloyd’s Algorithm Show example of 2 clusters on board. Converges to a local minimum.
33
Lloyd’s Algorithm Show example of 2 clusters on board. Converges to a local minimum.
34
Lloyd’s Algorithm Show example of 2 clusters on board. Converges to a local minimum.
35
Lloyd’s Algorithm Show example of 2 clusters on board. Converges to a local minimum.
36
Lloyd’s Algorithm Show example of 2 clusters on board. Converges to a local minimum.
37
Lloyd’s Algorithm Show example of 2 clusters on board. Converges to a local minimum.
38
Lloyd’s Algorithm – Local and not Global
For the following initialization + + Show example of 2 clusters on board. Initialize with (0,1) and 2 near (3,0). +
39
Lloyd’s Algorithm – Local and not Global
We receive a local optimum too far from the global one. How can we improve the algorithm? + + We can farthest traversal initialization +
40
Lloyd’s Algorithm – Local and not Global
We receive a local optimum too far from the global one. How can we improve the algorithm? + We can farthest traversal initialization
41
Lloyd’s Algorithm – Local and not Global
The global solution. How can we improve the algorithm? + + We can farthest traversal initialization +
42
Farthest-first traversal robustness
Can be easily fooled by outliers. For 𝑘=2:
43
Farthest-first traversal robustness
Can be easily fooled by outliers. For 𝑘=2:
44
Farthest-first traversal robustness
Can be easily fooled by outliers. For 𝑘=2:
45
K-means++ Initialization Method
(Original paper:
46
K-means++ Overview Choose centers at random from the data points, but weight the data points (the probability of choosing them) according to their squared distance from the closest center already chosen. Show example of 2 clusters on board. Converges to a local minimum.
47
Definitions Given clustering 𝐶 with objective 𝜙 and points 𝑋, we let 𝜙 𝐴 = 𝑥∈𝐴 min 𝑐∈𝐶 𝑥−𝑐 denote the contribution of 𝐴⊂𝑋 to the potential. let 𝐷 𝑥 = min 𝑐∈𝐶 𝑥−𝑐 denote the shortest distance from a data point 𝑥 to the closest center we have already chosen. Show example of 2 clusters on board. Converges to a local minimum.
48
The k-means++ algorithm
1a. Take one center 𝑐 1 , chosen uniformly at random from 𝑋. 1b. Take a new center 𝑐, choosing a∈𝑋 with probability 𝐷 𝑎 2 𝑥∈𝑋 𝐷 𝑥 1c. Repeat Step 1b. until we have taken 𝑘 centers altogether. Then: Proceed as with the chosen k-means algorithm (Lloyd). We call the weighting used in Step 1b simply “ 𝐷 2 weighting”. The algorithm takes 𝑂(𝑛⋅𝑘). Show example of 2 clusters on board. Converges to a local minimum.
49
Test Run Step k=0 Sample data set for clustering. Size of the points represent the relative chance to choose them. Choosing first center uniformly at random yields same size for all points. Show example of 2 clusters on board. Converges to a local minimum.
50
Test Run Step k=1 Second center will most likely be chosen far from the first one. Show example of 2 clusters on board. Converges to a local minimum.
51
Test Run Step k=2 The next one will most likely be chosen far away from the 2 already chosen centers. Show example of 2 clusters on board. Converges to a local minimum.
52
Test Run Step k=3 Show example of 2 clusters on board. Converges to a local minimum.
53
Test Run Step k=4 Show example of 2 clusters on board. Converges to a local minimum.
54
Test Run Step k=5 Show example of 2 clusters on board. Converges to a local minimum.
55
Test Run Step k=6
56
Test Run Step k=7
57
K-means++ is 𝑶 𝒍𝒐𝒈 𝒌 -Competitive
Theorem 3.1 If 𝐶 is constructed with k-means++, then the corresponding objective 𝜙 satisfies, 𝐸[𝜙] ≤ 8 𝑙𝑛 𝑘 + 2 𝜙 𝑂𝑃𝑇 . In fact, this holds after only Step 1 of the algorithm above. As noted above, the rest of the algorithm (Lloyd) can only decrease 𝜙.
58
Part 1 – First center chosen
Reminder (Lloyd’s) - Lemma 2.1 Let 𝑎 1 , 𝑎 2 ,…, 𝑎 𝑛 be a set of points, if we set 𝑐= 1 𝑛 𝑖=1 𝑛 𝑎 𝑖 (centroid) then, 𝑖 𝑎 𝑖 −𝑥 2 = 𝑖 𝑎 𝑖 −𝑐 2 +𝑛 𝑐−𝑥 2 𝑖 𝑎 𝑖 −𝑥 2 = 𝑖 𝑎 𝑖 −𝑐 2 +𝑛 𝑐−𝑥 2 . Lemma 3.2 Let 𝐴 be an arbitrary cluster in 𝐶 𝑂𝑃𝑇 (optimal clustering), and let 𝐶 be the clustering with just one center, which is chosen uniformly at random from 𝐴. Then, 𝐸[𝜙(𝐴)] = 2 𝜙 𝑂𝑃𝑇 (𝐴).
59
Part 1 – First center chosen
(𝐸 𝜙 𝐴 = 2 𝜙 𝑂𝑃𝑇 (𝐴)) Proof. Let 𝑐(𝐴) denote the centroid of 𝐴. By Lemma 2.1, we know that since 𝐶 𝑂𝑃𝑇 is optimal, 𝑐(𝐴) must be the center in cluster 𝐴. 𝐸 𝜙 𝐴 = 𝐶ℎ𝑜𝑜𝑠𝑖𝑛𝑔 𝑒𝑎𝑐ℎ 𝑝𝑜𝑖𝑛𝑡 𝑎 0 𝑎𝑠 𝑐𝑒𝑛𝑡𝑒𝑟 𝑢𝑛𝑖𝑓𝑜𝑟𝑚𝑙𝑦 𝑎𝑡 𝑟𝑎𝑛𝑑𝑜𝑚 1 𝐴 𝑎 0∈𝐴 𝑎∈𝐴 𝑎− 𝑎 0 2 = 𝑳𝒆𝒎𝒎𝒂 𝟐.𝟏 1 𝐴 𝑎 0∈𝐴 ( 𝑎∈𝐴 𝑎−𝑐 𝐴 𝐴 ⋅ 𝑎 0 −𝑐(𝐴) 2 ) + 𝒄(𝑨) 𝒂 𝟎 𝒂 Change order of sums. =2 𝑎∈𝐴 𝑎−𝑐 𝐴 2 =2 𝜙 𝑂𝑃𝑇 𝐴 ■
60
Part 2 – Remaining centers
Lemma 3.3 Let 𝐴 be an arbitrary cluster in 𝐶 𝑂𝑃𝑇 , and let 𝐶 be an arbitrary clustering. If we add a center to 𝐶 from 𝐴, chosen with “ 𝐷 2 weighting”, then 𝐸 𝜙 𝐴 ≤ 8 𝜙 𝑂𝑃𝑇 𝐴 .
61
Part 2 – Remaining centers
Proof. The probability we choose some fixed 𝑎 0 as our center, given that we are choosing something from 𝑨, is 𝐷 𝑎 0 2 𝑎∈𝑨 𝐷 𝑎 2 . After choosing the center 𝑎 0 , every point 𝑎 will contribute to the potential precisely min 𝐷 𝑎 , 𝑎− 𝑎 0 2 Therefore, E 𝜙 𝐴 = 𝑎 0 ∈𝐴 𝐷 𝑎 0 2 𝑎∈𝐴 𝐷 𝑎 2 ⋅ 𝑎∈𝐴 min 𝐷 𝑎 , 𝑎− 𝑎 0 2 𝑎∈𝐴 min 𝐷 𝑎 , 𝑎− 𝑎 0 2 . + 𝒄(𝒂) 𝒂 𝒂 𝟎 𝒂− 𝒂 𝟎 𝑫(𝒂)
62
Part 2 – Remaining centers
𝐸 𝜙 𝐴 ≤ 8 𝜙 𝑂𝑃𝑇 𝐴 Power-mean inequality (PMI): 𝑎 𝑖 2 ≥ 1 𝑚 ( 𝑎 𝑖 ) 2 . From the triangle inequality: 𝐷 𝑎 0 ≤𝐷 𝑎 +|𝑎− 𝑎 0 | for all 𝑎, 𝑎 0 . Together: 𝐷 𝑎 0 2 ≤ Δ 𝐷 𝑎 + 𝑎− 𝑎 0 2 ≤ 𝑃𝑀𝐼 2⋅(𝐷 𝑎 2 +2⋅ 𝑎− 𝑎 0 2 𝑎− 𝑎 0 2 ). Summing over 𝑎: 𝐷 𝑎 0 2 ≤ 2 𝐴 𝑎∈𝐴 𝐷 𝑎 𝐴 𝑎∈𝐴 𝑎− 𝑎 0 2 . + 𝒄(𝒂) 𝒂 𝒂 𝟎 𝒂− 𝒂 𝟎 𝑫(𝒂) Triangle: Because if a!=a_0 and it doesn’t hold then D(a_0) should be D(a) instead.
63
Part 2 – Remaining centers
𝐸 𝜙 𝐴 ≤ 8 𝜙 𝑂𝑃𝑇 𝐴 From E 𝜙 𝐴 = 𝑎 0 ∈𝐴 ( 𝑫 𝒂 𝟎 𝟐 𝑎∈𝐴 𝐷 𝑎 2 𝑎∈𝐴 min 𝐷 𝑎 , 𝑎− 𝑎 0 2 ) and 𝑫 𝒂 𝟎 𝟐 ≤ 2 𝐴 ( 𝑎∈𝐴 𝐷 𝑎 2 + 𝑎∈𝐴 𝑎− 𝑎 0 2 ) we get E 𝜙 𝐴 ≤ 2 𝐴 ⋅( 𝑎 0 ∈𝐴 𝑎∈𝐴 𝐷 𝑎 2 𝑎∈𝐴 𝐷 𝑎 2 ⋅ 𝑎∈𝐴 min 𝐷 𝑎 , 𝑎− 𝑎 0 2 ≤ 𝑎− 𝑎 𝑎 0 ∈𝐴 𝑎∈𝐴 𝑎− 𝑎 0 2 𝑎∈𝐴 𝐷 𝑎 2 ⋅ 𝑎∈𝐴 min 𝐷 𝑎 , 𝑎− 𝑎 0 2 ≤𝐷 𝑎 2 )≤ 4 𝐴 𝑎 0 ∈𝐴 𝑎∈𝐴 𝑎− 𝑎 0 2 = 1 𝐴 𝑎 0∈𝐴 𝑎∈𝐴 𝑎− 𝑎 0 2 =2⋅ 𝜙 𝑂𝑃𝑇 𝐴 𝟖 𝝓 𝑶𝑷𝑻 𝑨 ■ 𝑎 0 ∈𝐴 𝑎∈𝐴 𝑎− 𝑎 0 2 = 1 𝐴 𝑎 0∈𝐴 𝑎∈𝐴 𝑎− 𝑎 0 2 =2⋅ 𝜙 𝑂𝑃𝑇 𝐴 𝟖 𝝓 𝑶𝑷𝑻 𝑨 ■ Triangle: Because if a!=a_0 and it doesn’t hold then D(a_0) should be D(a) instead.
64
Recap We have now shown that our seeding technique is competitive (𝐸 𝜙 𝐴 ≤ 8 𝜙 𝑂𝑃𝑇 𝐴 ) as long as it chooses centers from each cluster of 𝐶 𝑂𝑃𝑇 , which completes the first half of our argument. We now use induction to show the total error in general is at most 𝑂(𝑙𝑜𝑔𝑘).
65
Part 2 – Remaining centers
Intuition: Given the following 𝐶 𝑂𝑃𝑇 , our algorithm could choose 2 centers from the same “optimal” cluster, costing us in the objective’s distance from the optimal objective. + 𝒄 𝟏 𝒄 𝟐 Write the equation on board.
66
Part 2 – Remaining centers
Lemma 3.4 Let 𝐶 be an arbitrary clustering. Choose 𝑢>0 “uncovered” clusters from 𝐶 𝑂𝑃𝑇 . Let 𝑋 𝑢 denote the set of points in these clusters. Let 𝑋 𝑐 =𝑋− 𝑋 𝑢 . Now suppose we add 𝑡≤𝑢 random centers to 𝐶, chosen with 𝐷 2 weighting. Let 𝐶′ denote the the resulting clustering, and let 𝜙 ′ denote the corresponding potential. Then, 𝐸 𝜙 ′ ≤ 𝜙 𝑋 𝑐 +8 𝜙 𝑂𝑃𝑇 𝑋 𝑢 ⋅ 1+ 𝐻 𝑡 + 𝑢−𝑡 𝑢 ⋅𝜙 𝑋 𝑢 . 𝐻 𝑡 denotes the harmonic sum, ···+ 1 𝑡 . Write the equation on board.
67
𝐸 𝜙 ′ ≤ 𝜙 𝑋 𝑐 +8 𝜙 𝑂𝑃𝑇 𝑋 𝑢 ⋅ 1+ 𝐻 𝑡 + 𝑢−𝑡 𝑢 ⋅𝜙( 𝑋 𝑢 )
Final proof Proof of Theorem 3.1 (𝐸[𝜙] ≤ 8 𝑙𝑛 𝑘 + 2 𝜙 𝑂𝑃𝑇 ): Consider the clustering 𝐶 after we have completed Step 1. Let 𝐴 denote the 𝐶 𝑂𝑃𝑇 cluster in which we chose the first center. Applying Lemma 3.4 with 𝑡=𝑢=𝑘−1, and with 𝐴 being the only covered cluster, we have 𝐸 𝜙 ≤ 𝜙 𝐴 +8 𝜙 𝑂𝑃𝑇 𝑋 −8 𝜙 𝑂𝑃𝑇 𝐴 ⋅ 1+ 𝐻 𝑘−1 ≤ 2 𝜙 𝑂𝑃𝑇 𝐴 𝑳𝒆𝒎𝒎𝒂 𝟑.𝟐 +8 𝜙 𝑂𝑃𝑇 ( 𝑋 𝑘−1 )−8 𝜙 𝑂𝑃𝑇 𝐴 ⋅ 2+ ln 𝑘 𝐻 𝑡 ≤1+ ln 𝑘 ≤8( ln 𝑘 +2) 𝜙 𝑂𝑃𝑇 ■
68
A common but simple initialization method
Randomly choose 𝑘 initial centroids for the clusters, then run a chosen algorithm. Repeat the two steps multiple times. In the end, choose the clustering that yields the best optimization criterion.
69
Happy Clustering!
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.