将距离矩阵传递给 seaborn clustermap
Pass distance matrix to seaborn clustermap
我想将自己的距离矩阵(行链接)传递给seaborn clustermap。
已有一些赞
Use Distance Matrix in scipy.cluster.hierarchy.linkage()?
但他们都指向
将聚类指标和方法作为参数。
scipy.cluster.hierarchy.linkage(y, method='single',
metric='euclidean', optimal_ordering=False)
The input y may be either a 1d condensed distance matrix or a 2d array
of observation vectors
我不明白的是:
My distance matrix is already based on a certain metric and method,
why would I want to recalculate this in scipy hierarchy linkage ?
Is there an option where it purely uses my distances and creates the linkages?
为了后代,这里有一个完整的方法,因为评论中的@WarrenWeckesser 和链接答案中的@SibbsGambling 省略了一些细节。
假设 distMatrix
是您的距离矩阵(不必是欧几里得),行 i
和列 j
中的条目代表 [=12] 之间的距离=]th 和 j
th 对象。那么:
# import packages
from scipy.cluster import hierarchy
import scipy.spatial.distance as ssd
import seaborn as sns
# define distance array as in linked answer
distArray = ssd.squareform(distMatrix)
# define linkage object
distLinkage = hierarchy.linkage(distArray)
# make clustermap
sns.clustermap(distMatrix, row_linkage=distLinkage, col_linkage=distLinkage)
请注意,在创建 clustermap
时,您仍然必须引用原始矩阵。如果您想使用不同的聚类方法,例如 method='ward'
,请在定义 distLinkage
时包含该选项。
我想将自己的距离矩阵(行链接)传递给seaborn clustermap。
已有一些赞
Use Distance Matrix in scipy.cluster.hierarchy.linkage()?
但他们都指向
将聚类指标和方法作为参数。
scipy.cluster.hierarchy.linkage(y, method='single', metric='euclidean', optimal_ordering=False)
The input y may be either a 1d condensed distance matrix or a 2d array of observation vectors
我不明白的是:
My distance matrix is already based on a certain metric and method, why would I want to recalculate this in scipy hierarchy linkage ?
Is there an option where it purely uses my distances and creates the linkages?
为了后代,这里有一个完整的方法,因为评论中的@WarrenWeckesser 和链接答案中的@SibbsGambling 省略了一些细节。
假设 distMatrix
是您的距离矩阵(不必是欧几里得),行 i
和列 j
中的条目代表 [=12] 之间的距离=]th 和 j
th 对象。那么:
# import packages
from scipy.cluster import hierarchy
import scipy.spatial.distance as ssd
import seaborn as sns
# define distance array as in linked answer
distArray = ssd.squareform(distMatrix)
# define linkage object
distLinkage = hierarchy.linkage(distArray)
# make clustermap
sns.clustermap(distMatrix, row_linkage=distLinkage, col_linkage=distLinkage)
请注意,在创建 clustermap
时,您仍然必须引用原始矩阵。如果您想使用不同的聚类方法,例如 method='ward'
,请在定义 distLinkage
时包含该选项。