def test_denrogram_children():
# temporary solution for
# https://stackoverflow.com/questions/40239956/node-indexing-in-hierarachical-clustering-dendrograms
import numpy as np
from scipy.cluster.hierarchy import dendrogram, linkage
from freediscovery.cluster import _DendrogramChildren
# generate two clusters: a with 10 points, b with 5:
np.random.seed(1)
a = np.random.multivariate_normal([10, 0], [[3, 1], [1, 4]],
size=[10, ])
b = np.random.multivariate_normal([0, 20], [[3, 1], [1, 4]],
size=[5, ])
X = np.concatenate((a, b),)
Z = linkage(X, 'ward')
# make distances between pairs of children uniform
# (re-scales the horizontal (distance) axis when plotting)
Z[:, 2] = np.arange(Z.shape[0])+1
ddata = dendrogram(Z, no_plot=True)
dc = _DendrogramChildren(ddata)
idx = 0
# check that we can compute children for all nodes
for i, d, c in zip(ddata['icoord'], ddata['dcoord'], ddata['color_list']):
node_children = dc.query(idx)
idx += 1
# last level node should encompass all samples
assert len(node_children) == X.shape[0]
assert_allclose(sorted(node_children), np.arange(X.shape[0]))
评论列表
文章目录