class

HClust::Dendrogram

Inherits Reference < Object

A step-wise dendrogram that encodes the arrangement of the clusters produced by hierarchical clustering as a binary tree.

A dendrogram consists of a sequence of N - 1 merge steps (see Step), where N is the number of elements or observations that were clustered, and a step corresponds to a merge between two distinct clusters.

The labeling of the clusters follows the SciPy convention, where new labels start at N:

  • If a cluster has a single element, the label would be the index of the element in the original sequence.
  • If a cluster has more than one elements (two previous clusters were merged), the label would be N + i, where i is the index of the merge step that created it.

Consequently, the labels of the newly created clusters ranges from N to N + N - 1.

Constructors

new(observations : Int32)

Creates a new Dendrogram with the given number of original elements or observations.

Source

Instance methods

<<(step : Step) : self

Appends the given merge step. Raises ArgumentError if the dendrogram is already full (contains N - 1 steps).

Source
==(rhs : self) : Bool

Returns true if the merge steps are equal to rhs's steps, else false.

Source
add(c_i : Int32, c_j : Int32, distance : Float64) : Step

Creates and appends a merge step between clusters c_i and c_j with the given distance.

Source
flatten(height : Number) : Array(Array(Int32))

Returns flat clusters of the original observations obtained by cutting the dendrogram at height (cophenetic distance).

Source
flatten(*, count : Int) : Array(Array(Int32))

Returns count or fewer flat clusters of the original observations. Raises ArgumentError if count is negative or zero.

It internally computes the smallest height at which cutting the dendrogram would generate count or fewer clusters, and then flattens the dendrogram at the computed height.

Source
labels(height : Number) : Array(Int32)

Returns a mask of cluster labels obtained by cutting the dendrogram at height (cophenetic distance).

The returned array has one entry per original observation. The value at position i is the 0-based index of the cluster containing that observation. Cluster indexes are sorted by population, so cluster 0 is the most populated, cluster 1 the second, and so on.

Source
labels(*, count : Int) : Array(Int32)

Returns a mask of count or fewer cluster labels. Raises ArgumentError if count is negative or zero.

It internally computes the smallest height at which cutting the dendrogram would generate count or fewer clusters, and then labels the observations at the computed height.

The returned array has one entry per original observation. The value at position i is the 0-based index of the cluster containing that observation. Cluster indexes are sorted by population, so cluster 0 is the most populated, cluster 1 the second, and so on.

Source
observations

Number of the original elements or observations that were clustered.

Source
relabel(ordered : Bool = false) : self

Returns a new Dendrogram with relabeled clusters. If ordered is true, the dendrogram's steps will be sorted by the dissimilarities first.

Internally, it uses a UnionFind data structure for creating merge steps with the new cluster labels efficiently.

NOTE: Cluster labels will follow the SciPy convention, where new clusters start at N with N equal to the number of observations (see UnionFind).

Source
steps

Returns a view of the merge steps.

Source

Nested types