Step by Step
Our algorithm works as follows, assuming we have inputs x1,x2,⋯,xn and value of K
- Step 1 - Pick K random points as cluster centers called centroids.
- Step 2 - Assign each xi to nearest cluster by calculating its distance to each centroid.
- Step 3 - Find new cluster center by taking the average of the assigned points.
- Step 4 - Repeat Step 2 and 3 until none of the cluster assignments change.
Implementation
Core code
Distance calculation:
Generate Random Clustering center at first
Simple approach by scikit-learn
Application
8bit style
Read image and use k-means to do clustering for pixel value. Make pic to 8bit color style.
Reference