Kmeans from A to Z
For this project, I implemented the Kmeans as well as the kmeans++ algorithm from scratch. I used five data sets to showcase some applications and results of those algorithms. Further, after uncovering the drawbacks of Kmeans, I implemented a ‘Spectral clustering’ using the random forest (RF) technique paired with kmeans++ to overcome the ‘discontinuity of clusters’ issue. Lastly, I addressed some limitations and possible improvements for future research reference.
Procedure:
-
Initialize centroids꞉ Randomly initialize k number of data points from the original X data. The number of k depends on how many clusters we want to end up with.
-
Compute distance꞉ Here I used Euclidean distance to measure the distance from each of the remaining data points to each of the centroids we initialized in step 1, assigning each of the remaining data points to the ‘closest ’ centroids.
-
Update centroids꞉ Within each cluster, compute the average distance of all the data points to that centroid FEATURE WISE. This average distance will be the new centroids’ ‘coordinate’ in that cluster. Intuitively speaking, this means we are correcting the centroids to be the ‘center’ of that cluster. This means our final centroids will most likely not be members of the dataset. The reason we picked data points from the dataset as initial centroids is simply to assign a starting point.
-
Reassign data point Finally, compute distance, reassign data points according to the new centroids we updated in step 3, update centroids. Iterate the above process until the centroids’ ‘coordinates’ don’t change any more.
ISSUES:
Each rounds’ initial centroids are different due to the mechanism of random initialization. This goes without saying, but the algorithm produces slightly different final centroids, labels, and clusters. The good news is all the final norms are 0, which means those clusters make perfect sense in each of their own round’s ‘world’.
Procedure:
-
Initialize centroids꞉ Randomly initialize k number of data points from the original X data. The number of k depends on how many clusters we want to end up with.
-
Compute distance꞉ Here I used Euclidean distance to measure the distance from each of the remaining data points to each of the centroids we initialized in step 1, assigning each of the remaining data points to the ‘closest ’ centroids.
-
Update centroids꞉ Within each cluster, compute the average distance of all the data points to that centroid FEATURE WISE. This average distance will be the new centroids’ ‘coordinate’ in that cluster. Intuitively speaking, this means we are correcting the centroids to be the ‘center’ of that cluster. This means our final centroids will most likely not be members of the dataset. The reason we picked data points from the dataset as initial centroids is simply to assign a starting point.
-
Reassign data point Finally, compute distance, reassign data points according to the new centroids we updated in step 3, update centroids. Iterate the above process until the centroids’ ‘coordinates’ don’t change any more.
- Synthetic data set
- Multi-dimension data (Circle data 500*2)
As you can see, Kmeans performs poorly on disjointed and nested structures. To rescue, I will introduce spectral clustering by using RF and Kmeans together in the Advanced topic section.
-
Breast cancer
- Without scaling X꞉
* With scaled X
Procedure:
-
RF ‘group’ similar data points.
-
Construct frequency (similarity) matrix
-
Feed similarity matrix to SpectralClustering
Test on circle data (sklearn vs. RF+Kmeans)
-
Randomness in RF will sometimes result in unexpected cluster labels (accuracy is not as steady).
-
Kmeans++ only considers picking the furthest point to its previous centroid. Take k=3 as an example, as a consequence, the 1st and 3rd controls can sometimes be quite close to each other. Is there a way to consider all the previous centroids and pick the furthest point from all the previous centroids? How do we even define the ‘minimum distance’ since each centroid has its own furthest points, one point can't be the furthest to multiple centroids?
Synthetic data set: A small synthetic data that has a shape of 16*1Multi-dimension data (Circle data 500*2): from sklearn.datasets.make_circlesBreast cancer: from sklearn.datasets.load_breast_cancernorth-africa-1940s-greyandparrt-vancouver.jpg: from Professor Terence Parr
Jupyter Notebook file: workspace where I performed and tested the works.kmeans.py: modularized support functions
kmeans.pdf: pdf presentation
img: png files were used in this project.
- Jupyter Notebook
- Atom
- Python 3.9
- Numpy
- Pandas
- Matplotlib
- Seaborn
- sklearn
- statistics
- PIL
- tqdm









