CS7641 UL02. Clustering Mohamed Ameen Amer
UL02. Clustering
Clustering Problem:
• Unsupervised learning: Making sense out of unlabeled data (data description).
• The clustering problem:
- Given: A set of objects 𝑋
Inter-object distances 𝐷(𝑥, 𝑦) = 𝐷(𝑦, 𝑥) 𝑥, 𝑦 ∈ 𝑋
- Output: Partition 𝑃𝐷 (𝑥) = 𝑃𝐷 (𝑦) 𝑖𝑓 𝑥 𝑎𝑛𝑑 𝑦 𝑎𝑟𝑒 𝑖𝑛 𝑡ℎ𝑒 𝑠𝑎𝑚𝑒 𝑐𝑙𝑢𝑠𝑡𝑒𝑟
Single Linkage Clustering – SLC:
• Consider each object a cluster (𝑛 objects).
• Define inter-cluster distance as the distance between the closest points in the two clusters.
• Merge the two closest clusters.
• Repeat 𝑛 − 𝑘 times to make 𝑘 clusters.
- 𝑘 is an input.
• How you define the inter-cluster distance is a domain knowledge.
• Running time of SLC: 𝑂(𝑛3 )
• Issues with SLC: It might end up with wrong clusters, depending on the distance definition.
𝑘-Means Clustering:
• Pick 𝑘 random center points.
• Each center claims its closest points.
• Recompute the centers by averaging the clustered points.
• Repeat until converge.
• 𝑘-means in Euclidean space:
− 𝑃𝑡 (𝑥): Partition/cluster of object 𝑥.
− 𝐶𝑖𝑡 : Set of all points in cluster 𝑖 = {𝑥 𝑠𝑢𝑐ℎ 𝑡ℎ𝑎𝑡 𝑃(𝑥) = 𝑖}
∑𝑦∈𝐶 𝑡 𝑦
− 𝑐𝑒𝑛𝑡𝑒𝑟𝑖𝑡 : 𝑖 ⁄
|𝐶𝑖 |
2
𝑃𝑡 (𝑥) = 𝑎𝑟𝑔𝑚𝑖𝑛𝑖 ‖𝑥 − 𝑐𝑒𝑛𝑡𝑒𝑟𝑖𝑡−1 ‖2
∑𝑦∈𝐶 𝑡 𝑦
𝑐𝑒𝑛𝑡𝑒𝑟𝑖𝑡 = 𝑖 ⁄
|𝐶𝑖 |
𝑡 (𝑥).
Use the new center to re-compute 𝑃
Page | 1
,CS7641 UL02. Clustering Mohamed Ameen Amer
• 𝑘-means as an optimization problem:
- Configurations:
𝑐𝑒𝑛𝑡𝑒𝑟
𝑃
- Scores: How much error you introduce by representing these points as centers.
2
𝐸(𝑃, 𝑐𝑒𝑛𝑡𝑒𝑟) = ∑‖𝑐𝑒𝑛𝑡𝑒𝑟𝑃(𝑥) − 𝑥‖2
𝑥
- Neighborhood: The neighborhood of a configuration is the set of pairs where you keep the
centers the same and you change the partitions, or you keep the partitions the same and you
move the centers.
𝑃, 𝑐𝑒𝑛𝑡𝑒𝑟 = {(𝑃′ , 𝑐𝑒𝑛𝑡𝑒𝑟)} ⋃{𝑃, 𝑐𝑒𝑛𝑡𝑒𝑟 ′ }
- Looking into 𝑘-means this way, we can notice that it follows the same procedures of Hill
Climbing:
1. The first function that updates 𝑃𝑡 (𝑥) moves the cluster only if the error goes down.
2. The second function that updates 𝑐𝑒𝑛𝑡𝑒𝑟𝑖𝑡 can never bring the error up because average
minimizes squared error.
• Properties of 𝑘-means clustering:
- Each iteration is polynomial 𝑂(𝑘𝑛).
- Finite (exponential) iterations 𝑂(𝑘 𝑛 ).
- Error decreases (if ties are broken consistently).
- Can get stuck, if it started with wrong centers. This is similar to converging to a local optima. One
solution to this problem is to use random restarts.
Soft Clustering:
• Soft Clustering attaches cluster probability to each point, instead of a specific cluster.
• Assume the data was generated by:
- Select one of 𝑘 possible Gaussians (Fixed know variance) uniformly.
- Sample 𝑥𝑖 from that Gaussian.
- Repeat 𝑛 times.
• Task: Find a hypothesis ℎ = 〈𝜇1 , … , 𝜇𝑘 〉 (𝜇1 , … , 𝜇𝑘 are Gaussian means) that maximizes the
probability of the data (Maximum likelihood).
• Maximum Likelihood Gaussian: The Maximum Likelihood mean of the Gaussian 𝜇 is the mean of the
data.
Page | 2
, CS7641 UL02. Clustering Mohamed Ameen Amer
Expectation Maximization – EM:
• EM assigns a cluster probability to each point.
𝑃(𝑥 = 𝑥𝑖 |𝜇 = 𝜇𝑗 )
𝐸[𝑍𝑖𝑗 ] =
∑𝑘𝑖=1 𝑃(𝑥 = 𝑥𝑖 |𝜇 = 𝜇𝑗 )
∑𝑖 𝐸[𝑍𝑖𝑗 ]𝑥𝑖
𝜇𝑗 =
∑𝑖 𝐸[𝑍𝑖𝑗 ]
−1 2 2
𝑃(𝑥 = 𝑥𝑖 |𝜇 = 𝜇𝑗 ) = 𝑒 2 𝜎 (𝑥𝑖−𝜇𝑗)
- Use the new 𝜇𝑗 to re-compute 𝐸[𝑍𝑖𝑗 ].
- Expectation: 𝐸[𝑍𝑖𝑗 ] defines the probability that element 𝑖 was produced by cluster 𝑗.
- Maximization: 𝜇𝑗 defines the mean of cluster 𝑗.
• EM properties:
- Monotonically non-decreasing likelihood: It’s not getting worse.
- Does not converge (practically converge).
- Will not diverge.
- Most probably, it will get stuck in a local optima: Use random restarts.
- Works with any distribution (if 𝐸 and 𝜇 are solvable).
Clustering Properties:
• Richness: For any assignment 𝐶 of objects to clusters, there’s some distance matrix 𝐷 such that 𝑃𝐷
returns that clustering 𝐶.
• Scale-invariance: Scaling distances by a positive value doesn’t change the clustering:
∀𝐷 ∀𝑘>0 𝑃𝐷 = 𝑃𝑘𝐷
• Consistency: shrinking intra-cluster distances (Moving points towards each other) and expanding
inter-cluster (Moving clusters away from each other) distances doesn’t change the clustering:
𝑃𝐷 = 𝑃́𝐷 .
• Impossibility Theorem: There’s no clustering algorithm that can achieve these three properties.
Page | 3
UL02. Clustering
Clustering Problem:
• Unsupervised learning: Making sense out of unlabeled data (data description).
• The clustering problem:
- Given: A set of objects 𝑋
Inter-object distances 𝐷(𝑥, 𝑦) = 𝐷(𝑦, 𝑥) 𝑥, 𝑦 ∈ 𝑋
- Output: Partition 𝑃𝐷 (𝑥) = 𝑃𝐷 (𝑦) 𝑖𝑓 𝑥 𝑎𝑛𝑑 𝑦 𝑎𝑟𝑒 𝑖𝑛 𝑡ℎ𝑒 𝑠𝑎𝑚𝑒 𝑐𝑙𝑢𝑠𝑡𝑒𝑟
Single Linkage Clustering – SLC:
• Consider each object a cluster (𝑛 objects).
• Define inter-cluster distance as the distance between the closest points in the two clusters.
• Merge the two closest clusters.
• Repeat 𝑛 − 𝑘 times to make 𝑘 clusters.
- 𝑘 is an input.
• How you define the inter-cluster distance is a domain knowledge.
• Running time of SLC: 𝑂(𝑛3 )
• Issues with SLC: It might end up with wrong clusters, depending on the distance definition.
𝑘-Means Clustering:
• Pick 𝑘 random center points.
• Each center claims its closest points.
• Recompute the centers by averaging the clustered points.
• Repeat until converge.
• 𝑘-means in Euclidean space:
− 𝑃𝑡 (𝑥): Partition/cluster of object 𝑥.
− 𝐶𝑖𝑡 : Set of all points in cluster 𝑖 = {𝑥 𝑠𝑢𝑐ℎ 𝑡ℎ𝑎𝑡 𝑃(𝑥) = 𝑖}
∑𝑦∈𝐶 𝑡 𝑦
− 𝑐𝑒𝑛𝑡𝑒𝑟𝑖𝑡 : 𝑖 ⁄
|𝐶𝑖 |
2
𝑃𝑡 (𝑥) = 𝑎𝑟𝑔𝑚𝑖𝑛𝑖 ‖𝑥 − 𝑐𝑒𝑛𝑡𝑒𝑟𝑖𝑡−1 ‖2
∑𝑦∈𝐶 𝑡 𝑦
𝑐𝑒𝑛𝑡𝑒𝑟𝑖𝑡 = 𝑖 ⁄
|𝐶𝑖 |
𝑡 (𝑥).
Use the new center to re-compute 𝑃
Page | 1
,CS7641 UL02. Clustering Mohamed Ameen Amer
• 𝑘-means as an optimization problem:
- Configurations:
𝑐𝑒𝑛𝑡𝑒𝑟
𝑃
- Scores: How much error you introduce by representing these points as centers.
2
𝐸(𝑃, 𝑐𝑒𝑛𝑡𝑒𝑟) = ∑‖𝑐𝑒𝑛𝑡𝑒𝑟𝑃(𝑥) − 𝑥‖2
𝑥
- Neighborhood: The neighborhood of a configuration is the set of pairs where you keep the
centers the same and you change the partitions, or you keep the partitions the same and you
move the centers.
𝑃, 𝑐𝑒𝑛𝑡𝑒𝑟 = {(𝑃′ , 𝑐𝑒𝑛𝑡𝑒𝑟)} ⋃{𝑃, 𝑐𝑒𝑛𝑡𝑒𝑟 ′ }
- Looking into 𝑘-means this way, we can notice that it follows the same procedures of Hill
Climbing:
1. The first function that updates 𝑃𝑡 (𝑥) moves the cluster only if the error goes down.
2. The second function that updates 𝑐𝑒𝑛𝑡𝑒𝑟𝑖𝑡 can never bring the error up because average
minimizes squared error.
• Properties of 𝑘-means clustering:
- Each iteration is polynomial 𝑂(𝑘𝑛).
- Finite (exponential) iterations 𝑂(𝑘 𝑛 ).
- Error decreases (if ties are broken consistently).
- Can get stuck, if it started with wrong centers. This is similar to converging to a local optima. One
solution to this problem is to use random restarts.
Soft Clustering:
• Soft Clustering attaches cluster probability to each point, instead of a specific cluster.
• Assume the data was generated by:
- Select one of 𝑘 possible Gaussians (Fixed know variance) uniformly.
- Sample 𝑥𝑖 from that Gaussian.
- Repeat 𝑛 times.
• Task: Find a hypothesis ℎ = 〈𝜇1 , … , 𝜇𝑘 〉 (𝜇1 , … , 𝜇𝑘 are Gaussian means) that maximizes the
probability of the data (Maximum likelihood).
• Maximum Likelihood Gaussian: The Maximum Likelihood mean of the Gaussian 𝜇 is the mean of the
data.
Page | 2
, CS7641 UL02. Clustering Mohamed Ameen Amer
Expectation Maximization – EM:
• EM assigns a cluster probability to each point.
𝑃(𝑥 = 𝑥𝑖 |𝜇 = 𝜇𝑗 )
𝐸[𝑍𝑖𝑗 ] =
∑𝑘𝑖=1 𝑃(𝑥 = 𝑥𝑖 |𝜇 = 𝜇𝑗 )
∑𝑖 𝐸[𝑍𝑖𝑗 ]𝑥𝑖
𝜇𝑗 =
∑𝑖 𝐸[𝑍𝑖𝑗 ]
−1 2 2
𝑃(𝑥 = 𝑥𝑖 |𝜇 = 𝜇𝑗 ) = 𝑒 2 𝜎 (𝑥𝑖−𝜇𝑗)
- Use the new 𝜇𝑗 to re-compute 𝐸[𝑍𝑖𝑗 ].
- Expectation: 𝐸[𝑍𝑖𝑗 ] defines the probability that element 𝑖 was produced by cluster 𝑗.
- Maximization: 𝜇𝑗 defines the mean of cluster 𝑗.
• EM properties:
- Monotonically non-decreasing likelihood: It’s not getting worse.
- Does not converge (practically converge).
- Will not diverge.
- Most probably, it will get stuck in a local optima: Use random restarts.
- Works with any distribution (if 𝐸 and 𝜇 are solvable).
Clustering Properties:
• Richness: For any assignment 𝐶 of objects to clusters, there’s some distance matrix 𝐷 such that 𝑃𝐷
returns that clustering 𝐶.
• Scale-invariance: Scaling distances by a positive value doesn’t change the clustering:
∀𝐷 ∀𝑘>0 𝑃𝐷 = 𝑃𝑘𝐷
• Consistency: shrinking intra-cluster distances (Moving points towards each other) and expanding
inter-cluster (Moving clusters away from each other) distances doesn’t change the clustering:
𝑃𝐷 = 𝑃́𝐷 .
• Impossibility Theorem: There’s no clustering algorithm that can achieve these three properties.
Page | 3