๐ง 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 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
X₁, X₂
d₁, d₂
Nearest Cluster
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:
| 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
We want to divide the six students into two groups.
3 Initialize Centroids
Initially choose two points as centroids.
These are the starting positions of the two clusters.
4 Calculate Distance
K-Means commonly uses Euclidean distance.
5 Distance for Student A
d(A,C₁) = √[(1-1)² + (2-2)²]
= √0
= 0
Distance from C₂:
d(A,C₂) = √[(1-8)² + (2-9)²]
= √[49 + 49]
= √98
= 9.899
Therefore A belongs to Cluster 1.
6 Distance for Student B
= √[1+1]
= 1.414
d(B,C₂) = √[(2-8)² + (1-9)²]
= √[36+64]
= 10
B → Cluster 1
7 Distance for Student C
= √2
= 1.414
d(C,C₂) = √[(2-8)² + (3-9)²]
= √72
= 8.485
8 Distance for Student D
= √98
= 9.899
d(D,C₂) = √[(8-8)² + (9-9)²]
= 0
9 Distance for Student E
= √100
= 10
d(E,C₂) = √[(9-8)² + (8-9)²]
= √2
= 1.414
10 Distance for Student F
= √74
= 8.602
d(F,C₂) = √[(8-8)² + (7-9)²]
= √4
= 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 2 = D, E, F
12 Update Centroids
Now calculate the mean of every feature inside each cluster.
New 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₂
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
Old C₂ = (8,9) New C₂ = (8.333,8)
14 Repeat Until Convergence
15 K-Means Mathematical Objective
K-Means tries to minimize the total squared distance between every data point and its assigned centroid.
This is commonly called the Within-Cluster Sum of Squares (WCSS).
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:② Choose K
③ Initial Centroids
④ Distance
For every point:d = √[(x₁-c₁)² + (x₂-c₂)²]
The nearest centroid is selected.
⑤ Assignment
Cluster 1: A, B, CCluster 2: D, E, F
⑥ Update Centroids
New C₁:(1.667, 2)
New C₂:
(8.333, 8)
⑦ Repeat
Cluster 1 → A, B, C
Cluster 2 → D, E, F
No comments:
Post a Comment