Python scipy的树状图/链接的交换叶子

问题描述 投票:5回答:1

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

[如果我们查看底部的代码和树状图,则有两个标签1125与大集群的其余部分分开。我对此真的感到不满意,希望1125的分支成为拆分的右分支,而集群的其余部分成为左分支。显示的距离仍然是相同的,因此仅出于美观目的,不会更改数据。

可以这样做吗?如何?我专用于手动干预,因为在这种情况下,最佳叶子排序算法supposedly不起作用。

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

python matplotlib scipy hierarchical-clustering dendrogram
1个回答
1
投票
import numpy as np import matplotlib.pyplot as plt # 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', optimal_ordering = True) 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 distance_sort=False, show_leaf_counts=True, count_sort=False ) plt.show()

result of using optimal_ordering in linkage

© www.soinside.com 2019 - 2024. All rights reserved.