๐ธ K-Means Clustering Using Iris Dataset
Step-by-step mathematical demonstration of the K-Means Clustering Algorithm using Iris flower data.
1. What is K-Means?
K-Means is an unsupervised machine learning algorithm used to divide data into K groups called clusters.
Unlike supervised learning, we do not give the algorithm the correct flower species during clustering.
2. Iris Dataset
The famous Iris dataset contains measurements of iris flowers. Each flower has four numerical features:
| Feature | Description |
|---|---|
| Sepal Length | Length of sepal |
| Sepal Width | Width of sepal |
| Petal Length | Length of petal |
| Petal Width | Width of petal |
The complete Iris dataset contains 150 observations. For easy manual calculation, we will use a small sample.
3. Small Iris Dataset for Manual Calculation
| Flower | Sepal L | Sepal W | Petal L | Petal W |
|---|---|---|---|---|
| P1 | 5.1 | 3.5 | 1.4 | 0.2 |
| P2 | 4.9 | 3.0 | 1.4 | 0.2 |
| P3 | 6.4 | 3.2 | 4.5 | 1.5 |
| P4 | 6.9 | 3.1 | 4.9 | 1.5 |
| P5 | 6.3 | 3.3 | 6.0 | 2.5 |
| P6 | 5.8 | 2.7 | 5.1 | 1.9 |
1 Choose Number of Clusters
The Iris dataset naturally contains three commonly known flower species, so for this demonstration:
Therefore: Cluster 1 Cluster 2 Cluster 3
Important: K-Means itself does not know the species names. It only creates three numerical groups.
2 Choose Initial Centroids
For easy calculation, select three observations as initial centroids.
| Centroid | Based On | SL | SW | PL | PW |
|---|---|---|---|---|---|
| C1 | P1 | 5.1 | 3.5 | 1.4 | 0.2 |
| C2 | P3 | 6.4 | 3.2 | 4.5 | 1.5 |
| C3 | P5 | 6.3 | 3.3 | 6.0 | 2.5 |
3 Calculate Distance
K-Means normally uses Euclidean distance.
Here the four dimensions are:
4 Example: Calculate Distances for P2
P2 is:
Distance from C1
= √[ (-0.2)² + (-0.5)² + 0² + 0² ]
= √(0.04 + 0.25)
= √0.29
≈ 0.539
Distance from C2
= √[ 2.25 + 0.04 + 9.61 + 1.69 ]
= √13.59
≈ 3.687
Distance from C3
= √[ 1.96 + 0.09 + 21.16 + 5.29 ]
= √28.50
≈ 5.339
Therefore P2 → Cluster 1
5 Calculate Distances for All Flowers
| Point | Distance C1 | Distance C2 | Distance C3 | Cluster |
|---|---|---|---|---|
| P1 | 0.000 | 3.507 | 5.142 | C1 |
| P2 | 0.539 | 3.687 | 5.339 | C1 |
| P3 | 3.507 | 0.000 | 1.865 | C2 |
| P4 | 4.016 | 0.548 | 1.542 | C2 |
| P5 | 5.142 | 1.865 | 0.000 | C3 |
| P6 | 4.122 | 0.943 | 1.063 | C2 |
6 Calculate New Centroids
After assigning every point to a cluster, calculate the average of all points inside each cluster.
7. Calculate New Centroid C1
Cluster 1 contains:
Sepal Length
Sepal Width
Petal Length
Petal Width
8. Calculate New Centroid C2
Cluster 2 contains P3, P4 and P6.
Sepal Length
Sepal Width
Petal Length
Petal Width
9. Calculate New Centroid C3
Cluster 3 contains only P5.
Therefore:
10. Centroids After First Iteration
| Centroid | Sepal L | Sepal W | Petal L | Petal W |
|---|---|---|---|---|
| C1 | 5.000 | 3.250 | 1.400 | 0.200 |
| C2 | 6.367 | 3.000 | 4.833 | 1.633 |
| C3 | 6.300 | 3.300 | 6.000 | 2.500 |
7 Repeat the Process
11. What Does Convergence Mean?
K-Means stops when the cluster assignments or centroids stop changing significantly.
12. Final Cluster Interpretation
| Cluster | Data Points | General Pattern |
|---|---|---|
| Cluster 1 | P1, P2 | Small petals |
| Cluster 2 | P3, P4, P6 | Medium petals |
| Cluster 3 | P5 | Larger petals |
The cluster numbers themselves have no inherent meaning. For example, Cluster 1 does not automatically mean "Iris setosa". We would compare the resulting groups with known labels afterward if we wanted to evaluate the clustering.
13. Complete K-Means Mathematical Flow
14. Interactive Step-by-Step K-Means
① Dataset
② Choose K
Three clusters will be created.
③ Initial Centroids
④ Distance
⑤ Assignment
Smallest distance = C1
P2 → Cluster 1
⑥ New Centroids
C2 = Average(P3,P4,P6)
C3 = Average(P5)
⑦ Repeat
⑧ Final Result
15. Important K-Means Formulas
Euclidean Distance
Cluster Assignment
Centroid Calculation
Objective Function
K-Means attempts to minimize the total squared distance between each data point and its assigned centroid.
No comments:
Post a Comment