我正在使用Python 3进行凝聚层次聚类实验,发现
然而,我需要能够对我的数据点进行
您可以使用此数据集测试您的代码。
scipy.cluster.hierarchy.cut_tree()
在某些输入链接矩阵中没有返回所请求的聚类数。因此,我现在知道cut_tree()函数存在错误(如此处所述)。然而,我需要能够对我的数据点进行
k
个不同标签的平坦聚类分配。您是否了解从任意输入链接矩阵Z
获得带有k
个标签的平坦聚类的算法?我的问题归结为:如何计算没有错误的cut_tree()
从头开始计算的内容?您可以使用此数据集测试您的代码。
from scipy.cluster.hierarchy import linkage, is_valid_linkage
from scipy.spatial.distance import pdist
## Load dataset
X = np.load("dataset.npy")
## Hierarchical clustering
dists = pdist(X)
Z = linkage(dists, method='centroid', metric='euclidean')
print(is_valid_linkage(Z))
## Now let's say we want the flat cluster assignement with 10 clusters.
# If cut_tree() was working we would do
from scipy.cluster.hierarchy import cut_tree
cut = cut_tree(Z, 10)
顺带提一下:另一种方法可能是使用rpy2的cutree()
代替scipy中的cut_tree()
,但我从未使用过它。你认为呢?