简体   繁体   中英

Converting igraph to networkx for clustering

I have a python code which uses igraph library

import igraph
edge =  [(0, 6), (0, 8), (0, 115), (0, 124), (0, 289), (0, 359), (0, 363), (6, 60), (6, 115), (6, 128), (6, 129), (6, 130), (6, 131), (6, 359), (6, 529), (8, 9), (8, 17), (8, 115)]
G = igraph.Graph(edges=edge, directed=False)
G.vs['label'] = nodes
G.es["weight"] = weights
dendrogram = G.community_edge_betweenness()
clusters = dendrogram.as_clustering()
membership = clusters.membership
out = pd.Series(membership, index=nodes)

and I need to convert it to networkx library.

import networkx as nx
G = nx.Graph(edges)
dendrogram = nx.edge_betweenness_centrality(G)
clusters = nx.clustering(dendrogram)
membership = clusters.membership
out = pd.Series(membership, index=nodes)

However, dendrogram cannot be clustered in networkx library. Can someone help in replicating the igraph code to networkx clusters?

The problem is that "clustering" refers to two different things in network science. It either refers to the clustering coefficient (fraction of triangles in the ego graph; nx.clustering ) or it refers to a group of nodes (aka data clustering, network community, node partition, etc).

In this case you are using igraph community_edge_betweenness() to hierarchically cluster your nodes, and then cut the dendrogram to create a node partition through dendrogram.as_clustering() .

The equivalent in networkx would be to use girvan_newman :

from networkx.algorithms.community.centrality import girvan_newman

nx_dendrogram = girvan_newman(G)
move_down_dendrogram = itertools.takewhile(lambda c: len(c) <= 4, nx_dendrogram)
for c in move_down_dendrogram:
    clustering_list = c
print(clustering_list)

membership = [0] * G.number_of_nodes()
for ic, cset in enumerate(clustering_list):
    for n in cset:
        membership[n] = ic
out = pd.Series(membership, index=nodes)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM