Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 998
Dgstah Avatar asked May 19 '19 07:05

Dgstah


Video Answer


1 Answers

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 (a.k.a. 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)
like image 150
ComplexGates Avatar answered Sep 30 '22 10:09

ComplexGates