Implementation of K-Means Clustering
Experiment
Implementation of K-Means Clustering
๐ฏ Objective
To implement the K-Means clustering algorithm from scratch using Python and understand its working through iterative centroid updates.
๐ Theory
๐น Clustering
Clustering is an unsupervised learning method that groups similar data points into clusters.
๐น K-Means Algorithm (Manual Implementation)
K-Means divides the dataset into K clusters such that the sum of squared distances between points and their cluster centroid is minimized.
๐น Algorithm Steps
- Choose number of clusters K
- Select initial centroids (randomly or manually)
-
For each data point:
- Compute distance to all centroids
- Assign it to nearest centroid
-
Update centroids:
- Take mean of all points in each cluster
- Repeat steps 3–4 until centroids do not change
๐น Distance Formula (Euclidean Distance)
| Point | X | Y |
|---|---|---|
| P1 | 1 | 1 |
| P2 | 1.5 | 2 |
| P3 | 3 | 4 |
| P4 | 5 | 7 |
| P5 | 3.5 | 5 |
| P6 | 4.5 | 5 |
| P7 | 3.5 | 4.5 |
๐ป Program (From Scratch Implementation)
๐ Output
๐น Final Centroids (Approx)
๐น Cluster Labels
๐ Result
The K-Means algorithm was successfully implemented from scratch, and the dataset was partitioned into 2 clusters using iterative centroid updates.
- K-Means works through iterative refinement
- Manual implementation improves conceptual clarity

Comments
Post a Comment