0% found this document useful (0 votes)
18 views5 pages

K-means & Nearest Neighbour Clustering

The tutorial focuses on clustering techniques using k-means and Nearest Neighbour algorithms. For k-means, it involves clustering 8 examples into 3 clusters using Euclidean distance, detailing the process over multiple epochs. The Nearest Neighbour method is also applied with a threshold to determine cluster membership for the same examples.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views5 pages

K-means & Nearest Neighbour Clustering

The tutorial focuses on clustering techniques using k-means and Nearest Neighbour algorithms. For k-means, it involves clustering 8 examples into 3 clusters using Euclidean distance, detailing the process over multiple epochs. The Nearest Neighbour method is also applied with a threshold to determine cluster membership for the same examples.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

DATA7703 – Machine Learning for Data Scientists S2 - 2025

Tutorial – Week 5
Clustering

The below tutorial problems are meant to be solved using pen and paper.
1) K-means clustering

Use the k-means algorithm and Euclidean distance to cluster the following 8 examples into 3
clusters:
A1=(2,10), A2=(2,5), A3=(8,4), A4=(5,8), A5=(7,5), A6=(6,4), A7=(1,2), A8=(4,9).

The distance matrix based on the Euclidean distance is given below:

Suppose that the initial seeds (centers of each cluster) are A1, A4 and A7. Run the k-means
algorithm for 1 epoch only. At the end of this epoch show:

a) The new clusters (i.e. the examples belonging to each cluster)


b) The centers of the new clusters
c) Draw a 10 by 10 space with all the 8 points and show the clusters after the first epoch and the
new
centroids.
d) How many more iterations are needed to converge? Draw the result for each epoch.

Solution:
d(a,b) denotes the Euclidean distance between a and b. It is obtained directly from the distance
matrix or calculated as follows: d(a,b)=sqrt((xb-xa)2+(yb-ya)2))

seed1=A1=(2,10),
seed2=A4=(5,8),
seed3=A7=(1,2)

epoch1 – start:

A1:
d(A1, seed1)=0 as A1 is seed1
d(A1, seed2)= √13 >0
d(A1, seed3)= √65 >0
A1 ∈ cluster1

A2:
d(A2,seed1)= √25= 5
d(A2, seed2)= √18 = 4.24
d(A2, seed3)= √10 = 3.16
A2 ∈ cluster3
A3:
d(A3, seed1)= √36= 6
d(A3, seed2)= √25 = 5
d(A3, seed3)= √53 = 7.28
A3 ∈ cluster2

A4:
d(A4, seed1)= √13
d(A4, seed2)=0 as A4 is seed2
d(A4, seed3)= √52 >0
A4 ∈ cluster2

A5:
d(A5, seed1)= √50 = 7.07
d(A5, seed2)= √13 = 3.60
d(A5, seed3)= √45 = 6.70
A5 ∈ cluster2

A6:
d(A6, seed1)= √52 = 7.21
d(A6, seed2)= √17 = 4.12
d(A6, seed3)= √29 = 5.38
A6 ∈ cluster2

A7:
d(A7, seed1)= √65 >0
d(A7, seed2)= √52 >0
d(A7, seed3)=0 as A7 is seed3
A7 ∈ cluster3

A8:
d(A8, seed1)= √5
d(A8, seed2)= √2
d(A8, seed3)= √58
A8 ∈ cluster2
end of epoch1
new clusters: 1: {A1}, 2: {A3, A4, A5, A6, A8}, 3: {A2, A7}

b) centers of the new clusters:

C1= (2, 10),


C2= ((8+5+7+6+4)/5, (4+8+5+4+9)/5) = (6, 6),
C3= ((2+1)/2, (5+2)/2) = (1.5, 3.5)
c)

d)
We would need two more epochs. After the 2nd epoch the results would be:
1: {A1, A8},
2: {A3, A4, A5, A6},
3: {A2, A7}
with centers C1=(3, 9.5), C2=(6.5, 5.25) and C3=(1.5, 3.5).

After the 3rd epoch, the results would be:


1: {A1, A4, A8},
2: {A3, A5, A6},
3: {A2, A7}
with centers C1=(3.66, 9), C2=(7, 4.33) and C3=(1.5, 3.5).
2) Nearest Neighbour clustering
Use the Nearest Neighbour clustering algorithm and Euclidean distance to cluster the examples
from the previous exercise: A1=(2,10), A2=(2,5), A3=(8,4), A4=(5,8), A5=(7,5), A6=(6,4),
A7=(1,2), A8=(4,9).
Suppose that the threshold T is 4.

Solution:
A1 is placed in a cluster by itself, so we have K1={A1}.

We then look at A2 if it should be added to K1 or be placed in a new cluster.


d(A1,A2)= √25 = 5 > T  K2={A2}

A3: we compare the distances from A3 to A1 and A2.


A3 is closer to A2 and d(A2,A1)= √37 > T  K3={A3}

A4: We compare the distances from A4 to A1, A2 and A3.


A1 is the closest object and d(A4,A1)= √13 < T  K1={A1, A4}

A5: We compare the distances from A5 to A1, A2, A3 and A4.


A3 is the closest object and d(A5,A3)= √2 < T  K3={A3, A5}

A6: We compare the distances from A6 to A1, A2, A3, A4 and A5.
A5 is the closest object and d(A6,A5)= √2 < T  K3={A3, A5, A6}

A7: We compare the distances from A7 to A1, A2, A3, A4, A5, and A6.
A2 is the closest object and d(A7,A2)= √10 < T  K2={A2, A7)

A8: We compare the distances from A8 to A1, A2, A3, A4, A5, A6 and A7.
A4 is the closest object and d(A8,A4)= √2 < T  K1={A1, A4, A8)

Common questions

Powered by AI

The choice of distance metric greatly influences the output of clustering algorithms like K-means or Nearest Neighbour, as it determines how distances between points are computed. Both algorithms use Euclidean distance, which calculates straight-line distances, impacting cluster formation. For K-means, this means points are grouped based on their proximity to centroids determined by Euclidean calculations . Similarly, Nearest Neighbour uses this metric to compare distances between points and add them to clusters only if the distance is within a specified threshold . A change in metric, like using Manhattan or cosine, would lead to different cluster shapes and possibly different cluster compositions.

The main differences between the clustering outcomes of the K-means algorithm and the Nearest Neighbour algorithm for points A1 through A8 are in how the clusters are formed and the final cluster compositions. In the K-means algorithm, the initial clusters are determined by the seed points, and after three epochs, the final clusters are: Cluster 1: {A1, A4, A8}, Cluster 2: {A3, A5, A6}, Cluster 3: {A2, A7} with respective centers C1=(3.66, 9), C2=(7, 4.33), and C3=(1.5, 3.5). The Nearest Neighbour algorithm forms clusters by sequentially evaluating the distance to the threshold and placing closest points together. The final clusters in this method are: Cluster 1: {A1, A4, A8}, Cluster 2: {A2, A7}, Cluster 3: {A3, A5, A6} . Despite different initial steps, the clusters converge to similar groupings indicating robustness across methods.

The final cluster compositions for both K-means and Nearest Neighbour algorithms show remarkable similarities despite differing methodologies. For both, the clusters formed are: Cluster 1: {A1, A4, A8}, Cluster 2: {A3, A5, A6}, and Cluster 3: {A2, A7}. These outcomes reveal that while Nearest Neighbour bases groupings on direct pairwise distance consideration relative to a threshold, K-means iteratively clusters based on distance to computed centroids . This indicates that the intrinsic spatial distribution of points in this dataset supports similar logical groupings given Euclidean distances, highlighting how different algorithms can align when data naturally cluster in certain ways.

Setting the threshold value to T=4 in Nearest Neighbour clustering determines how close points must be to form clusters. With this specific threshold, a point joins a cluster if its distance to at least one member is less than 4. This results in Cluster 1 including close points A1, A4, and A8; Cluster 2 containing A2 and A7, and Cluster 3 composed of A3, A5, and A6, reflecting how proximity and threshold compare favorably for clustering . A different threshold could lead to different assignments by either merging clusters or making them smaller depending on whether the threshold is increased or decreased.

The computational complexity of the K-means algorithm is primarily driven by the distance calculations and centroid recomputation in each iteration. The time complexity is O(n * k * i * d), where n is the number of data points (8 for the dataset A1 through A8), k is the number of clusters (3 in this case), i is the number of iterations until convergence (3 epochs in the provided solution), and d is the number of dimensions of the data points (2 since each point is a coordinate pair). Each iteration involves calculating the distance from each point to each centroid and possibly updating cluster membership, followed by recalculating centroids which involves computing the mean for each cluster. These calculations sum up over multiple iterations, contributing to the algorithm's computational burden .

In the Nearest Neighbour algorithm, the threshold value determines the maximum allowed distance for a point to be included in the same cluster as another point. For the dataset with points A1 through A8, a threshold value of 4 means that a point will only be clustered with another point if its Euclidean distance is less than or equal to 4. This threshold results in the formation of clusters with proximity-based members, leading to the final clusters being: Cluster 1: {A1, A4, A8}, Cluster 2: {A2, A7}, and Cluster 3: {A3, A5, A6} . A higher or lower threshold could change these groupings significantly.

Several factors can influence the number of epochs to convergence in K-means. The initial seed choice, spatial distribution of the data, and tolerance for movement all play roles. For A1 through A8, fewer epochs might occur if seeds start closer to natural centers, minimizing required movement. More epochs could result if initial seeds are poorly placed, requiring iterative adjustments to find natural groupings. Less variance within core point groups or high movement tolerance for centroid positionings could allow quicker stabilizations compared to highly dispersed or complex distributions all potentially lead to variable convergence pacing .

The K-means algorithm determines the number of iterations needed to achieve convergence by monitoring changes in cluster assignments and centroids. Convergence is reached when the cluster assignments do not change between iterations. In the given example, after three epochs, the clusters stabilize with no further changes in assignment or centroid positions, demonstrating convergence. The clusters after each epoch are adjusted based on recalculated centers until no significant changes occur .

In K-means clustering, centroid recalculation involves computing the mean of all points belonging to each cluster, which then becomes the new center for that cluster. For dataset points A1 through A8, after each epoch, centroids were recalculated leading to updated cluster arrangements. Initially, clusters are based on distances from initial seeds A1, A4, and A7. After recalculation, new centroids: C1=(3, 9.5), C2=(6.5, 5.25), C3=(1.5, 3.5) were formed after the second epoch stabilizing to C1=(3.66, 9), C2=(7, 4.33), C3=(1.5, 3.5) in the third, stabilizing clustering with no more reallocations of points indicated by lack of changes from previous epoch upon recomputation . This demonstrates the iterative refinement until convergence.

In the K-means algorithm, choosing initial seed points can significantly affect the clustering outcome. Strategies for selecting initial seeds include random selection, k-means++ which chooses seeds to be far apart, or using domain knowledge for informed decision. In the given problem, the seeds are initially A1, A4, and A7 . This choice determines the initial grouping but can lead to different clusters if reset differently after each epoch. In this case, after three epochs with the given initialization, the final clusters become stable with: Cluster 1: {A1, A4, A8}, Cluster 2: {A3, A5, A6}, and Cluster 3: {A2, A7} . The choice of initial seeds influences which points will initially congregate, impacting the number of iterations required for convergence.

You might also like