vtraag / leidenalg

Implementation of the Leiden algorithm for various quality functions to be used with igraph in Python.
GNU General Public License v3.0
566 stars 76 forks source link

Modularity (Q value) for each cluster #169

Closed sgautam666 closed 4 months ago

sgautam666 commented 4 months ago

It seems like the current partition method can return modularity for the cluster result. Is it possible to get the Q-value to each individual clusters?

vtraag commented 4 months ago

No, no such values per community are returned. But you can easily calculate this yourself using standard python and functionality of igraph. If you are not able to figure it out, let me know.

sgautam666 commented 4 months ago

It would be nice if you have an example already that you can share. Thank you

vtraag commented 4 months ago

Sure, here's an example of how to calculate some statistics per cluster

#%% Create graph

G = ig.Graph.Famous('Zachary')

#%% Run clustering

resolution = 0.05
part = la.find_partition(G, la.CPMVertexPartition, 
                         resolution_parameter=resolution)

#%% Calculate statistics per cluster

Qc = []
for H in part.subgraphs():
    ec = H.ecount()
    nc = H.vcount()
    Qc.append(2*ec - resolution*nc*(nc - 1))
    print(e, nc, Qc)

print(sum(Qc), part.quality())