【发布时间】:2019-09-20 20:47:02
【问题描述】:
在给定平方距离矩阵的情况下,我正在尝试将平坦的单链接集群分配给由编辑距离 scipy.cluster.hierarchy.fclusterdata() 和 criterion='distance' 可能是一种方法,但它并没有完全返回我对这个玩具示例所期望的集群。
具体来说,在下面的 4x4 距离矩阵示例中,我希望 clusters_50(它使用 t=50)创建 2 个集群,实际上它找到了 3 个。我认为问题是 fclusterdata() 不期望一个距离矩阵,但 fcluster() 似乎也没有做我想要的。
我也查看了sklearn.cluster.AgglomerativeClustering,但这需要指定n_clusters,并且我想根据需要创建尽可能多的集群,直到满足我指定的距离阈值。
我看到有一个 当前未合并 scikit-learn 对这个确切功能的拉取请求:https://github.com/scikit-learn/scikit-learn/pull/9069
谁能指出我正确的方向?使用绝对距离阈值标准进行聚类似乎是一个常见的用例。
import pandas as pd
from scipy.cluster.hierarchy import fclusterdata
cols = ['a', 'b', 'c', 'd']
df = pd.DataFrame([{'a': 0, 'b': 29467, 'c': 35, 'd': 13},
{'a': 29467, 'b': 0, 'c': 29468, 'd': 29470},
{'a': 35, 'b': 29468, 'c': 0, 'd': 38},
{'a': 13, 'b': 29470, 'c': 38, 'd': 0}],
index=cols)
clusters_20 = fclusterdata(df.values, t=20, criterion='distance')
clusters_50 = fclusterdata(df.values, t=50, criterion='distance')
clusters_100 = fclusterdata(df.values, t=100, criterion='distance')
names_clusters_20 = {n: c for n, c in zip(cols, clusters_20)}
names_clusters_50 = {n: c for n, c in zip(cols, clusters_50)}
names_clusters_100 = {n: c for n, c in zip(cols, clusters_100)}
names_clusters_20 # Expecting 3 clusters, finds 3
>>> {'a': 1, 'b': 3, 'c': 2, 'd': 1}
names_clusters_50 # Expecting 2 clusters, finds 3
>>> {'a': 1, 'b': 3, 'c': 2, 'd': 1}
names_clusters_100 # Expecting 2 clusters, finds 2
>>> {'a': 1, 'b': 2, 'c': 1, 'd': 1}
【问题讨论】:
标签: python python-3.x scipy cluster-analysis bioinformatics