python - 如何给 sns.clustermap 一个预先计算的距离矩阵?

标签 python matplotlib heatmap seaborn hierarchical-clustering

通常当我做树状图和热图时,我使用距离矩阵并做一堆 SciPy 的东西。我想尝试 SeabornSeaborn 想要我的数据为矩形(rows=samples、cols=attributes,而不是距离矩阵)?

我基本上想使用 seaborn 作为后端来计算我的树状图并将其添加到我的热图上。这可能吗?如果没有,这是否可以成为 future 的功能。

也许我可以调整一些参数,以便它可以采用距离矩阵而不是矩形矩阵?

用法如下:

seaborn.clustermap¶
seaborn.clustermap(data, pivot_kws=None, method='average', metric='euclidean',
 z_score=None, standard_scale=None, figsize=None, cbar_kws=None, row_cluster=True,
 col_cluster=True, row_linkage=None, col_linkage=None, row_colors=None,
 col_colors=None, mask=None, **kwargs)

我的代码如下:

from sklearn.datasets import load_iris
iris = load_iris()
X, y = iris.data, iris.target
DF = pd.DataFrame(X, index = ["iris_%d" % (i) for i in range(X.shape[0])], columns = iris.feature_names)

enter image description here

我不认为我的方法在下面是正确的,因为我给它一个预先计算的距离矩阵,而不是它要求的矩形数据矩阵。没有关于如何将相关/距离矩阵与 clustermap 一起使用的示例,但有 https://stanford.edu/~mwaskom/software/seaborn/examples/network_correlations.html 的示例。但是排序不是用普通的 sns.heatmap 函数聚集的。

DF_corr = DF.T.corr()
DF_dism = 1 - DF_corr
sns.clustermap(DF_dism)

enter image description here

最佳答案

您可以将预先计算的距离矩阵作为链接传递给 clustermap():

import pandas as pd, seaborn as sns
import scipy.spatial as sp, scipy.cluster.hierarchy as hc
from sklearn.datasets import load_iris
sns.set(font="monospace")

iris = load_iris()
X, y = iris.data, iris.target
DF = pd.DataFrame(X, index = ["iris_%d" % (i) for i in range(X.shape[0])], columns = iris.feature_names)

DF_corr = DF.T.corr()
DF_dism = 1 - DF_corr   # distance matrix
linkage = hc.linkage(sp.distance.squareform(DF_dism), method='average')
sns.clustermap(DF_dism, row_linkage=linkage, col_linkage=linkage)

对于 clustermap(distance_matrix)(即,没有传递链接),链接是根据距离矩阵中的行和列的成对距离在内部计算的(有关完整详细信息,请参阅下面的注释)直接使用距离矩阵的元素(正确的解决方案)。结果,输出与问题中的输出有些不同: clustermap

注意:如果没有将 row_linkage 传递给 clustermap(),则行链接在内部通过将每一行视为一个“点”(观察值)并计算成对来确定点之间的距离。所以行树状图反射(reflect)了行的相似性。类似于 col_linkage,其中每一列都被视为一个点。这个解释应该被添加到 docs .这里修改了文档的第一个示例以明确内部链接计算:

import seaborn as sns; sns.set()
import scipy.spatial as sp, scipy.cluster.hierarchy as hc
flights = sns.load_dataset("flights")
flights = flights.pivot("month", "year", "passengers")
row_linkage, col_linkage = (hc.linkage(sp.distance.pdist(x), method='average')
  for x in (flights.values, flights.values.T))
g = sns.clustermap(flights, row_linkage=row_linkage, col_linkage=col_linkage) 
  # note: this produces the same plot as "sns.clustermap(flights)", where
  #  clustermap() calculates the row and column linkages internally

关于python - 如何给 sns.clustermap 一个预先计算的距离矩阵?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38705359/

相关文章:

python - matplotlib 如何填充_between 步骤函数

matplotlib - 在 matplotlib 中,有没有办法异步弹出一个图形?

python - Pandas 如何访问多索引 DataFrame 以选择值来制作热图..!

python - pcolormesh 正在发布一张空白 map ?

javascript - Jupyter notebook 用不同的颜色为不同的括号着色

python - 使用 SQLAlchemy 获取第一个和最后一个元素

python - 数字作为字符串的 seaborn 色调

python - Python 中的空间数据热图

python - 将按行排序的数据帧映射到原始列标签(Pandas)

python - 计算 3D 球面掩膜的直径线