K-Means Clustering

Hi all,

I am wondering if anyone is familiar with the K-Means clustering component in the Ivy Plugin?

I am interested in implementing the algorithm through either C# or python in GH in general (and ideally with no plugins) for panelization of surfaces with limited number of unit types similar to this:

I’ve come across two threads on the old forum but haven’t been able to see much of the codes
2. https://www.grasshopper3d.com/photo/copycating-the-soumaya-museum-guys

Any pointer would be much appreciated!

@nejur might be able to spill the beans on what’s going on inside his Ivy implementation.

  1. Download Accord. Net library that runs on both C# and python.

  2. Follow the documentation to run K Means clustering.

  3. The cluster inputs is just point x y z values and additional stuff you would like to add. The more dimensions you want to cluster the more noise you get

I did this for my Fox plugin, lunch box also implemented Accord NET for machine learning compinents.

Documentation is really straight forward.

thanks for the input @AndersDeleuran and @Petras_Vestartas

Hi Petras,

EDIT: imported Accord, .,MachineLearning, .Math, and .Statistics DLLs as referenced assemblies into the C# component and a test code calling the KMeans class seems to be working now.
If possible, might you be able to provide a sample of how you call and return values from some of the functions?

Nevermind the samples, found a demo:

Thanks nonetheless!

Hi @MateuszZwierzycki,

Might you be able to expand a bit more on the method used in the post below especially regarding the pre-processing of the data set before feeding into the k-means clustering step?

My first guess is that you have both length and angle parameters in the array of 6-dimensional coordinates. How are they normalized - is it simply converting both types of values to between 0 and 1?

Hi @Petras_Vestartas, if you might be able to enlighten me a bit on processes you’ve been working on that’d be awesome too.
A quick test GH file and screenshot of clustering points with locations below (C# component referencing Accord, .MachineLearning, .Math, and .Statistics DLLs):
kmeans_exp_1.gh (9.7 KB)

Thanks all!


I think the 6 dimensions were XY coordinates of each point regarding some arbitrary central plane of each panel. Certainly not the smartest method, but it did something. This was a setup I did with Anemone and native gh components (each 6d point is a list of numbers).
You might be interested in using the KMeans component from Owl, the setup is simpler than the one with accord suggested here.

Owl’s source code is now on github, so if you’re interested in my kmeans implementation it’s here https://github.com/mateuszzwierzycki/Owl/blob/master/Owl.Learning/Clustering/KMeansEngine.vb


Thanks for responding and for the link to your source code!
Those inputs make sense for triangular panels -
Might you have any advice on the input list of numbers for quad panels?
Perhaps the two diagonal lengths and some others?

Download Accord. Net library that runs on both C# and python

Can you explain how to use it within a python component?


Rewrite this in python you will need System.Collections because you cannot use python lists.

This is another example from Accord.Net library that I did while working at CITA, try re-read it and adapt it to KMeans clustering, the logic is similar, but the array is a bit confusing. This gives you a grasp how to import .NET libraries:

1 Like

Thanks for the prompt answer, very helpful!