Total Pageviews

Monday, August 31, 2026

๐Ÿง  K-Means Clustering with ANN-Style Explanation

๐Ÿง  K-Means Clustering with ANN-Style Explanation

Step-by-step mathematical explanation of K-Means clustering, presented using the concept of inputs, distance calculation, assignment and iterative learning.

⚠️ Important Concept

K-Means is NOT an Artificial Neural Network algorithm.

K-Means is an unsupervised machine learning algorithm. However, we can explain its learning process using an ANN-style structure:

Input Data → Distance Calculation → Assignment → Update → Repeat

1. ANN-Style Representation

Input Layer
X₁, X₂
Distance Layer
d₁, d₂
Assignment
Nearest Cluster
Update
Centroids

The important difference is that there are no trainable neural-network weights and biases here. Instead, K-Means repeatedly updates the cluster centroids.

1 Input Dataset

Consider the following six students represented by two numerical features:

X₁ = Study Hours X₂ = Exam Score
Student Study Hours Exam Score
A 1 2
B 2 1
C 2 3
D 8 9
E 9 8
F 8 7

2 Choose Number of Clusters

K = 2

We want to divide the six students into two groups.

Cluster 1
Cluster 2

3 Initialize Centroids

Initially choose two points as centroids.

C₁ = (1, 2) C₂ = (8, 9)

These are the starting positions of the two clusters.

4 Calculate Distance

K-Means commonly uses Euclidean distance.

d(X,C) = √[ (x₁-c₁)² + (x₂-c₂)² ]

5 Distance for Student A

A = (1,2) C₁ = (1,2) C₂ = (8,9)
Distance from C₁:
d(A,C₁) = √[(1-1)² + (2-2)²]
= √0
= 0

Distance from C₂:
d(A,C₂) = √[(1-8)² + (2-9)²]
= √[49 + 49]
= √98
= 9.899
0 < 9.899

Therefore A belongs to Cluster 1.

6 Distance for Student B

B = (2,1)
d(B,C₁) = √[(2-1)² + (1-2)²]
= √[1+1]
= 1.414

d(B,C₂) = √[(2-8)² + (1-9)²]
= √[36+64]
= 10
1.414 < 10

B → Cluster 1

7 Distance for Student C

C = (2,3)
d(C,C₁) = √[(2-1)² + (3-2)²]
= √2
= 1.414

d(C,C₂) = √[(2-8)² + (3-9)²]
= √72
= 8.485
C → Cluster 1

8 Distance for Student D

D = (8,9)
d(D,C₁) = √[(8-1)² + (9-2)²]
= √98
= 9.899

d(D,C₂) = √[(8-8)² + (9-9)²]
= 0
D → Cluster 2

9 Distance for Student E

E = (9,8)
d(E,C₁) = √[(9-1)² + (8-2)²]
= √100
= 10

d(E,C₂) = √[(9-8)² + (8-9)²]
= √2
= 1.414
E → Cluster 2

10 Distance for Student F

F = (8,7)
d(F,C₁) = √[(8-1)² + (7-2)²]
= √74
= 8.602

d(F,C₂) = √[(8-8)² + (7-9)²]
= √4
= 2
F → Cluster 2

11 Cluster Assignment

Student Distance C₁ Distance C₂ Cluster
A 0 9.899 C₁
B 1.414 10 C₁
C 1.414 8.485 C₁
D 9.899 0 C₂
E 10 1.414 C₂
F 8.602 2 C₂
Cluster 1 = A, B, C

Cluster 2 = D, E, F

12 Update Centroids

Now calculate the mean of every feature inside each cluster.

New C₁

C₁ = Mean(A,B,C)

X₁: (1 + 2 + 2) / 3 = 5/3 = 1.667

X₂: (2 + 1 + 3) / 3 = 6/3 = 2

New C₁ = (1.667 , 2)

New C₂

C₂ = Mean(D,E,F)

X₁: (8 + 9 + 8) / 3 = 25/3 = 8.333

X₂: (9 + 8 + 7) / 3 = 24/3 = 8

New C₂ = (8.333 , 8)

13 Iteration 2

The centroids have moved:
Old C₁ = (1,2) New C₁ = (1.667,2)
Old C₂ = (8,9) New C₂ = (8.333,8)
Now K-Means calculates the distances again.

14 Repeat Until Convergence

Distance ↓ Assignment ↓ Calculate Mean ↓ Move Centroid ↓ Distance Again ↓ Assignment Again ↓ Repeat
When the cluster assignments stop changing, the algorithm has approximately converged.

15 K-Means Mathematical Objective

K-Means tries to minimize the total squared distance between every data point and its assigned centroid.

J = ฮฃ ||Xแตข - Cโ‚–||²

This is commonly called the Within-Cluster Sum of Squares (WCSS).

Smaller WCSS ↓ Tighter clusters ↓ Better clustering

16. K-Means vs ANN

K-Means Artificial Neural Network
Unsupervised Usually supervised, but can also be unsupervised
No labels required Depends on architecture
Uses centroids Uses weights and biases
Distance calculation Weighted sum + activation
Updates centroids Updates weights
Uses K clusters Uses neurons/layers

17. ⭐ Interactive Step-by-Step

① Input

The input contains two numerical features:
X₁ = Study Hours X₂ = Exam Score

② Choose K

K = 2
We want two clusters.

③ Initial Centroids

C₁ = (1,2) C₂ = (8,9)

④ Distance

For every point:

d = √[(x₁-c₁)² + (x₂-c₂)²]

The nearest centroid is selected.

⑤ Assignment

Cluster 1: A, B, C

Cluster 2: D, E, F

⑥ Update Centroids

New C₁:
(1.667, 2)

New C₂:
(8.333, 8)

⑦ Repeat

Distance → Assignment → Mean → New Centroids → Repeat
The process continues until the assignments stop changing.
๐ŸŽฏ FINAL CLUSTERS

Cluster 1 → A, B, C
Cluster 2 → D, E, F

18. Complete Mathematical Flow

INPUT DATA ↓ Choose K ↓ Initialize Centroids ↓ Calculate Euclidean Distance ↓ Assign Each Point ↓ Calculate Mean ↓ Update Centroids ↓ Calculate Distance Again ↓ Repeat ↓ Convergence ↓ FINAL CLUSTERS

No comments:

Post a Comment