I have a dataframe with over 280 features.
I ran correlation map to detect groups of features that are highly correlated:
Now, I want to divide the features to groups, such that each group will be a "red zone", meaning each group will have features that are all have correlation >0.5 with each other.
How can it be done?
Thanks
Disclaimer:
Theory
The problem is essentially a clique problem in graph theory, which means finding all the complete subgraphs in a given graph (with nodes > 2).
Imagine a graph that all the features are nodes and pairs of features satisfying
corr > 0.5
are edges. Then the task of finding all "groups" requested can simply translates into "finding all complete subgraphs in the graph".Code
The code uses networkx.algorithms.find_cliques for the search task, which implements Bron–Kerbosch algorithm according to the docs.
The code conprises of two parts. The first part extract the edges using
np.triu
(modified from this post) and the second part feeds the edge list intonetworkx
.The Coorelation Matrix
Feature [A,B,C] and [C,D,E] are closely correlated respectively, but not between [A,B] and [D,E].
Part 1
Part 2