Before the lab, eight points.
Eight Points, Three Clusters
The original K-means exercise: assign eight points, move three centroids, print the iterations, repeat.
- Python
- NumPy
- Object-oriented modelling
Public source
A small enough world to inspect
Separate Point and Cluster classes hold membership and representative positions. Each iteration finds the nearest centroid, moves observations between clusters, updates the means, and compares centroid movement.
An idea worth returning to
The original README proposed plotting points and animating the changing clusters. In 2026, Clustering 3D Lab takes that direction further—and preserves the original eight-point dataset as a link back to this implementation.
8 POINTS → 3 INITIAL CENTROIDS
│ │
└─ nearest ────┘
│
update means
│
repeat until settledMembership belongs to points; the mean belongs to a cluster.
- 01 / controller
kMeans
Eight input coordinates
Three initial representatives
- generate points → 2. Point
- generate clusters → 3. Cluster
- 02 / class
Point
id · x · y · cluster_number
set_cluster_number()
- coordinates → 4. find_closest_centroid
- 03 / class
Cluster
cluster_points · centroid
calculate_distance()
- centroids → 4. find_closest_centroid
- 04 / assignment
find_closest_centroid
argmin of distances
Remove / add membership
- new memberships → 5. update_centroid
- 05 / update
update_centroid
Mean of member coordinates
Compare old / new centres
- movement → 6. is_converged
- 06 / termination
is_converged
Sum of movement == 0
Or iteration bound reached
- continue if moving → 4. find_closest_centroid
- 1kMeans Pointgenerate points
- 2kMeans Clustergenerate clusters
- 3Point find_closest_centroidcoordinates
- 4Cluster find_closest_centroidcentroids
- 5find_closest_centroid update_centroidnew memberships
- 6update_centroid is_convergedmovement
- 7is_converged find_closest_centroidcontinue if moving
Read from the implementation
kmeans.pypoint.pycluster.py