Function Repository Resource:

ClusteringTreeHeightHistogram

Source Notebook

Make a histogram of the heights in a clustering tree

Contributed by: Robert Nachbar

ResourceFunction["ClusteringTreeHeightHistogram"][tree]

plots a histogram of the cluster heights in the ClusteringTree tree.

ResourceFunction["ClusteringTreeHeightHistogram"][tree,bspec]

plots a histogram with bin width specification bspec.

ResourceFunction["ClusteringTreeHeightHistogram"][tree,bspec,hspec]

plots a histogram with bin heights computed according to the specification hspec.

Details and Options

A Histogram extension of ClusteringTree.
The histogram heights can be used as an aid for choosing where to cut the tree.
ClusteringTreeHeightHistogram takes the same options as Histogram.

Examples

Basic Examples (2) 

Obtain a cluster hierarchy from a list of numbers:

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};
In[2]:=
tree = ClusteringTree[data]
Out[2]=

Plot the histogram of heights at which the clusters merge:

In[3]:=
ResourceFunction["ClusteringTreeHeightHistogram"][tree]
Out[3]=

Scope (2) 

Generate a ClusteringTree from data:

In[4]:=
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[5]=

Specify the bin width:

In[6]:=
ResourceFunction["ClusteringTreeHeightHistogram"][tree, {0.02}]
Out[6]=

Applications (6) 

Make a list of data:

In[7]:=
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[8]:=
tree = ClusteringTree[cities]
Out[8]=

Plot the histogram of heights to help choose a height at which to cut the tree:

In[9]:=
ResourceFunction["ClusteringTreeHeightHistogram"][tree, {25}]
Out[9]=

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

In[10]:=
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[10]=

Extract the clusters at a height of 200:

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

Plot the locations of the cites colored by cluster:

In[12]:=
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[12]=

Publisher

Robert Nachbar

Requirements

Wolfram Language 13.0 (December 2021) or above

Version History

  • 1.0.0 – 25 March 2024

Related Resources

License Information