交换Python scipy的树状图/linkag的叶子

2024-05-14 20:57:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我为我的数据集生成了一个树状图,我不高兴某些级别的拆分是如何排序的。因此,我正在寻找一种方法来交换单个分支的两个分支(或叶)。在

如果我们看一下底部的代码和树状图,有两个标签11和{}与大集群的其余部分分开。我真的对此很不满意,并且希望带有11和{}的分支作为拆分的右分支,集群的其余部分作为左分支。显示的距离仍然相同,因此数据不会改变,只是美观。在

这能做到吗?怎么办?我特别赞成手动干预,因为在这种情况下,最优叶排序算法(按推测是不起作用)。在

import numpy as np

# random data set with two clusters
np.random.seed(65)  # for repeatability of this tutorial
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=[20,])
X = np.concatenate((a, b),)

# create linkage and plot dendrogram    
from scipy.cluster.hierarchy import dendrogram, linkage
Z = linkage(X, 'ward')

plt.figure(figsize=(15, 5))
plt.title('Hierarchical Clustering Dendrogram')
plt.xlabel('sample index')
plt.ylabel('distance')
dendrogram(
    Z,
    leaf_rotation=90.,  # rotates the x axis labels
    leaf_font_size=12.,  # font size for the x axis labels
)
plt.show()

enter image description here


Tags: 数据importforsize排序分支np集群

热门问题