【问题标题】:Reordering the high-level clusters from seaborn clustermap results从 seaborn clustermap 结果重新排序高级集群
【发布时间】:2019-10-09 01:58:58
【问题描述】:

有没有办法通过脚本从下图中的ab?我正在使用seaborn.clustermap() 到达a(即保留行的顺序。但是,列顺序仅在第二高级别更改)。

我想知道是否可以使用seaborn.clustermap() 返回的seaborn.matrix.ClusterGrid,对其进行修改并绘制修改后的结果。 b P.S. 我问这个的原因是顺序有一个含义(首先是蓝色,接下来是绿色,最后是红色)。

更新: 这是一个生成情况的小数据集:

df = pd.DataFrame([[1, 1.1, 0.9, 1.9, 2, 2.1, 2.8, 3, 3.1], 
                   [1.8, 2, 2.1, 0.7, 1, 1.1, 2.7, 3, 3.3]],
              columns = ['d1', 'd2', 'd3', 
                         'l3', 'l2', 'l1', 
                         'b1', 'b2', 'b3'],
              index = ['p1', 'p2'])

cg = sns.clustermap(df); ## returns a ClusterGrid

输出是这样的:

我们可以认为以b 开头的列是早餐,l 是午餐,d 是晚餐。现在,订单是breakfast -> dinner -> lunch。我想去breakfast -> lunch -> dinner

【问题讨论】:

  • 你能用代码创建一个小数据集来生成clustermap吗?
  • @ScottBoston:这是一个相当大的数据集,因为它是可见的,所以我已经截断了行。恐怕行数较少,我无法重现目前显示的分离。但我会尽力而为。我会在成功创建问题后立即更新问题。
  • @ScottBoston 我现在添加了一些数据。希望对您有所帮助。
  • 您要保留b1,b2,b3,l1,l2,l3,d1,d2,d3 的顺序还是只保留一般顺序b,l,d 并且其中的每个子类都由算法排序?
  • @Dataman 是的,这一小组数据很有帮助。但是,我无法确定实现您想要的结果的方法。

标签: python matplotlib seaborn hierarchical-clustering


【解决方案1】:

这就是我解决问题的方法。它有效,但可能没有人们希望的那么优雅!

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

# set the desired order of groups eg: breakfast, lunch, dinner
groups = ['b', 'l', 'd'] 

# reorder indexes/indices besed on the desired order
new_order = []
for group in groups:
    indexes = cg.data2d.columns.str.startswith(group)
    indexes_locs = np.where(indexes)[0].tolist()
    new_order += indexes_locs
    
## reorder df based on the new order
ordered_df = cg.data2d.iloc[:, new_order]

## Run clustermap on the reordered dataframe by disabling 
## the clustering for both rows and columns
ocg = sns.clustermap(ordered_df, 
                     row_cluster=False, 
                     col_cluster=False,
                    );

## draw dendrogram x-axis
axx = ocg.ax_col_dendrogram.axes
axx.clear()

with plt.rc_context({'lines.linewidth': 0.5}):
    
    link = cg.dendrogram_col.linkage ## extract the linkage information

    ## manualy inspect the linkage and determine the new desired order
    link[[4, 2]] = link[[2, 4]]  ## swaping the two groups of higher hierarchy
    
    ## draw the the dendrogram on the x-axis
    dendrogram(link, 
           color_threshold=0, 
           ax=axx,
           truncate_mode='lastp',
           orientation='top',
           link_color_func=lambda x: 'k'
          );

axx.set_yticklabels(['']*len(axx.get_yticklabels()))
axx.tick_params(color='w')
    
## draw dendrogram y-axis (no chage here)
axy = ocg.ax_row_dendrogram.axes
axy.clear()

with plt.rc_context({'lines.linewidth': 0.5}):
    
    ## draw the the dendrogram on the y-axis
    dendrogram(cg.dendrogram_row.linkage, 
           color_threshold=0, 
           ax=axy,
           truncate_mode='lastp',
           orientation='left',
           link_color_func=lambda x: 'k',
          );

axy.set_xticklabels(['']*len(axy.get_yticklabels()))
axy.tick_params(color='w')
# axy.invert_yaxis() # we might need to invert y-axis

输出如下所示:

【讨论】:

    猜你喜欢
    • 2015-03-11
    • 1970-01-01
    • 2016-11-06
    • 1970-01-01
    • 2019-03-25
    • 2016-04-11
    • 2016-04-06
    • 2015-12-28
    • 2016-11-11
    相关资源
    最近更新 更多