Function Repository Resource:

ClusteringTreeClustersByHeight

Source Notebook

Cut a ClusteringTree graph at a given height and return the resulting clusters

Contributed by: Robert Nachbar

ResourceFunction["ClusteringTreeClustersByHeight"][tree, h]

gives the clusters in the ClusteringTree tree when cut at height h.

Details

tree is a Graph returned by ClusteringTree and must have the annotations VertexWeight and "LeafLabels".
ResourceFunction["ClusteringTreeClustersByHeight"] is an efficient way to reuse a ClusteringTree Graph which is expensive to compute.

Examples

Basic Examples (2) 

Using given data, find the ClusteringTree:

In[1]:=
data = {4.15, 4.74, 5.02, 6.36, 6.72, 11.49, 10.4, 8.98, 11.81, 13.49,
    12.04, 9.19, 21.45, 28.43, 23.16, 23.79, 24.56, 21.89};
tree = ClusteringTree[data]
Out[2]=

Obtain a cluster hierarchy from the given tree:

In[3]:=
ResourceFunction["ClusteringTreeClustersByHeight"][tree, 0.2]
Out[3]=

Applications (5) 

Make a list of data:

In[4]:=
cities = {Entity["City", {"Sydney", "NewSouthWales", "Australia"}], Entity["City", {"Melbourne", "Victoria", "Australia"}], Entity["City", {"Brisbane", "Queensland", "Australia"}], Entity["City", {"Perth", "WesternAustralia", "Australia"}], Entity["City", {"Adelaide", "SouthAustralia", "Australia"}], Entity["City", {"GoldCoast", "Queensland", "Australia"}], Entity["City", {"Newcastle", "NewSouthWales", "Australia"}], Entity["City", {"Canberra", "AustralianCapitalTerritory", "Australia"}], Entity["City", {"SunshineCoast", "Queensland", "Australia"}], Entity["City", {"Wollongong", "NewSouthWales", "Australia"}], Entity["City", {"Blacktown", "NewSouthWales", "Australia"}], Entity["City", {"Hobart", "Tasmania", "Australia"}], Entity["City", {"CityOfStirling", "WesternAustralia", "Australia"}], Entity["City", {"Geelong", "Victoria", "Australia"}], Entity["City", {"Townsville", "Queensland", "Australia"}], Entity["City", {"Gosford", "NewSouthWales", "Australia"}], Entity["City", {"Ipswich", "Queensland", "Australia"}], Entity["City", {"Cairns", "Queensland", "Australia"}], Entity["City", {"Toowoomba", "Queensland", "Australia"}], Entity["City", {"Darwin", "NorthernTerritory", "Australia"}], Entity["City", {"Launceston", "Tasmania", "Australia"}]};

Compute the clustering tree:

In[5]:=
tree = ClusteringTree[cities]
Out[5]=

Plot the dendrogram to help choose a height at which to cut the tree:

In[6]:=
Dendrogram[tree, Right, GridLines -> {{200}, None}, Sequence[
 GridLinesStyle -> RGBColor[1, 0, 0], Frame -> {{False, False}, {True, True}}, ImageSize -> Medium, PlotRangePadding -> {
Scaled[0.02], 
Scaled[0.03]}]]
Out[6]=

Compute the clusters at a height of 200:

In[7]:=
ResourceFunction["ClusteringTreeClustersByHeight"][tree, 200]
Out[7]=

Plot the locations of the cites colored by cluster:

In[8]:=
Graphics[{LightGray, CountryData["Australia", "Polygon"], MapThread[{cluster, color} |-> ({PointSize[Large], color, Tooltip[Point[Reverse[CityData[#, "Coordinates"]]], CityData[#, "Name"]]} & /@ cluster), {clusters, ColorData[97] /@ Range@Length@clusters}]}]
Out[8]=

Publisher

Robert Nachbar

Requirements

Wolfram Language 13.0 (December 2021) or above

Version History

  • 1.0.0 – 25 March 2024

Related Resources

License Information