Skip to content

Clustering, density estimation, and anomaly detection · Graph and community clustering · UCL030 · Algorithm

Louvain community detection

Detect graph communities by greedy local improvement followed by aggregation of communities into supernodes. It is fast and widely used when dense internal connection relative to a degree-based null model defines a…

Runs in CodeSplash (Experimental) Component demo, not a trained model CodeSplash's run and the reference's record

In CodeSplash

A CodeSplash method runs this entry in Python and R. Set it up from the method's page. The methods that run it are Experimental.

Do it in CodeSplash Run it on your own data. It's in the Experimental lane in the app.

Availability
Runs in CodeSplash (Experimental)
Code example
Component demo, not a trained model
Who ran it
CodeSplash's run and the reference's record
CodeSplash's run
Verified in Python and R through discover.communities. The reference's example ran in Python and R here, and they agree.
The reference's record
Python: Ran in the reference's review. The report doesn't record which version of the code ran. R: Reviewed but not run: R wasn't available to the reference's authors.

Purpose

Detect graph communities by greedy local improvement followed by aggregation of communities into supernodes. It is fast and widely used when dense internal connection relative to a degree-based null model defines a community.

Question / hypotheses

Which partition or latent block structure best explains the supplied graph relationships? This entry defines an estimation or optimization objective, not an intrinsic null/alternative hypothesis test.

Architecture / computational structure

Local modularity moves followed by graph aggregation into supernodes.

Assumptions

Modularity has a resolution limit and multiple near-optimal solutions; a higher value does not establish a scientifically true community structure. Louvain can produce poorly connected communities, motivating Leiden.

Formula / computational definition

For an undirected graph, modularity is Q=(2m)−1∑ij[Aij−γkikj/(2m)]1(zi=zj)Q=(2m)^{-1}\sum_{ij}[A_{ij}-\gamma k_ik_j/(2m)]\mathbf1(z_i=z_j), where mm is total edge weight, kik_i weighted degree, and γ\gamma resolution. Local moves and aggregation seek a high-modularity partition.

Parameters and notation

Learned or fitted quantities: A community partition; modularity is its evaluation statistic. User-set hyperparameters/design choices: Set resolution, weights, and randomization. Tune these against the stated objective and stability/validation criteria; do not select settings on the final evaluation set.

Fitting, fine-tuning, and adaptation

Neural fine-tuning is not applicable. Refit after material edge or weight changes and compare several initializations. Warm-starting a partition can help computation but may preserve an obsolete solution; use the same graph construction when comparing versions.

How to use and calibrate

Report modularity alongside within-community connectivity, group-size distributions, stability over seeds, and sensitivity to resolution. Compare partitions at matched resolution conventions.

Example application

Find densely connected collaboration communities. Define the reference population, measurement units, and success criterion before fitting; investigate whether discovered groups or alerts reflect the intended phenomenon.

Interpretation / pitfalls

Watch out

The reported result is conditional on the representation and objective. A stable partition can reflect nuisance structure such as batch, collection site, or missingness. Confirm the interpretation in original variables and an independent sample.

Python and R code examples

R offers igraph::cluster_louvain; Python can bridge to that implementation or use an independently verified equivalent. The displayed modularity calculation is portable NumPy/base R and evaluates a supplied partition, not the whole optimization algorithm.

Component: Modularity of a supplied partition. Python: executed successfully; R: not executed (Rscript unavailable). These small fixtures establish the arithmetic operation, not full-method accuracy or reproduction of paper results.

Python and R

The reference's own programs, shown as written. A component demo shows one central piece of the method; it doesn't train a full model.

Python

Component demo, not a trained model — Modularity of a supplied partition. · needs numpy

import numpy as np
A=np.array([[0.,1.,.1],[1.,0.,.1],[.1,.1,0.]])
z=np.array([0,0,1]); k=A.sum(1); total=k.sum(); gamma=1.
result=np.sum((A-gamma*np.outer(k,k)/total)*(z[:,None]==z))/total
print(result)
R

Component demo, not a trained model — Modularity of a supplied partition.

A=rbind(c(0,1,.1),c(1,0,.1),c(.1,.1,0)); z=c(1,1,2); k=rowSums(A); total=sum(k)
result=sum((A-outer(k,k)/total)*outer(z,z,'=='))/total
print(result)

Sources

From the CodeSplash reference, entry UCL030.