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

问题描述 投票:1回答:1

我正在使用马尔科夫聚类来聚拢一个878个节点的图。其实现是基于这里提到的工作 https:/github.comguyallardmarkov_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)]

看起来程序给我的节点顺序不是我用来建立图的原始标签,它是这样的: 780873982, 928735728, 293482978等,有没有办法将上述结果映射到原始节点标签上?

预计结果会是这样的

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

先谢谢你!

python cluster-analysis networkx markov
1个回答
1
投票

你可以使用一个字典。

# 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])
© www.soinside.com 2019 - 2024. All rights reserved.