K-means clustering

While browing videos on youtube I came across this video on K-means clustering. Once you get past the channel’s awful intro music :hear_no_evil_monkey: it was worth watching especially if you hadn’t heard of clustering before and have a curious nature.

For a statistician or researcher finding clusters of similar items in a large dataset is an important activity.

In some datasets the expected number of clusters is based on the type of data collected so we simple want to split the data into these clusters. In other datasets the number of clusters is not obvious so we need to find the optimum number first and from a programmers perspective that is the interesting and challenging part of the algorithm which is why I decided to see what I could do.

The video describes a visual approach, the ‘elbow method’ and my first challenge was to program an algorithm to do this. Later research described another algorithm the 'silhouette score` and my solution combines both algorithms.

Anyway after that long introduction here is the sketch, enjoy :grinning:
(The fully documented source code is here)

That KMeans clustering sounds interesting.
During Covid there was clustering happening, so I guess there are some real world applications.

I really like the 3D version of your KMeans clustering code.

Thanks for sharing quark.

Also stumbled across k nearest neighbors for points on a sphere on Github:
Link Here:

The 1D, 2D and 3D examples in my sketch are based on data-items with 1, 2 or 3 attributes respectively.

Performing clustering on the surface of a sphere requires 2 attributes [longitude, latitude] rather than the [x, y] used in this sketch. The clusters could be rendered in 3D on a sphere or in 2D on a Mercator projection of the sphere.

The code in my sketch can perform clustering on data-items with any number of attributes but it would be difficult to create a visualisation with 4 or more dimensions :thinking: :innocent: :grinning: