graphy.louvain - Module for running Louvain community detection algorithm

Module for running Louvain community detection algorithm.

graphy.louvain.optimize_modularity(conn_mx, rand_init=True, num_runs=1, debug=False, errortol=0.01)[source]

Optimize directed, weighted Newman’s modularity using the Louvain algorithm. This uses C++ implementation from https://github.com/raldecoa/SurpriseMe/tree/master/src/CPM

For example:

>>> import networkx as nx
>>> from graphy.louvain import optimize_modularity
>>> G = nx.karate_club_graph()
>>> best_membership, q = optimize_modularity(nx.to_numpy_array(G))
>>> print(best_membership, q)   
Parameters:
conn_mx2-dimensional np.array or scipy.sparse matrix

Connectivity matrix.

rand_initbool (default True)

Whether to randomly shuffle order of nodes (makes results non-deterministic)

num_runsint (default 1)

How many runs to perform (highest quality run returned). Only allow if rand_init is True.

debugbool (default False)

If True, prints various debugging information.

Returns:
np.array

Optimal membership vector indicating community membership of each node.

float

Modularity value corresponding to the optimal membership vector. Notice that because the modularity value is computed by adding up increments over many moves, this may only be accurate to a few decimal places.