vtraag / leidenalg

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

question: is it possible to retreive the node given the resulting membership? #116

Closed ivan-marroquin closed 1 year ago

ivan-marroquin commented 1 year ago

Hi,

I have this question. After I run a community detection using this command: partition= la.find_partition(G, la.RBERVertexPartition, weights= None, n_iterations= -1, seed= 1969, resolution_parameter= 4.25)

Then, I retrieve the membership of nodes by doing: grouping_assignments= np.asarray(partition.membership, dtype= np.int32)

Is there a way to retrieve the node id associated with a particular membership?

Thanks, Ivan

vtraag commented 1 year ago

Any VertexPartition in leidenalg derives from VertexClustering in igraph, so you can also find more information about it in that documentation.

You can treat the partition as a sort of list, where each element in the list refers to a cluster. That is, partition[0] will return the vertices that are part of cluster 0, etc. You can also iterate over all clusters, e.g. [len(c) for c in partition] will indicate the number of nodes in each cluster. Hence, if node is i is in cluster c then partition.membership[i] == c and i in partition[c].