# K-Means Clustering

**URL:** <https://discourse.mcneel.com/t/k-means-clustering/74903>\
**Category:** Scripting\
**Tags:** script, clustering\
**Created:** [November 16, 2018, 2:14am UTC](https://discourse.mcneel.com/t/k-means-clustering/74903 "2018-11-16T02:14:21Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![timothytai](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/timothytai/32/649892_2.png) [@timothytai](https://discourse.mcneel.com/u/timothytai)\
**Post date:** [November 16, 2018, 2:14am UTC](https://discourse.mcneel.com/t/k-means-clustering/74903/1 "2018-11-16T02:14:21Z")

</div>

Hi all,

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

> **[k-means clustering with Ivy](https://www.grasshopper3d.com/video/k-means-clustering-with-ivy)**
>
> Region detection based on the geometrical characteristics of a mesh. The algorithm just needs the number of regions. In this example detection is based on the…

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:  
https://player.vimeo.com/video/111300393?app_id=122963

I’ve come across two threads on the old forum but haven’t been able to see much of the codes  
1.[https://www.grasshopper3d.com/forum/topics/kmeans-1?id=2985220%3ATopic%3A215631&page=1#comments](https://www.grasshopper3d.com/forum/topics/kmeans-1?id=2985220%3ATopic%3A215631&page=1#comments)  
2. [https://www.grasshopper3d.com/photo/copycating-the-soumaya-museum-guys](https://www.grasshopper3d.com/photo/copycating-the-soumaya-museum-guys)

Any pointer would be much appreciated!

---

<div class="post-metadata">

**Author:** ![AndersDeleuran](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/andersdeleuran/32/476683_2.png) [@AndersDeleuran](https://discourse.mcneel.com/u/AndersDeleuran)\
**Post date:** [November 16, 2018, 7:55am UTC](https://discourse.mcneel.com/t/k-means-clustering/74903/2 "2018-11-16T07:55:25Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Petras\_Vestartas](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/petras_vestartas/32/480531_2.png) [@Petras\_Vestartas](https://discourse.mcneel.com/u/Petras_Vestartas)\
**Post date:** [November 16, 2018, 8:22am UTC](https://discourse.mcneel.com/t/k-means-clustering/74903/3 "2018-11-16T08:22:29Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![timothytai](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/timothytai/32/649892_2.png) [@timothytai](https://discourse.mcneel.com/u/timothytai)\
**Post date:** [November 18, 2018, 10:08pm UTC](https://discourse.mcneel.com/t/k-means-clustering/74903/4 "2018-11-18T22:08:22Z")

</div>

thanks for the input @AndersDeleuran and @Petras_Vestartas

---

<div class="post-metadata">

**Author:** ![timothytai](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/timothytai/32/649892_2.png) [@timothytai](https://discourse.mcneel.com/u/timothytai)\
**Post date:** [November 20, 2018, 10:42pm UTC](https://discourse.mcneel.com/t/k-means-clustering/74903/5 "2018-11-20T22:42:31Z")

</div>

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?

EDIT2:  
Nevermind the samples, found a demo:

> **[accord-net/framework](https://github.com/accord-net/framework/wiki/Clustering)**
>
> Machine learning, computer vision, statistics and general scientific computing for .NET - accord-net/framework

Thanks nonetheless!

---

<div class="post-metadata">

**Author:** ![timothytai](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/timothytai/32/649892_2.png) [@timothytai](https://discourse.mcneel.com/u/timothytai)\
**Post date:** [November 27, 2018, 8:24pm UTC](https://discourse.mcneel.com/t/k-means-clustering/74903/6 "2018-11-27T20:24:43Z")

</div>

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?

> **[Copycating the Soumaya Museum guys...](https://www.grasshopper3d.com/photo/copycating-the-soumaya-museum-guys)**
>
> Playing with optimization concept used for Soumaya Museum's facade.
> 
> \- 6-dimensional k-means clustering
> \- ~6000 triangular panels grouped in 15 families
> \- equi…

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](https://global.discourse-cdn.com/mcneel/uploads/default/original/3X/a/d/ad27d9bd75de4e4619fccfe39b406d350adc11a2.gh) (9.7 KB)

Thanks all!

 ![kmeans_1](https://global.discourse-cdn.com/mcneel/uploads/default/original/3X/d/f/df4d0bce4c8a7183a8e93309f9ec708027810e51.png)

---

<div class="post-metadata">

**Author:** ![MateuszZwierzycki](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/mateuszzwierzycki/32/756427_2.png) [@MateuszZwierzycki](https://discourse.mcneel.com/u/MateuszZwierzycki)\
**Post date:** [November 30, 2018, 8:58am UTC](https://discourse.mcneel.com/t/k-means-clustering/74903/7 "2018-11-30T08:58:25Z")

</div>

Hey  
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](https://github.com/mateuszzwierzycki/Owl/blob/master/Owl.Learning/Clustering/KMeansEngine.vb)

---

<div class="post-metadata">

**Author:** ![timothytai](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/timothytai/32/649892_2.png) [@timothytai](https://discourse.mcneel.com/u/timothytai)\
**Post date:** [November 30, 2018, 2:16pm UTC](https://discourse.mcneel.com/t/k-means-clustering/74903/8 "2018-11-30T14:16:49Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![charles2](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/charles2/32/296662_2.png) [@charles2](https://discourse.mcneel.com/u/charles2)\
**Post date:** [March 25, 2019, 4:03pm UTC](https://discourse.mcneel.com/t/k-means-clustering/74903/9 "2019-03-25T16:03:12Z")

</div>

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

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

---

<div class="post-metadata">

**Author:** ![Petras\_Vestartas](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/petras_vestartas/32/480531_2.png) [@Petras\_Vestartas](https://discourse.mcneel.com/u/Petras_Vestartas)\
**Post date:** [March 25, 2019, 4:12pm UTC](https://discourse.mcneel.com/t/k-means-clustering/74903/10 "2019-03-25T16:12:41Z")

</div>

Hi,

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

> **[Clustering · accord-net/framework Wiki](https://github.com/accord-net/framework/wiki/Clustering)**
>
> Machine learning, computer vision, statistics and general scientific computing for .NET - Clustering · accord-net/framework Wiki

This is another example from [Accord.Net](http://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:

 ![image](https://global.discourse-cdn.com/mcneel/uploads/default/original/3X/8/7/8743de97ae5191a4ce9cec8b01681edc18133f22.png)

---

<div class="post-metadata">

**Author:** ![charles2](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/charles2/32/296662_2.png) [@charles2](https://discourse.mcneel.com/u/charles2)\
**Post date:** [March 25, 2019, 5:12pm UTC](https://discourse.mcneel.com/t/k-means-clustering/74903/11 "2019-03-25T17:12:29Z")

</div>

> [@Petras\_Vestartas](#):
>
> CITA

Thanks for the prompt answer, very helpful!
