使用预先计算的距离相关矩阵的Seaborn簇图问题

问题描述 投票:1回答:1

我是:

然后我要:

  • ((B)使用带有我自己的数据的几个DataFrame执行相同的操作。

我按照文档示例中的说明直接向sns.clustermap提供了距离相关性,因为我对热图中的结构感兴趣,而不是像this SO answer中那样使用距离相关性矩阵来计算链接。 , 例如。我使用此excellent SO answer的代码修改来创建距离校正矩阵。

  • ((A)这里没有问题
  • 我执行时:

distcorr = lambda column1, column2: dcor.distance_correlation(column1, column2)
dcor_df= df.apply(lambda col1: df.apply(lambda col2: distcorr(col1, col2)))
sns.clustermap(dcor_df, cmap="mako",
               row_colors=network_colors, col_colors=network_colors,
               linewidths=.75, figsize=(13, 13))

我得到了我期望的结果:

enter image description here

  • ((B)我在这里确实遇到了问题],当我移至自己的数据时
  • [对于某些背景:我有两个带有两个标记为A, B, ..., P的变量的DataFrames。变量是相同的(相同的度量,相同的单位),但是度量是在空间上分开的两个位置收集的,因此我的目标是分别运行分析,以查看变量是否以相似的方式关联(即具有相似的结构)在热图上)。

    来自第一个位置的数据存储在here中。

我执行以下代码:

df_1 = pd.read_csv('df_1.csv')
pd.options.display.float_format = '{:,.3f}'.format
distcorr = lambda column1, column2: dcor.distance_correlation(column1, column2)
rslt_1 = df_1.apply(lambda col1: df_1.apply(lambda col2: distcorr(col1, col2)))
rslt_1

并且我得到了预期的(正方形,对称的)距离相关矩阵:我可以用enter image description here绘制为sns.heatmap

h=sns.heatmap(rslt_1, cmap="mako",  vmin=0, vmax=1, 
              xticklabels=True, yticklabels=True, square=True) 
fig = plt.gcf()
fig.set_size_inches(14, 10)

enter image description here

但是,当我尝试通过以下方式将距离相关矩阵传递给“ sns.clustermap”时:

s=sns.clustermap(rslt_1, cmap="mako", standard_scale=1, linewidths=0) 
fig = plt.gcf()
fig.set_size_inches(10, 10);

我明白了:

enter image description here

这对我来说很奇怪,因为我期望行和列的排序与上述修改后的文档示例中的排序相同。除非我完全不出去吃午饭,或者想念或误解一些重要的东西。

如果我像这样通过metric='correlation'

s=sns.clustermap(rslt_1, cmap="mako", metric='correlation', 
             standard_scale=1, linewidths=0) 
fig = plt.gcf()
fig.set_size_inches(10, 10);

我得到的结果与对角线对称,正如我所期望的,如果我“眼球”这些簇,那么在与表格形式的矩阵进行比较时,它们对我来说更有意义:enter image description here

使用第二个位置的数据存储here,无论是否通过metric='correlation',我都会得到合理的结果(虽然不完全相同,但相当相似):final

我无法解释第一种情况下的行为。 我想念什么吗?>?

谢谢。

PS

我在Windows 10 PC上。一些信息:

enter image description hereenter image description here

我:(A)运行Seaborn文档中的示例,发现热图数据中的结构,但使用dcor库中的距离关联,而不是pandas.DataFrame.corr,其中...

从集群图中删除标准比例参数。

s=sns.clustermap(rslt_1, cmap="mako", metric='correlation', linewidths=0) 
python seaborn heatmap correlation hierarchical-clustering
1个回答
0
投票

从集群图中删除标准比例参数。

s=sns.clustermap(rslt_1, cmap="mako", metric='correlation', linewidths=0) 
© www.soinside.com 2019 - 2024. All rights reserved.