Seaborn.clustermap:使用不同度量对行和列进行集群

2024-09-28 03:25:14 发布

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

我试图使用Seaborn函数之一生成clustermap。 目前,它允许我对行和列使用相同的度量(Pearson、Euclidean等),但仍然难以使用不同的度量,这与MATLAB的clustergram不同

基于this

To use different metrics (or methods) for rows and columns, you may construct each linkage matrix yourself and provide them as {row,col}_linkage.

但是有人知道怎么做吗


Tags: andto函数度量useseabornthis行和列
1条回答
网友
1楼 · 发布于 2024-09-28 03:25:14

可以使用^{}获得链接矩阵

以下是一个例子:

import matplotlib.pyplot as plt
import numpy as np
import scipy.cluster
import seaborn as sns

X = np.array([[1, 2,  1,  1],
              [2, 4,  1,  1],
              [3, 6, 23, 23],
              [4, 8, 23, 23],
              [5, 10, 8,  1]])

# Clear clusters on columns by correlation: (0,1), (2,3)
# Clear clusters on rows by distance: (0,1), (2,3)

fig, axs = plt.subplots(1,2)
Z_columns = scipy.cluster.hierarchy.linkage(np.transpose(X), metric='correlation')
scipy.cluster.hierarchy.dendrogram(Z_columns, ax=axs[0])
Z_rows = scipy.cluster.hierarchy.linkage(X, metric='euclidean')
scipy.cluster.hierarchy.dendrogram(Z_rows, orientation='left', ax=axs[1])
axs[0].set_title('Columns, correlation')
axs[1].set_title('Rows, euclidean')
plt.show()

Dendrograms resulting from clustering scipy.clustering rows and columns separately

# Use the computed linkage matrices in seaborn clustermap
g = sns.clustermap(X, row_linkage=Z_rows, col_linkage=Z_columns)
g.fig.suptitle('Correlation on columns, euclidean distance on rows')

enter image description here

相关问题 更多 >

    热门问题