ecotyper.rcompat.cluster

Hierarchical clustering and silhouettes exactly as in R.

Line-by-line ports of R’s stats Fortran routines HCLUST/HCASS2 (src/library/stats/src/hclust.f), cutree (hclust-utils.c), cophenetic.default, dist(method = "euclidean") (distance.c) and cluster::silhouette.default (sildist.c). The same loop order and strict comparisons are kept so that ties are broken identically.

Functions

cophenetic(tree)

cophenetic(tree) as a dist vector (as.dist order).

cutree(tree, k)

cutree(tree, k) → 1-based cluster ids in observation order.

dist_euclidean_rows(X)

dist(X) (Euclidean distances between rows of X).

hclust(d, n[, method, labels])

hclust(d, method) where d is a dist vector (as.dist order).

silhouette_avg_width(clustering, dmatrix)

summary(silhouette(x, dmatrix = D))$avg.width (NaN when R returns NA).

silhouette_avg_width_dist(clustering, d)

summary(silhouette(x, d))$avg.width for a dist vector.

silhouette_widths(clustering, d, ismat)

cluster::silhouette.default: widths in observation order, or None (NA).

Classes

HClust(merge, height, order[, labels, method])

class ecotyper.rcompat.cluster.HClust(merge: 'np.ndarray', height: 'np.ndarray', order: 'np.ndarray', labels: 'list | None' = None, method: 'str' = 'complete')[source]

Bases: object

Parameters:
merge: ndarray
height: ndarray
order: ndarray
labels: list | None = None
method: str = 'complete'
ecotyper.rcompat.cluster.hclust(d, n, method='complete', labels=None)[source]

hclust(d, method) where d is a dist vector (as.dist order).

Parameters:
Return type:

HClust

ecotyper.rcompat.cluster.cutree(tree, k)[source]

cutree(tree, k) → 1-based cluster ids in observation order.

Parameters:
Return type:

ndarray

ecotyper.rcompat.cluster.cophenetic(tree)[source]

cophenetic(tree) as a dist vector (as.dist order).

Parameters:

tree (HClust)

Return type:

ndarray

ecotyper.rcompat.cluster.dist_euclidean_rows(X)[source]

dist(X) (Euclidean distances between rows of X).

Parameters:

X (ndarray)

Return type:

ndarray

ecotyper.rcompat.cluster.silhouette_widths(clustering, d, ismat)[source]

cluster::silhouette.default: widths in observation order, or None (NA).

Parameters:

ismat (bool)

Return type:

ndarray | None

ecotyper.rcompat.cluster.silhouette_avg_width(clustering, dmatrix)[source]

summary(silhouette(x, dmatrix = D))$avg.width (NaN when R returns NA).

Parameters:

dmatrix (ndarray)

Return type:

float

ecotyper.rcompat.cluster.silhouette_avg_width_dist(clustering, d)[source]

summary(silhouette(x, d))$avg.width for a dist vector.

Parameters:

d (ndarray)

Return type:

float