获取每个集群中的节点成员资格

Getting node membership in each cluster

我正在使用 Markov 聚类对 878 个节点的图进行聚类。实现基于这里提到的工作https://github.com/guyallard/markov_clustering

adj_matrix = nx.to_numpy_matrix(G)
res = mcl.run_mcl(adj_matrix)
clusters = mcl.get_clusters(res)

集群:

[(0,73, 88,173,223,235,390,405,409,435,442,456,481,501,573,615), 
(5, 38, 193, 403, 657, 679, 760, 791, 835, 854),
...
...
(7, 201, 640)]

看起来程序给我的是节点顺序,而不是我用来构建图形的原始标签,就像这样 780873982928735728293482978 等. 有没有办法把上面的结果映射到原来的节点标签上?

预期的结果是这样的

[(780873982, 928735728, 293482978), (293482932, 883482978), ...] 

提前致谢!

你可以使用字典:

# dummy nodes and graph
nodes = [np.random.randint(0,10031) for a in range(100)]
G = nx.Graph()
G.add_nodes_from(nodes)

# dummy clusters
clusters = [list(np.random.choice(range(len(G.nodes)), np.random.randint(1,5))) for a in range(8)]


# make mapping dictionary:
mapping = {a:b for a, b in zip(range(len(G.nodes)), G.nodes())}

# apply mapping_dictionary:

clusters_with_node_names = []

for c in clusters:
    clusters_with_node_names.append([mapping[a] for a in c])