List Highest Correlation Pairs from a Large Correlation Matrix in Pandas?

后端 未结 13 575
心在旅途
心在旅途 2020-12-22 17:45

How do you find the top correlations in a correlation matrix with Pandas? There are many answers on how to do this with R (Show correlations as an ordered list, not as a lar

13条回答
  •  清酒与你
    2020-12-22 18:11

    I was trying some of the solutions here but then I actually came up with my own one. I hope this might be useful for the next one so I share it here:

    def sort_correlation_matrix(correlation_matrix):
        cor = correlation_matrix.abs()
        top_col = cor[cor.columns[0]][1:]
        top_col = top_col.sort_values(ascending=False)
        ordered_columns = [cor.columns[0]] + top_col.index.tolist()
        return correlation_matrix[ordered_columns].reindex(ordered_columns)
    

提交回复
热议问题