Total Pageviews

Monday, August 31, 2026

๐ŸŒธ K-Means Clustering Using Iris Dataset

๐ŸŒธ 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.

Input Data ↓ Choose K ↓ Select Initial Centroids ↓ Calculate Distances ↓ Assign Data to Nearest Centroid ↓ Calculate New Centroids ↓ Repeat ↓ Clusters Stabilize

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:

K = 3

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.

Distance = √[(x₁-c₁)² + (x₂-c₂)² + (x₃-c₃)² + (x₄-c₄)²]

Here the four dimensions are:

x₁ = Sepal Length x₂ = Sepal Width x₃ = Petal Length x₄ = Petal Width

4 Example: Calculate Distances for P2

P2 is:

P2 = (4.9, 3.0, 1.4, 0.2)

Distance from C1

d(P2,C1) = √[ (4.9-5.1)² + (3.0-3.5)² + (1.4-1.4)² + (0.2-0.2)² ]

= √[ (-0.2)² + (-0.5)² + 0² + 0² ]

= √(0.04 + 0.25)

= √0.29

≈ 0.539

Distance from C2

d(P2,C2) = √[ (4.9-6.4)² + (3.0-3.2)² + (1.4-4.5)² + (0.2-1.5)² ]

= √[ 2.25 + 0.04 + 9.61 + 1.69 ]

= √13.59

≈ 3.687

Distance from C3

d(P2,C3) = √[ (4.9-6.3)² + (3.0-3.3)² + (1.4-6.0)² + (0.2-2.5)² ]

= √[ 1.96 + 0.09 + 21.16 + 5.29 ]

= √28.50

≈ 5.339
Nearest Centroid = C1

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
Cluster 1: P1, P2
Cluster 2: P3, P4, P6
Cluster 3: P5

6 Calculate New Centroids

After assigning every point to a cluster, calculate the average of all points inside each cluster.

New Centroid = Sum of all points in cluster ÷ Number of points in cluster

7. Calculate New Centroid C1

Cluster 1 contains:

P1 = (5.1, 3.5, 1.4, 0.2) P2 = (4.9, 3.0, 1.4, 0.2)

Sepal Length

(5.1 + 4.9) / 2 = 10 / 2 = 5.0

Sepal Width

(3.5 + 3.0) / 2 = 6.5 / 2 = 3.25

Petal Length

(1.4 + 1.4) / 2 = 1.4

Petal Width

(0.2 + 0.2) / 2 = 0.2
New C1 = (5.0, 3.25, 1.4, 0.2)

8. Calculate New Centroid C2

Cluster 2 contains P3, P4 and P6.

P3 = (6.4, 3.2, 4.5, 1.5) P4 = (6.9, 3.1, 4.9, 1.5) P6 = (5.8, 2.7, 5.1, 1.9)

Sepal Length

(6.4 + 6.9 + 5.8) / 3 = 19.1 / 3 ≈ 6.367

Sepal Width

(3.2 + 3.1 + 2.7) / 3 = 9.0 / 3 = 3.0

Petal Length

(4.5 + 4.9 + 5.1) / 3 = 14.5 / 3 ≈ 4.833

Petal Width

(1.5 + 1.5 + 1.9) / 3 = 4.9 / 3 ≈ 1.633
New C2 ≈ (6.367, 3.0, 4.833, 1.633)

9. Calculate New Centroid C3

Cluster 3 contains only P5.

P5 = (6.3, 3.3, 6.0, 2.5)

Therefore:

New C3 = (6.3, 3.3, 6.0, 2.5)

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

New Centroids ↓ Calculate Distances Again ↓ Assign Points Again ↓ Calculate Centroids Again ↓ Check Whether Centroids Changed ↓ If Changed → Repeat If Not Changed → Stop

11. What Does Convergence Mean?

K-Means stops when the cluster assignments or centroids stop changing significantly.

Centroid Change ≈ 0 ↓ No Important Cluster Changes ↓ Algorithm Converged
๐ŸŽฏ K-Means has found stable clusters.

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

Iris Dataset
Choose K = 3
Initial Centroids
Calculate Distance
Assign Cluster
New Centroids
Repeat

14. Interactive Step-by-Step K-Means

① Dataset

Each flower: [Sepal Length, Sepal Width, Petal Length, Petal Width]

② Choose K

K = 3

Three clusters will be created.

③ Initial Centroids

C1 = P1 C2 = P3 C3 = P5

④ Distance

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

⑤ Assignment

P2: d(C1) = 0.539 d(C2) = 3.687 d(C3) = 5.339

Smallest distance = C1
P2 → Cluster 1

⑥ New Centroids

C1 = Average(P1,P2)
C2 = Average(P3,P4,P6)
C3 = Average(P5)

⑦ Repeat

New Centroids ↓ Distances ↓ Cluster Assignment ↓ New Centroids ↓ Repeat Until Convergence

⑧ Final Result

Cluster 1 → P1, P2 Cluster 2 → P3, P4, P6 Cluster 3 → P5
๐ŸŒธ K-Means Clustering Completed

15. Important K-Means Formulas

Euclidean Distance

d(x,c) = √ฮฃ(xแตข-cแตข)²

Cluster Assignment

Assign x to the cluster whose centroid has the minimum distance.

Centroid Calculation

Cโฑผ = 1 / |Cโฑผ| × ฮฃ xแตข

Objective Function

J = ฮฃ ฮฃ ||xแตข - ฮผโฑผ||²

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

16. Important Terms

Iris Dataset Unsupervised Learning K-Means Cluster Centroid Euclidean Distance Assignment Iteration Convergence Feature Distance Mean

No comments:

Post a Comment