在python中进行层次聚类后获得排序后的矩阵

时间:2019-04-16 13:25:35

标签: python-3.x hierarchical-clustering

我有一个2D二进制矩阵。我使用链接功能进行层次聚类。进行聚类后,我可以得到树状图。但是我想要的是根据该聚类排序的矩阵。 例如:

X = np.array([[0,0,0,0,0,1,0,0,0,1,0,0,0,0],[0,0,1,0,0,0,1,1,0,0,0,1,0,0],[0,0,0,0,0,1,0,0,1,0,1,0,0,1],
          [1,0,1,0,0,0,1,1,0,0,0,0,0,0],
          [0,1,1,0,0,0,1,1,0,0,0,0,0,0],
          [1,0,1,0,0,0,1,1,0,0,0,0,0,0]])

在进行分级聚类后,此numpy数组应如下所示:

res = np.array([[1,0,1,0,0,0,1,1,0,0,0,0,0,0],[1,0,1,0,0,0,1,1,0,0,0,0,0,0],[0,0,1,0,0,0,1,1,0,0,0,1,0,0],
          [0,1,1,0,0,0,1,1,0,0,0,0,0,0,
          [0,0,0,0,0,1,0,0,0,1,0,0,0,0],
          [0,0,0,0,0,1,0,0,0,1,0,0,0,0]])

到目前为止,我的代码如下:

import numpy as np
from scipy.cluster.hierarchy import dendrogram, linkage  
from matplotlib import pyplot as plt

linked = linkage(X, 'centroid')

labelList = range(1, 7)

plt.figure(figsize=(10, 7))  
dendrogram(linked,  
        orientation='top',
        labels=labelList,
        distance_sort='descending',
        show_leaf_counts=True)
plt.show()  

如何获得与树状图顺序相同的numpy数组。帮助将不胜感激。

0 个答案:

没有答案