Notes
Notes
It is called unsupervised because the data does not already have labels.
The algorithm tries to find natural groups in the data.
Main idea
K-means groups observations so that:
For example:
(x1 , y1 )
and
(x2 , y2 )
d = √(x1 − x2 )2 + (y1 − y2 )2
The new centroid is the mean of all the points in that cluster.
Step 4: Repeat
The algorithm repeats these two steps:
Objective of K-means
K-means tries to minimize the total distance between each point and its cluster centroid.
K
∑ ∑ ∥xi − µk ∥2
k=1 i∈Ck
where:
Simple example
Suppose we have data on students using:
hours studied;
test marks.
Important points
K-means works best when:
For example, if one variable is income and another variable is age, income may dominate the distance calculation because it has larger values.
Advantages of K-means
K-means is useful because:
it is simple to understand;
it is fast;
it works well for large datasets;
it is easy to implement.
Disadvantages of K-means
K-means has some limitations:
Choosing K
A common method for choosing K is the elbow method.
K = 1, 2, 3, 4, 5, …
The best value of K is often chosen at the point where the decrease starts to slow down.
Summary
K-means clustering is a method that groups data into K clusters.
It works by:
The goal is to form clusters where points are close to their own centroid and far from other cluster centroids.
Hierarchical Clustering
Hierarchical clustering is an unsupervised learning method used to group observations into clusters. It is called hierarchical because it builds a tree-like structure of clusters. This
tree is called a dendrogram. Unlike K-means clustering, hierarchical clustering does not require us to choose the number of clusters at the start.
Main idea
Hierarchical clustering groups observations based on how similar or close they are.
The closer two observations are, the more likely they are to be grouped together.
Steps
Suppose we have n observations.
Step 5: Repeat
Repeat the process until all observations are grouped into one cluster.
In simple words
Agglomerative clustering works like this:
Steps
Step 1: Start with one cluster
All observations begin in one large cluster.
Step 4: Stop
The process stops when each observation is in its own cluster or when the desired number of clusters is reached.
In simple words
Divisive clustering works like this:
Dendrogram
A dendrogram is a tree diagram that shows how clusters are formed. It is used to visualize hierarchical clustering.
For example:
The number of vertical lines crossed by the cut gives the number of clusters.
(x1 , y1 )
and
(x2 , y2 )
d = √(x1 − x2 )2 + (y1 − y2 )2
In general, for two observations xi and xj , the distance can be written as:
d(xi , xj )
Linkage methods
A linkage method tells us how to measure the distance between two clusters.
1. Single Linkage
Single linkage uses the minimum distance between points in two clusters.
Single linkage:
2. Complete Linkage
Complete linkage uses the maximum distance between points in two clusters.
Complete linkage:
3. Average Linkage
Average linkage uses the mean distance between all pairs of points in two clusters.
1
d(A, B) = ∑ ∑ d(i, j)
|A||B| i∈A j∈B
Average linkage:
Number of clusters Does not need K at the start Must choose K at the start
Flexibility Can choose clusters after viewing dendrogram Must decide K before running
Summary
Hierarchical clustering is an unsupervised learning method that groups observations into clusters by building a hierarchy.
1. Agglomerative clustering, which starts with individual observations and merges them.
2. Divisive clustering, which starts with one large cluster and splits it.
The distance between clusters is determined using linkage methods such as:
single linkage;
complete linkage;
average linkage;
centroid linkage.
Hierarchical clustering is useful because it shows the structure of the data and does not require choosing the number of clusters before running the algorithm.
It is based on the idea that clusters are found in areas where the data points are dense.
Unlike k-NN classification, k-NN clustering does not use known class labels.
Main idea
The main idea is:
So, k-NN clustering tries to identify the dense regions in the data and group observations according to those regions.
k
f^(x) =
nV (x)
where:
Intuition
If the k nearest neighbours are very close to x, then the volume V (x) is small.
If the k nearest neighbours are far away from x, then the volume V (x) is large.
Therefore:
and
Role of k
The value of k controls the smoothness of the density estimate.
So:
and
If k is too large, the method becomes too smooth and may hide important cluster structure.
So:
Observations that move toward the same mode are placed in the same cluster.
In simple words
k-NN mode seeking clustering follows the idea:
Points that end at the same mode belong to the same cluster.
Instead of using the distance to the k-th nearest neighbour, KDE places a smooth kernel function over each observation.
The densities from all the kernels are then added together to form a smooth density estimate.
KDE formula
For observations x1 , x2 , … , xn , the KDE at point x is:
n
1 x − xi
f^(x) = ∑K( )
nh h
i=1
where:
Bandwidth
The bandwidth h controls the smoothness of the KDE.
So:
and
In KDE:
Main idea Uses distance to the k-th nearest neighbour Uses kernel weights around each point
Density high when k neighbours are close Many points are close to x
Density low when k neighbours are far Few points are close to x
The difference is that the density estimate comes from KDE instead of k-NN.
So:
In simple words
Using KDE in mode seeking clustering means:
it is non-parametric;
it does not assume a specific distribution;
it can find clusters based on density;
it can identify clusters through density modes;
it is an alternative to mean shift clustering.
Mean shift usually uses KDE to estimate the density and then moves points toward density peaks.
k-NN mode seeking uses k-NN density estimation, but KDE can be used as an alternative.
So:
and
False.
If k is too close to n, the density estimate becomes too smooth and may hide cluster structure.
Statement 2
k-NN mode seeking clustering and k-NN classification are essentially the same.
False.
Statement 3
k-NN mode seeking clustering is computationally expensive.
True.
It can be computationally expensive because distances between many observations need to be calculated.
Statement 4
k-NN mode seeking is an alternative to mean shift clustering.
True.
Statement 5
k-NN mode seeking uses non-parametric density estimation.
True.
It uses k-NN density estimation, and KDE can also be used as an alternative non-parametric density estimator.
Statement 6
k-NN mode seeking clustering is a density-based clustering algorithm.
True.
Summary
k-NN clustering is a density-based unsupervised learning method.
In k-NN density estimation, density is based on the distance to the k-th nearest neighbour.
k
f^(x) =
nV (x)
Both k-NN density estimation and KDE are non-parametric density estimation methods.
Observations that move toward the same density mode are placed in the same cluster.
Model-Based Clustering
Model-based clustering is a clustering approach where we assume that the data comes from a mixture of probability distributions. Instead of only grouping observations based on
distance, model-based clustering assumes that each cluster is generated by an underlying statistical model.
In simple words:
The aim is to estimate these distributions and decide which observation most likely belongs to which cluster.
For example, suppose we have G clusters. Then the overall distribution of the data can be written as a mixture:
G
f(x) = ∑ πg fg (x)
g=1
where:
Mixing proportions
The value πg represents the probability that an observation belongs to cluster g.
0 ≤ πg ≤ 1
and
G
∑ πg = 1
g=1
Each cluster contributes to the overall distribution according to its mixing proportion.
Example
Suppose student marks come from three groups:
Cluster Interpretation
The total distribution of marks is then a mixture of the three group distributions.
G
f(x) = ∑ πg ϕ(x; µg , Σg )
g=1
where:
a mean vector, µg ;
a covariance matrix, Σg ;
a mixing proportion, πg .
Soft clustering
In soft clustering, an observation can partially belong to more than one cluster.
For example:
x1 0.90 0.10
x2 0.55 0.45
x3 0.20 0.80
Hard clustering
Although GMM gives probabilities, we can still assign each observation to one cluster.
P (Cluster 1 ∣ xi ) = 0.70
and
P (Cluster 2 ∣ xi ) = 0.30
The Expectation-Maximisation algorithm, or EM algorithm, is used to estimate the model parameters when the cluster memberships are hidden.
EM algorithm
The EM algorithm has two main steps:
1. E-step
2. M-step
These steps are repeated until the estimates stop changing much.
τig = P (zi = g ∣ xi )
where:
πg ϕ(xi ; µg , Σg )
τig = G
∑h=1 πh ϕ(xi ; µh , Σh )
EM algorithm summary
The EM algorithm works as follows:
In simple words:
Normal distribution;
Poisson distribution;
Binomial distribution;
Gamma distribution;
Exponential distribution.
Instead, each cluster can follow a distribution from the exponential family.
G
f(x) = ∑ πg fg (x; θg )
g=1
where:
Examples
Different data types may require different mixture models.
G e−λg λxg
f(x) = ∑ πg
x!
g=1
where:
number of claims;
number of accidents;
number of purchases;
number of website visits.
G
f(x) = ∑ πg pxg (1 − pg )1−x
g=1
where:
x can be 0 or 1;
pg is the probability of success in cluster g.
For example:
Equal covariance All clusters have the same shape and size
Mixture of regressions
In mixture of regressions, each cluster has its own regression model.
For example:
y = β 0g + β 1g x + ϵ
dimension reduction;
parsimonious covariance structures;
factor analyzers;
variable selection.
Robust extensions use distributions with heavier tails, such as the t-distribution.
A t-mixture model can handle outliers better than a Gaussian mixture model.
This is useful because the EM algorithm can estimate missing values while estimating cluster membership.
A. Likelihood
The likelihood measures how well the model explains the observed data.
n G
L = ∏ ∑ πg fg (xi ; θg )
i=1 g=1
n G
ℓ = ∑ log(∑ πg fg (xi ; θg ))
i=1 g=1
However, using more clusters usually increases the likelihood, so we need penalties for model complexity.
B. AIC
The Akaike Information Criterion is:
AIC = −2ℓ + 2m
where:
ℓ is the log-likelihood;
m is the number of estimated parameters.
C. BIC
The Bayesian Information Criterion is:
where:
ℓ is the log-likelihood;
m is the number of estimated parameters;
n is the sample size.
BIC penalizes model complexity more strongly than AIC, especially when the sample size is large.
D. Entropy
Entropy measures the uncertainty in cluster assignment.
n G
E = − ∑ ∑ τig log(τig )
i=1 g=1
where:
E. Posterior probabilities
Posterior probabilities show how strongly each observation belongs to each cluster.
F. Confusion matrix
If true labels are available, a confusion matrix can be used to compare the cluster labels with the true labels.
G. Silhouette coefficient
The silhouette coefficient measures how well an observation fits into its assigned cluster compared with other clusters.
For observation i:
bi − a i
si =
max(ai , bi )
where:
ai is the average distance from observation i to other observations in the same cluster;
bi is the smallest average distance from observation i to observations in another cluster.
Assumption Clusters based on distance to centroid Data comes from mixture distributions
Summary
Model-based clustering assumes that the data comes from a mixture of probability distributions.
G
f(x) = ∑ πg fg (x; θg )
g=1
Mixture models can also be built using other distributions from the exponential family, such as Poisson, Bernoulli, Gamma, and Exponential distributions.
log-likelihood;
AIC;
BIC;
entropy;
posterior probabilities;
silhouette coefficient.
Model-based clustering is more flexible than K-means because it allows probability-based clustering and can measure uncertainty in cluster membership.
In [ ]:
In [ ]:
In [ ]:
In [ ]: