An open API service indexing awesome lists of open source software.

https://github.com/centrogeo/hierarchicalgeoclustering


https://github.com/centrogeo/hierarchicalgeoclustering

Last synced: about 1 year ago
JSON representation

Awesome Lists containing this project

README

          

Hierarchical Geo Clustering
================

## Install

The library uses the cgal library, if the library is use inside a conda
enviroment the installation procedure should create the enviroment and
install the cgal library using `conda install -c conda-forge cgal`.

To install the library can be simply:

`pip install git+https://github.com/CentroGeo/HierarchicalGeoClustering.git`

## How to use

### Build a TreeCluster

We can build a hierarchical geographical cluster, the `TreeCluster`
class allows to create te structure and then populate it.

``` python
HGC = TreeClusters(3, random_seed= 12)
```

``` python
HGC.populate_tree(number_per_cluster=100)
```

``` python
HGC.print_structure()
```

Root
Root
├── Root_l_0_n_0
│ ├── Root_l_0_n_0_l_1_n_0
│ │ ├── Root_l_0_n_0_l_1_n_0_l_2_n_0
│ │ ├── Root_l_0_n_0_l_1_n_0_l_2_n_1
│ │ ├── Root_l_0_n_0_l_1_n_0_l_2_n_2
│ │ ├── Root_l_0_n_0_l_1_n_0_l_2_n_3
│ │ └── Root_l_0_n_0_l_1_n_0_l_2_n_4
│ └── Root_l_0_n_0_l_1_n_1
│ ├── Root_l_0_n_0_l_1_n_1_l_2_n_0
│ └── Root_l_0_n_0_l_1_n_1_l_2_n_1
├── Root_l_0_n_1
│ ├── Root_l_0_n_1_l_1_n_0
│ │ └── Root_l_0_n_1_l_1_n_0_l_2_n_0
│ ├── Root_l_0_n_1_l_1_n_1
│ │ ├── Root_l_0_n_1_l_1_n_1_l_2_n_0
│ │ ├── Root_l_0_n_1_l_1_n_1_l_2_n_1
│ │ └── Root_l_0_n_1_l_1_n_1_l_2_n_2
│ ├── Root_l_0_n_1_l_1_n_2
│ ├── Root_l_0_n_1_l_1_n_3
│ │ ├── Root_l_0_n_1_l_1_n_3_l_2_n_0
│ │ └── Root_l_0_n_1_l_1_n_3_l_2_n_1
│ └── Root_l_0_n_1_l_1_n_4
│ ├── Root_l_0_n_1_l_1_n_4_l_2_n_0
│ ├── Root_l_0_n_1_l_1_n_4_l_2_n_1
│ └── Root_l_0_n_1_l_1_n_4_l_2_n_2
└── Root_l_0_n_2
├── Root_l_0_n_2_l_1_n_0
│ ├── Root_l_0_n_2_l_1_n_0_l_2_n_0
│ └── Root_l_0_n_2_l_1_n_0_l_2_n_1
├── Root_l_0_n_2_l_1_n_1
│ ├── Root_l_0_n_2_l_1_n_1_l_2_n_0
│ ├── Root_l_0_n_2_l_1_n_1_l_2_n_1
│ ├── Root_l_0_n_2_l_1_n_1_l_2_n_2
│ ├── Root_l_0_n_2_l_1_n_1_l_2_n_3
│ └── Root_l_0_n_2_l_1_n_1_l_2_n_4
├── Root_l_0_n_2_l_1_n_2
│ ├── Root_l_0_n_2_l_1_n_2_l_2_n_0
│ ├── Root_l_0_n_2_l_1_n_2_l_2_n_1
│ └── Root_l_0_n_2_l_1_n_2_l_2_n_2
└── Root_l_0_n_2_l_1_n_3
├── Root_l_0_n_2_l_1_n_3_l_2_n_0
├── Root_l_0_n_2_l_1_n_3_l_2_n_1
├── Root_l_0_n_2_l_1_n_3_l_2_n_2
├── Root_l_0_n_2_l_1_n_3_l_2_n_3
└── Root_l_0_n_2_l_1_n_3_l_2_n_4

``` python
fig, axs = plt.subplots( figsize=(8,8))
HGC.visualize(axs, polygon=True)
```

![](index_files/figure-gfm/cell-5-output-1.png)

``` python
HGC.root.polygon_cluster
```

![](index_files/figure-gfm/cell-6-output-1.svg)

``` python
HGC.get_deepth()
```

4

### Extract the points and clusterize

To clusterize we extract the points and use the clusterize tool
implemented in the `Clustering` module

``` python
original_points= HGC.get_points_tree()
X_2=np.array([[p.x,p.y] for p in original_points])
dic_points={'points':[X_2], 'parent':''}
```

``` python
HGC_adapta_DBSCAN = recursive_clustering_tree(dic_points,
levels_clustering = 3,
algorithm = 'adaptative_DBSCAN'
)
```

``` python
fig, axs = plt.subplots( figsize=(8,8))
HGC_adapta_DBSCAN.visualize(axs, polygon=True)
```

![](index_files/figure-gfm/cell-10-output-1.png)

### Get the Similarity Shape Measurement

To obtain the SSM the clusters tree has to be used, this due to the use
of the geometric shape in the metric. Therefore the polygons of each
cluster on each level has to be able to compare.

``` python
form_metric=[]

for l in range(0, 4):
d = { 'Level': l,
'adaptive DBSCAN':SSM(HGC.levels_nodes[l],
HGC_adapta_DBSCAN.levels_nodes[l])
}
form_metric.append(d)
```

``` python
form_metric
```

[{'Level': 0, 'adaptive DBSCAN': 0.6046098496677557},
{'Level': 1, 'adaptive DBSCAN': 0.6195370749041116},
{'Level': 2, 'adaptive DBSCAN': 0.4080125145736268},
{'Level': 3, 'adaptive DBSCAN': 0.22732397098600046}]

``` python
df_metric_form = pd.DataFrame(form_metric)
```

``` python
df_metric_form
```

.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}

.dataframe tbody tr th {
vertical-align: top;
}

.dataframe thead th {
text-align: right;
}




Level
adaptive DBSCAN




0
0
0.604610


1
1
0.619537


2
2
0.408013


3
3
0.227324

A detail
[documentation](https://centrogeo.github.io/HierarchicalGeoClustering/)
is available