用seaborn-clustermap提取层次聚类中的聚类行

2024-06-02 02:14:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用的是seaborn.clustermap公司对我的数据进行群集。这样可以很好地在热图中很好地可视化集群。但是,现在我想提取分配给不同集群的所有行值。在

我的数据如下:

import pandas as pd

# load DataFrame 
df = pd.read_csv('expression_data.txt', sep='\t', index_col=0)

df 
^{pr2}$

然后我使用seaborn执行集群,如下所示:

fig = sns.clustermap(df)

将生成以下clustermap: enter image description here

对于这个例子,我可以手动解释属于每个集群的值(例如,TFRC和HSP90AA1集群)。不过,我计划在更大的数据集上做这些聚类分析。在

所以我的问题是:有人知道如何获取属于每个集群的行值吗?在

谢谢


Tags: 数据importpandasdf可视化asload集群
1条回答
网友
1楼 · 发布于 2024-06-02 02:14:10

使用scipy.cluster.hierarchy具有fcluster的模块允许群集检索:

import pandas as pd
import seaborn as sns
import scipy.cluster.hierarchy as sch

df = pd.read_csv('expression_data.txt', sep='\t', index_col=0)

# retrieve clusters using fcluster 
d = sch.distance.pdist(df)
L = sch.linkage(d, method='complete')
# 0.2 can be modified to retrieve more stringent or relaxed clusters
clusters = sch.fcluster(L, 0.2*d.max(), 'distance')

# clusters indicices correspond to incides of original df
for i,cluster in enumerate(clusters):
    print(df.index[i], cluster)

输出:

^{pr2}$

相关问题 更多 >