交换 Python scipy 的 dendrogram/linkage 的叶子
Swap leafs of Python scipy's dendrogram/linkage
我为我的数据集生成了树状图,但我对某些级别的拆分排序方式不满意。因此,我正在寻找一种方法来交换单个拆分的两个分支(或叶子)。
如果我们查看底部的代码和树状图,有两个标签 11
和 25
从大集群的其余部分分离出来。我对此真的很不高兴,并且希望 11
和 25
的分支是拆分的右分支,而集群的其余部分是左分支。显示的距离仍然相同,因此数据不会改变,只是美观。
这能做到吗?如何?我是专门针对人工干预的,因为最佳叶子排序算法据说在这种情况下不起作用
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()
我有一个类似的问题,并通过在链接中使用 optimal_ordering 选项得到解决。我附上了你的案例的代码和结果,这可能不是你喜欢的,但对我来说似乎有了很大的改进。
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
我为我的数据集生成了树状图,但我对某些级别的拆分排序方式不满意。因此,我正在寻找一种方法来交换单个拆分的两个分支(或叶子)。
如果我们查看底部的代码和树状图,有两个标签 11
和 25
从大集群的其余部分分离出来。我对此真的很不高兴,并且希望 11
和 25
的分支是拆分的右分支,而集群的其余部分是左分支。显示的距离仍然相同,因此数据不会改变,只是美观。
这能做到吗?如何?我是专门针对人工干预的,因为最佳叶子排序算法据说在这种情况下不起作用
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()
我有一个类似的问题,并通过在链接中使用 optimal_ordering 选项得到解决。我附上了你的案例的代码和结果,这可能不是你喜欢的,但对我来说似乎有了很大的改进。
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