The Question
CodingK-Means Clustering Implementation
Implement the K-Means clustering algorithm to partition a set of $N$ points into $K$ clusters.
You are given:
-
points: A list of $D$-dimensional coordinates (as tuples).
- k: The number of clusters to identify.
- initial_centroids: A list of $K$ points representing the starting positions of the centroids.
- max_iterations: The maximum number of assignment/update cycles to perform.
Your algorithm should follow these steps:
1. Assignment: Assign each point to the cluster associated with the nearest centroid using squared Euclidean distance.
2. Update: Recompute the centroid of each cluster by taking the arithmetic mean of all points assigned to that cluster. If a cluster has no assigned points, its centroid remains unchanged for that iteration.
3. Convergence: If the centroids do not change between two consecutive iterations, terminate early.
The final output should be a list of the $K$ cluster centroids, with each coordinate rounded to exactly 4 decimal places.Python
K-Means Clustering
Euclidean Distance