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
We can build a hierarchical geographical cluster, the TreeCluster
class allows to create te structure and then populate it.
HGC = TreeClusters(3, random_seed= 12)
HGC.populate_tree(number_per_cluster=100)
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
fig, axs = plt.subplots( figsize=(8,8))
HGC.visualize(axs, polygon=True)
HGC.root.polygon_cluster
HGC.get_deepth()
4
To clusterize we extract the points and use the clusterize tool
implemented in the Clustering
module
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':''}
HGC_adapta_DBSCAN = recursive_clustering_tree(dic_points,
levels_clustering = 3,
algorithm = 'adaptative_DBSCAN'
)
fig, axs = plt.subplots( figsize=(8,8))
HGC_adapta_DBSCAN.visualize(axs, polygon=True)
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.
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)
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}]
df_metric_form = pd.DataFrame(form_metric)
df_metric_form
.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 is available