Adr*_*ock 13 python numpy hierarchical-clustering scipy
我正在使用SciPy的分层凝聚聚类方法来聚类特征的amxn矩阵,但是在聚类完成之后,我似乎无法弄清楚如何从生成的聚类中获取质心.下面是我的代码:
Y = distance.pdist(features)
Z = hierarchy.linkage(Y, method = "average", metric = "euclidean")
T = hierarchy.fcluster(Z, 100, criterion = "maxclust")
Run Code Online (Sandbox Code Playgroud)
我正在使用我的特征矩阵,计算它们之间的欧氏距离,然后将它们传递给层次聚类方法.从那里,我正在创建平面群集,最多有100个群集
现在,基于平面簇T,我如何获得代表每个平面簇的1 xn质心?
你可以这样做(D=维数):
# Sum the vectors in each cluster
lens = {} # will contain the lengths for each cluster
centroids = {} # will contain the centroids of each cluster
for idx,clno in enumerate(T):
centroids.setdefault(clno,np.zeros(D))
centroids[clno] += features[idx,:]
lens.setdefault(clno,0)
lens[clno] += 1
# Divide by number of observations in each cluster to get the centroid
for clno in centroids:
centroids[clno] /= float(lens[clno])
Run Code Online (Sandbox Code Playgroud)
这将为您提供一个字典,其中簇号作为键,特定簇的质心作为值。
| 归档时间: |
|
| 查看次数: |
6042 次 |
| 最近记录: |