Skip to content

hclust

hclust(
    data: PlotData,
    *,
    labels: str | None = None,
    axis: Literal["rows", "columns"] = "rows",
    method: Literal = "complete",
    distance: Literal = "euclidean",
) -> ClusterTree

Cluster the rows (or columns) of a table hierarchically, as R's hclust.

Merges, heights and leaf order match R hclust(dist(x), method) (or hclust(as.dist(1 - cor(t(x))), method) for correlation), ties included. One exception: rows whose correlation is 1 up to rounding can tie differently from R, as 1 - r may come out as 0 on one side and 2e-16 on the other.

Parameters:

  • data (DataFrame or dict) –

    A wide table: every column but labels holds numbers, without missing values.

  • labels (str, default: None ) –

    Column naming the rows (the leaves when clustering rows). Default: rows are named "1", "2", ...

  • axis ((rows, columns), default: "rows" ) –

    Cluster the rows, or the numeric columns (leaves named by column).

  • method ((complete, average, single, D2), default: "complete" ) –

    Linkage.

  • distance ((euclidean, correlation), default: "euclidean" ) –

    Dissimilarity: straight-line distance, or 1 - r with Pearson's r.

Returns:

Raises:

  • SpecError –

    If axis, method or distance is unknown.

  • DataError –

    If a column is not numeric, a value is missing, a label repeats, or (for correlation) an item has no variance.

Examples:

>>> from ggrust import hclust
>>> frame = {"gene": ["CD3E", "CD8A", "MS4A1"], "t": [5.0, 4.5, 0.1], "b": [0.2, 0.1, 6.0]}
>>> hclust(frame, labels="gene", method="average").ordered_labels
('MS4A1', 'CD3E', 'CD8A')