免责声明:
- 此解决方案未解决可视化问题。仅找到组。
- 已知该解决方案是 NP 难的,因此请注意效率问题。
理论
这个问题本质上是图论中的clique problem,这意味着找到给定图中的所有完整子图(节点> 2)。
想象一个图,所有特征都是节点,满足corr > 0.5 的特征对是边。那么查找所有请求的“组”的任务可以简单地转化为“查找图中所有完整的子图”。
代码
代码使用networkx.algorithms.find_cliques作为搜索任务,根据文档实现Bron–Kerbosch algorithm。
代码由两部分组成。第一部分使用np.triu(修改自this post)提取边缘,第二部分将边缘列表提供给networkx。
相关矩阵
特征 [A,B,C] 和 [C,D,E] 分别密切相关,但在 [A,B] 和 [D,E] 之间不相关。
np.random.seed(111) # reproducibility
x = np.random.normal(0, 1, 100)
y = np.random.normal(0, 1, 100)
a = x
b = x + np.random.normal(0, .5, 100)
c = x + y
d = y + np.random.normal(0, .5, 100)
e = y + np.random.normal(0, .5, 100)
df = pd.DataFrame({"A":a, "B":b, "C":c, "D":d, "E":e})
corr = df.corr()
corr
Out[24]:
A B C D E
A 1.000000 0.893366 0.677333 -0.078369 -0.090510
B 0.893366 1.000000 0.577459 -0.072025 -0.079855
C 0.677333 0.577459 1.000000 0.587695 0.579891
D -0.078369 -0.072025 0.587695 1.000000 0.777803
E -0.090510 -0.079855 0.579891 0.777803 1.000000
第 1 部分
# keep only upper triangle elements (excluding diagonal elements)
mask_keep = np.triu(np.ones(corr.shape), k=1).astype('bool').reshape(corr.size)
# melt (unpivot) the dataframe and apply mask
sr = corr.stack()[mask_keep]
# filter and get names
edges = sr[sr > 0.5].reset_index().values[:, :2]
edges
Out[25]:
array([['A', 'B'],
['A', 'C'],
['B', 'C'],
['C', 'D'],
['C', 'E'],
['D', 'E']], dtype=object)
第 2 部分
import networkx as nx
g = nx.from_edgelist(edges)
ls_cliques = []
for clique in nx.algorithms.find_cliques(g):
ls_cliques.append(clique)
# result
ls_cliques
Out[26]: [['C', 'A', 'B'], ['C', 'D', 'E']]