【问题标题】:How to sort an adjacency matrix如何对邻接矩阵进行排序
【发布时间】:2017-06-14 00:13:47
【问题描述】:

Python 中是否有一种方法可以“排序”邻接矩阵,以便更好地查看连接节点的不同集群?

我还有一些矩阵,但图案看起来像是随机分布在上面的。例如,在现实世界中,我知道我有 N 个独立的集群(两者之间没有连接)。

所以我希望邻接矩阵看起来有 N 个不同的模式。

这可以实现吗?

更新: 我昨天没有时间,但现在这里有一些细节:

import networkx as nx
import numpy as np
import matplotlib.pyplot as plt
G = nx.Graph()
S = {'7064', '7065', '7066', '7067', '7068', '7069', '7070', '7071', '7072', '7073', '7074', '7075', '7076', '7077', '7078', '7079', '7080'}
E = [('7064', '7065'),
('7067', '7068'),
('7067', '7076'),
('7067', '7077'),
('7067', '7078'),
('7067', '7079'),
('7067', '7080'),
('7067', '7081'),
('7068', '7076'),
('7068', '7077'),
('7068', '7078'),
('7068', '7080'),
('7068', '7081'),
('7069', '7075'),
('7070', '7072'),
('7070', '7074'),   
('7071', '7074'),
('7076', '7077'),
('7076', '7078'),
('7076', '7079'),
('7076', '7080'),
('7076', '7081'),
('7077', '7078'),
('7077', '7079'),
('7077', '7080'),
('7077', '7081'),
('7078', '7079'),
('7078', '7080'),
('7078', '7081'),
('7079', '7080'),
('7079', '7081'),
('7080', '7081')]
G.add_nodes_from(S)
G.add_edges_from(E)
adj_matrix = nx.adjacency_matrix(G).toarray()

plt.imshow(adj_matrix)

事实上,我只关心上三角形,因为它是一个对称矩阵(样本与自身)。

sub_graphs   = list(nx.connected_components(G))
nb_clusters  = len(sub_graphs) # total number of sub graphs, including isolated points, there is here 2 isolated points. All other are at least linked with min 1 other point. 

我希望邻接矩阵看起来像有 6 个“斑点”(其中两个将是两个孤立点的单个像素)。目前,它显示在其上三角形,12个视觉分离区域(它们只是视觉分离,实际上矩阵还可以,但我想重新排列它以更适合实际子图的数量)

【问题讨论】:

  • 邻接矩阵在你的代码中是如何表示的?你能展示一些你正在使用的代码吗?
  • 主帖已编辑。

标签: python networkx adjacency-matrix


【解决方案1】:

这似乎已经在 Hierarchical clustering of heatmap in python 中得到了回答,但是,没有接受的答案。

答案是对热图的行和列进行聚类以获得相似性(例如欧几里德距离),就像 seaborn 可以自动为您做的一样 http://seaborn.pydata.org/generated/seaborn.clustermap.html 。 邻接矩阵只是 seaborn 可以使用的常规矩阵的特例,其中元素为 0 或 1。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-07-03
    • 1970-01-01
    • 1970-01-01
    • 2020-11-04
    • 2017-12-18
    • 1970-01-01
    • 2020-08-15
    相关资源
    最近更新 更多