Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

KMeans clustering

Intuition

Practice

Loading...
Loading...
Loading...
Loading...
Loading...

Elbow method

The elbow method is a heuristic used in determining the number of clusters in a data set. The method consists of plotting the explained variation as a function of the number of clusters, and picking the elbow of the curve as the number of clusters to use.

Loading...
Loading...

By observing the plot of explained variation as a function of the number of clusters, we can see that the inertia decreases quickly at k=2 and k=3. After k=3, the decrease in inertia is much slower. Therefore, we can conclude that the optimal number of clusters is k=3.

If we use Dunn Index, the peak of the curve is at k=3 as well.

Finally, we plot the clusters and the centroids when k=3 as follows:

Loading...
Loading...

Appendix

Calculating the distance pairwisely between two collections of vectors with vectorization

  • Non-vectorized version

17 μs ± 683 ns per loop (mean ± std. dev. of 7 runs, 1,000 loops each)
  • Vectorized version

4.91 μs ± 68.6 ns per loop (mean ± std. dev. of 7 runs, 1,000 loops each)