Process1
Process1

Reputation: 488

Python modularity statistic using louvain package

I am trying to do batch wise run of python's louvain modularity partition of my network atleast a 100 times. I need to save the partitions and save the modularity score after the partition.Is there any way to get both of them? I have tried the documentation and example and it only returns the partitions but no modularity statistic. Please point me to the right direction and I will be extremely grateful for your help.

Upvotes: 0

Views: 1446

Answers (2)

seralouk
seralouk

Reputation: 33147

Here is how to estimate the modularity Q using louvain algorithm in igraph.

Important: If you have weighted adjacency matrices, DO NOT FORGET the weights=graph.es['weight'] argument in the below functions!

import numpy as np
from igraph import *

W = np.random.rand(10,10)
np.fill_diagonal(W,0.0)

graph = Graph.Weighted_Adjacency(W.tolist(), mode=ADJ_UNDIRECTED, attr="weight", loops=False)

louvain_partition = graph.community_multilevel(weights=graph.es['weight'], return_levels=False)

modularity1 = graph.modularity(louvain_partition, weights=graph.es['weight'])

print("The modularity Q based on igraph is {}".format(modularity1))

The modularity Q based on igraph is 0.02543865641866777

Upvotes: 0

Paul Brodersen
Paul Brodersen

Reputation: 13041

You can compute the modularity score using igraph.Graph.community():

import igraph

g = igraph.Graph.Erdos_Renyi(n=100, p=0.1)
clusters = g.community_multilevel()
modularity_score = g.modularity(clusters.membership)

Upvotes: 1

Related Questions