替代 scipy.cluster.hierarchy.cut_tree()
Alternative to scipy.cluster.hierarchy.cut_tree()
我在 Python 3 中进行了凝聚层次聚类实验,我发现 scipy.cluster.hierarchy.cut_tree()
没有为某些输入链接矩阵返回请求的聚类数。所以,现在我知道 cut_tree() function (as described here).
中有一个错误
但是,我需要能够通过为我的数据点分配 k
不同标签来获得平面聚类。您知道从任意输入链接矩阵 Z
获得具有 k
标签的平面聚类的算法吗?我的问题归结为:我如何计算 cut_tree()
正在从头开始计算而没有错误?
您可以使用 this dataset 测试您的代码。
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()
,但我从未使用过它。你怎么看?
获得k
扁平簇的一种方法是使用scipy.cluster.hierarchy.fcluster
和criterion='maxclust'
:
from scipy.cluster.hierarchy import fcluster
clust = fcluster(Z, k, criterion='maxclust')
我在 Python 3 中进行了凝聚层次聚类实验,我发现 scipy.cluster.hierarchy.cut_tree()
没有为某些输入链接矩阵返回请求的聚类数。所以,现在我知道 cut_tree() function (as described here).
但是,我需要能够通过为我的数据点分配 k
不同标签来获得平面聚类。您知道从任意输入链接矩阵 Z
获得具有 k
标签的平面聚类的算法吗?我的问题归结为:我如何计算 cut_tree()
正在从头开始计算而没有错误?
您可以使用 this dataset 测试您的代码。
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()
,但我从未使用过它。你怎么看?
获得k
扁平簇的一种方法是使用scipy.cluster.hierarchy.fcluster
和criterion='maxclust'
:
from scipy.cluster.hierarchy import fcluster
clust = fcluster(Z, k, criterion='maxclust')