You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何更改correlation的排序方式?当前分别设为auto和ordinal alpha

修改Correlation排序方式的解决方案

问题1:从auto排序改为其他设置

如果是在seaborn.clustermap中使用默认的auto聚类排序(自动判断是否启用聚类),可以通过以下方式修改:

  • 完全关闭排序,保留原始变量顺序:

    import seaborn as sns
    import pandas as pd
    
    # 计算相关系数矩阵
    corr_matrix = df.corr()
    # 关闭行/列聚类排序
    sns.clustermap(corr_matrix, row_cluster=False, col_cluster=False)
    
  • 自定义排序逻辑(比如按目标变量的相关系数绝对值排序):

    # 按"target"列的相关系数绝对值从大到小排序行和列
    sorted_order = corr_matrix["target"].abs().sort_values(ascending=False).index
    sorted_corr = corr_matrix.loc[sorted_order, sorted_order]
    sns.clustermap(sorted_corr)
    

问题2:从ordinal alpha(字母顺序)排序改为其他设置

若当前是按变量名称的字母顺序排序,可通过以下方式调整:

  • 启用聚类排序(基于相关系数的层次聚类):

    sns.clustermap(corr_matrix, row_cluster=True, col_cluster=True)
    
  • 按相关系数大小排序(比如按对角线值排序):

    # 按对角线相关系数降序排列
    sorted_idx = corr_matrix.diagonal().sort_values(ascending=False).index
    sorted_corr = corr_matrix.loc[sorted_idx, sorted_idx]
    sns.heatmap(sorted_corr)
    
  • 手动指定自定义排序顺序:

    # 自定义变量顺序
    custom_var_order = ["feature_a", "feature_c", "feature_b", "feature_d"]
    sorted_corr = corr_matrix.loc[custom_var_order, custom_var_order]
    sns.heatmap(sorted_corr)
    

内容的提问来源于stack exchange,提问作者DatDepzai

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.29 05:24:58