【问题标题】:How to create a seaborn clustermap based on rows and extract row labels?如何基于行创建seaborn clustermap并提取行标签?
【发布时间】:2020-10-17 16:00:13
【问题描述】:

我有一个包含项目列表和相关值的数据框。哪种指标和方法最适合执行聚类?

  1. 我想仅基于行从列表中创建一个seaborn clustermap(树状图加热图),映射它(如图所示是代码),但是我怎样才能获得每个集群的项目列表或每个蛋白质及其簇信息。 (类似于Extract rows of clusters in hierarchical clustering using seaborn clustermap但仅基于行而不是列)

  2. 如何确定哪种“方法”和“指标”最适合我的数据?

data.csv 示例:

item,v1,v2,v3,v4,v5
A1,1,2,3,4,5
B1,2,4,6,8,10
C1,3,6,9,12,15
A1,2,3,4,5,6
B2,3,5,7,9,11
C2,4,7,10,13,16

我创建集群图的代码:

import pandas as pd
from scipy.cluster.hierarchy import linkage, dendrogram, fcluster
import matplotlib.pyplot as plt
import seaborn as sns
import scipy.cluster.hierarchy as sch

df = pd.read_csv('data.csv', index_col=0)
sns.clustermap(df, col_cluster=False, cmap="coolwarm", method='ward', metric='euclidean', figsize=(40,40))
plt.savefig('plot.pdf', dpi=300)

【问题讨论】:

  • 感谢您编辑问题
  • 集群是无监督的,这意味着有一些指标可以告诉你集群是否稳定或解释更多的差异,但最终,它是相当主观的。这取决于您的最终目标,您自己必须清楚这一点。您可以尝试不同的层次聚类方法,并使用 clustermap 中的row_linkage= 选项提供链接。
  • @StupidWolf 非常感谢,有什么方法可以检查哪种方法对我们的数据最有效(一种验证)。

标签: python seaborn cluster-analysis


【解决方案1】:

我只是一起破解了这个。这是你想要的吗?

import pandas as pd
import numpy as np
import seaborn as sns

cars = {'item': ['A1','B1','C1','A1','B1','C1'],
        'v1': [1.0,2.0,3.0,2.0,3.0,4.0],
        'v2': [2.0,4.0,6.0,3.0,5.0,7.0],
        'v3': [3.0,6.0,9.0,4.0,7.0,10.0],
        'v4': [4.0,8.0,12.0,5.0,9.0,13.0],
        'v5': [5.0,10.0,15.0,6.0,11.0,16.0]
        }

df = pd.DataFrame(cars)
df

heatmap_data = pd.pivot_table(df, values=['v1','v2','v3','v4','v5'], 
                              index=['item'])
heatmap_data.head()
sns.clustermap(heatmap_data)

df = df.drop(['item'], axis=1)
g = sns.clustermap(df)

此外,请查看下面的链接以获取有关此主题的更多信息。

https://seaborn.pydata.org/generated/seaborn.clustermap.html

https://kite.com/python/docs/seaborn.clustermap

【讨论】:

  • 非常感谢,但我正在寻找 seaborn custermap :)
  • 对不起,我看到了集群,我以为你指的是别的东西。我刚刚更新了我的答案。希望有帮助。我唯一不能真正理解的是您所做的“提取行”评论。
  • 感谢您的努力,但我的代码中已经完成了很多工作。我想要的是仅基于行的集群图(也在我的代码中使用“col_cluster = False”选项完成)。我想要的是我的项目的列表格式,它们在基于行进行聚类后属于哪个集群。
猜你喜欢
  • 2016-04-06
  • 1970-01-01
  • 2015-03-11
  • 2019-03-25
  • 2018-08-21
  • 2019-10-24
  • 1970-01-01
  • 1970-01-01
  • 2020-09-14
相关资源
最近更新 更多