Cluster a Correlation Matrix (in python) – Wil Yegelwel
correlation-matrixclusteringscipyseabornpython
Abstraction: Reordering correlation matrix columns/rows using hierarchical clustering
Key points:
cluster_corr()function rearranges a pandas DataFrame or numpy ndarray correlation matrix so highly correlated variables appear adjacent- Uses
scipy.cluster.hierarchy: computes pairwise distances viadistance.pdist, builds linkage withmethod='complete', cuts atpairwise_distances.max()/2 np.argsort(idx_to_cluster_array)produces the sorted index used to reorder both rows and columns- Works as drop-in preprocessing for
seaborn.heatmap(cluster_corr(df.corr()))to produce interpretable correlation heatmaps
Connections: Scipy · Hierarchical Clustering · Correlation Analysis
Source: https://wil.yegelwel.com/cluster-correlation-matrix/