seaborn clustermap行重新排序

时间:2018-06-05 21:50:21

标签: pandas seaborn

我正在尝试使用以前的群集映射的行顺序重新排序seaborn群集映射,这样:

p = sns.clustermap(m1, cmap='RdBu_r', linewidths=0.5)
order = p.dendrogram_row.reordered_ind
ind = m2.index[order]
m2 = m2.reindex(ind)
sns.clustermap(m2, cmap='RdBu_r', linewidths=0.5)

当我打印时,m2被正确排序,但当我把它放在群集图中时,热图没有排序,你能帮帮我吗?

1 个答案:

答案 0 :(得分:0)

我认为,无论您按什么顺序输入,都可以根据聚类图绘制聚类图。这可能是创建自定义距离矩阵然后将其作为链接传递的解决方案。如果您从先前的聚类中获得了聚类标签,则只需创建一个距离矩阵,如果两个项目在同一聚类中,则距离矩阵为零。

import pandas as pd
import seaborn as sns
import numpy as np
from scipy.cluster.hierarchy import linkage
import scipy.spatial.distance as ssd

# create some random 3 dimensional data
data = pd.DataFrame(index=range(10), columns=["x", "y", "z"], 
data=np.random.random(size=(10,3)))

# create some random clustering with 3 different labels
cl_labels = [0,1,2]
cluster_labels = np.random.choice(cl_labels, size=data.shape[0])
prepared_clustering = pd.Series(index=data.index, data=cluster_labels)

# create a "distance" matrix
# distance for items in the same cluster will be zero, distance to elements outside the cluster are arbitrary (non-zero)
d = np.array([cluster_labels]*10)
distance_matrix = pd.DataFrame(index=data.index, columns=data.index, data = (d != d.T)).astype(int)

# create some colors to illustrate the clustering
colors = dict(zip(cl_labels, sns.hls_palette(len(cl_labels), l=0.5, s=0.8)))
row_colors = prepared_clustering.map(colors)

# plotting the clustermap
sns.clustermap(data,
           col_cluster=False,
           # don't turn off row_clustering, because it will do some default clustering/ordering
           row_cluster=True,
           # set the row_linkage to a custom linkage generated from the distance matrix
           # have no clue, but without the ssd.squareform a warning is displayed. the result is the same anyway
           #row_linkage=linkage(distance_matrix),
           row_linkage=linkage(ssd.squareform(distance_matrix)),
           # decorate with the rows
           row_colors=row_colors
          )

Clustermap with predefined clusters