# Group Nearly Points by Location

**URL:** <https://forum.dynamobim.com/t/group-nearly-points-by-location/98110>\
**Category:** Developers\
**Tags:** dynamo\
**Created:** [February 15, 2024, 8:36am UTC](https://forum.dynamobim.com/t/group-nearly-points-by-location/98110 "2024-02-15T08:36:19Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![mansonchen](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/mansonchen/32/98076_2.png) [@mansonchen](https://forum.dynamobim.com/u/mansonchen)\
**Post date:** [February 15, 2024, 8:36am UTC](https://forum.dynamobim.com/t/group-nearly-points-by-location/98110/1 "2024-02-15T08:36:19Z")

</div>

Hi,

I have a list of points. My goal is to group these nearly points by the location

Please find attache a diagram:

Thank you.

 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/c/c/cced1d1f02a4c4cac05f27068050018676d6d61f.png)

---

<div class="post-metadata">

**Author:** ![Alien](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/alien/32/40701_2.png) [@Alien](https://forum.dynamobim.com/u/Alien)\
**Post date:** [February 15, 2024, 8:37am UTC](https://forum.dynamobim.com/t/group-nearly-points-by-location/98110/2 "2024-02-15T08:37:44Z")

</div>

Ah you edited it 😃

---

<div class="post-metadata">

**Author:** ![jacob.small](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/jacob.small/32/161030_2.png) [@jacob.small](https://forum.dynamobim.com/u/jacob.small)\
**Post date:** [February 15, 2024, 8:53am UTC](https://forum.dynamobim.com/t/group-nearly-points-by-location/98110/3 "2024-02-15T08:53:21Z")

</div>

If you know your maximum allowable distance to form a group, you could voxelize the points and find the ‘gaps’ by taking the void voxels. From there you have a set of cells which you can query or group by.

---

<div class="post-metadata">

**Author:** ![chuongmep](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/chuongmep/32/197537_2.png) [@chuongmep](https://forum.dynamobim.com/u/chuongmep)\
**Post date:** [February 15, 2024, 9:02am UTC](https://forum.dynamobim.com/t/group-nearly-points-by-location/98110/4 "2024-02-15T09:02:32Z")

</div>

This problem you can refenrece algorithm Kmeans : [k-means clustering - Wikipedia](https://en.wikipedia.org/wiki/K-means_clustering)

> [@Design Script - Imperative kmeans](https://forum.dynamobim.com/t/design-script-imperative-kmeans/68060/2):
>
> @Vikram_Subbaiah? Good persentation today, thanks for the inspration!

---

<div class="post-metadata">

**Author:** ![jacob.small](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/jacob.small/32/161030_2.png) [@jacob.small](https://forum.dynamobim.com/u/jacob.small)\
**Post date:** [February 15, 2024, 1:07pm UTC](https://forum.dynamobim.com/t/group-nearly-points-by-location/98110/5 "2024-02-15T13:07:47Z")

</div>

This is relevant, but not a direct solution as K Means requires you know how many groups you have to start with.

---

<div class="post-metadata">

**Author:** ![Durmus\_Cesur](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/durmus_cesur/32/62638_2.png) [@Durmus\_Cesur](https://forum.dynamobim.com/u/Durmus_Cesur)\
**Post date:** [February 15, 2024, 2:15pm UTC](https://forum.dynamobim.com/t/group-nearly-points-by-location/98110/6 "2024-02-15T14:15:39Z")

</div>

Hi,

There are always different ways of doing it.

Here’s some examples  
Gaussian Mixture:

 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/9/9/99770a4e96f0edc6da908f0b0e0ab24a93a28d0c.png)

K-Means:

 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/4/6/46cabeb791ba91eea4e09f61ed0d0372f62c7549.png)

Specktrale Clustering:

 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/2/8/2878977da01a67db61a49a848d7e233ae318a548.png)

If you have an idea of the minimum distance between them, you can make a grouping again.

> [@jacob.small](#):
>
> This is relevant, but not a direct solution as K Means requires you know how many groups you have to start with.

![ML_](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/8/2/82a29584cbc713ba7bff31957361999a7db0120c.gif)

---

<div class="post-metadata">

**Author:** ![jacob.small](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/jacob.small/32/161030_2.png) [@jacob.small](https://forum.dynamobim.com/u/jacob.small)\
**Post date:** [February 15, 2024, 2:50pm UTC](https://forum.dynamobim.com/t/group-nearly-points-by-location/98110/7 "2024-02-15T14:50:33Z")

</div>

I may be misunderstanding here, but when I compare your image to the original image I see very different results. When I see the original image the Red box only has points which are within N units of each other. The points in the blue box and cyan boxes are \>N units away away from all points in the red box, and so those are in a separate group.

And applying that to the datasets showing the colored points, we’d always have 1 color as if any point on the grid is within N units of another than all points in the grid are within N units of each other.

It may be that the un-exposed Python in the animation is doing this, but I can’t tell offhand.

---

<div class="post-metadata">

**Author:** ![Durmus\_Cesur](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/durmus_cesur/32/62638_2.png) [@Durmus\_Cesur](https://forum.dynamobim.com/u/Durmus_Cesur)\
**Post date:** [February 16, 2024, 8:37am UTC](https://forum.dynamobim.com/t/group-nearly-points-by-location/98110/8 "2024-02-16T08:37:42Z")

</div>

> [@jacob.small](#):
>
> And applying that to the datasets showing the colored points, we’d always have 1 color as if any point on the grid is within N units of another than all points in the grid are within N units of each other.

I think there is no harm in saying no to this. Because the K-Means algorithm first learns how many groups the user wants. Let’s call it “n”. Then it selects “n” number of centre points in the whole data grid and groups them by looking at the distances between them. If you want n groups, you will see n different colour groups.

The algorithm played in gifs is very different from image files. Here the user chooses the points that can be the single centre. Let’s call this “index”. Then he gives an upper limit. Let’s call this “Distance”. The algorithm regroups the entire data set according to the centre point the user has selected from the index and the distance value. In the example I have tried to show the state of each centre point in the data set. We can also observe how the whole data set is grouped by specifying one or more centre points.

I know, it sounds very complicated, but it is very simple and effective.

---

<div class="post-metadata">

**Author:** ![jacob.small](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/jacob.small/32/161030_2.png) [@jacob.small](https://forum.dynamobim.com/u/jacob.small)\
**Post date:** [February 16, 2024, 9:02am UTC](https://forum.dynamobim.com/t/group-nearly-points-by-location/98110/9 "2024-02-16T09:02:32Z")

</div>

Might be what they are after, but from what I can see it doesn’t align. Might help if you shared the python script which is doing the bulk of the work, or the dyn file.

---

<div class="post-metadata">

**Author:** ![mansonchen](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/mansonchen/32/98076_2.png) [@mansonchen](https://forum.dynamobim.com/u/mansonchen)\
**Post date:** [February 19, 2024, 6:39am UTC](https://forum.dynamobim.com/t/group-nearly-points-by-location/98110/10 "2024-02-19T06:39:51Z")

</div>

“Thanks for your response~  
I need the maximum allowable distance to form a group.  
K Means is an interesting solution if the k value can be automatically classified according to distance.”

---

<div class="post-metadata">

**Author:** ![mansonchen](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/mansonchen/32/98076_2.png) [@mansonchen](https://forum.dynamobim.com/u/mansonchen)\
**Post date:** [February 26, 2024, 2:48am UTC](https://forum.dynamobim.com/t/group-nearly-points-by-location/98110/11 "2024-02-26T02:48:47Z")

</div>

Hi Durmus\_Cesur  
May you shared the python script which is doing the bulk of the work, or the dyn file.  
thanks a lot ~

---

<div class="post-metadata">

**Author:** ![Ewan\_Opie](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/ewan_opie/32/77070_2.png) [@Ewan\_Opie](https://forum.dynamobim.com/u/Ewan_Opie)\
**Post date:** [February 26, 2024, 6:59am UTC](https://forum.dynamobim.com/t/group-nearly-points-by-location/98110/12 "2024-02-26T06:59:09Z")

</div>

> [@jacob.small](#):
>
> voxelize the points

Example of a Voxelized Rabbit for reference. 👍

Unit values need to be converted for good calculation though, where voxel coordinate sizes (which like to int’s not floats) need to be multiplied if point values are less than 1.0 int unit.

 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/6/f/6ff7537b6e73eb2bae9750628b636d8f66eb5257.jpeg)

Then grouping populated voxels by their proximity can sort the identified voxels into voxel groups.  
You just need to find the sweet spot for voxel size for your data set.

 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/c/b/cbeb4bd094123947a6fdef5b76e60a3524ba0802.jpeg)

---

<div class="post-metadata">

**Author:** ![Durmus\_Cesur](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/durmus_cesur/32/62638_2.png) [@Durmus\_Cesur](https://forum.dynamobim.com/u/Durmus_Cesur)\
**Post date:** [February 27, 2024, 9:31am UTC](https://forum.dynamobim.com/t/group-nearly-points-by-location/98110/13 "2024-02-27T09:31:25Z")

</div>

Hi,

I’m sorry it’s been so long. I’ve been going through a bad time, so I haven’t had time to look here. 🤮

You can find the code and script here.  
[Home.dyn](https://forum.dynamobim.com/uploads/short-url/zRUvTE18HWrR6HxOlI27AjCphiT.dyn) (54.9 KB)

“X\_test” compares all “X\_train” data according to each data input into “X\_test” and estimates their indexes and then their distances. It “weights” them according to their distance.  
Packages you need: Numpy, Shapely, Sklearn, Pandas, Tensorflow

```auto
# Copyright(c) 2023, Durmus Bayryam
# Gıthub : https://github.com/DurmusCesur/Shapely.git
import clr
import sys
import re
import System

clr.AddReference('Python.Included')
import Python.Included as pyInc
path_py3_lib = pyInc.Installer.EmbeddedPythonHome
sys.path.append(path_py3_lib + r'\Lib\site-packages')

import tensorflow as tf
from tensorflow.python.client import device_lib
from sklearn.neighbors import RadiusNeighborsRegressor
import numpy as np
import pandas as pd
from io import StringIO 

# Input
column = IN[1]
data = pd.read_csv(IN[0], usecols=column)
X_train = data.drop(columns=["label"])
y_train = data["label"]
X_test = IN[2]
radius = IN[3]
algorithm = IN[4]

sys.stdout = StringIO()

def calculate_weights(distances, scale_factor=1.0):
    weights = 10000 / (distances + scale_factor)
    return weights

# Training
model = RadiusNeighborsRegressor(radius=radius, algorithm=algorithm)
model.fit(X_train, y_train)

# Prec
y_test_pred,weight = [],[]

for test_example in X_test:
    neighbors_indices = model.radius_neighbors([test_example], return_distance=False)[0]
    y_test_pred.append(neighbors_indices)
    
    #distance
    neighbors_distance = model.radius_neighbors([test_example], return_distance=True)[0]
    weights = calculate_weights(neighbors_distance)
    weight.append(weights)
    

# Result
sys.stdout.seek(0)
readerstdout = sys.stdout.read()
   
OUT = y_test_pred,weight

```
