The K-means algorithm is so simple that we can quickly write it in 100 lines of code. The algorithm goes through the following steps to reach its goal:
- First, it picks up data points from the samples as initial centroids.
- Then, it assigns each sample to the closest centroid (
) using the following formula:
- Next, it updates the centroids so that they are at the center of the new clusters being led by the new cluster assignment.
- Repeat steps 2-3 until the cluster assignment remains unchanged or reaches the maximum number of iterations. ...