mojolearn.Louvain#

class mojolearn.Louvain(resolution=1.0, *, threshold=1e-07, max_level=None, seed=None)[source]#

Bases: _XNeighbors

Louvain community detection on an undirected weighted graph.

Finds communities by greedy modularity optimization, following networkx’s louvain_communities. Local moving runs in parallel in a fixed order (the nodes of one color of a fixed graph coloring move together, ties go to the lowest community id) instead of networkx’s random shuffle, so the result is reproducible and does not depend on seed; it can differ from networkx’s partition. The graph is a symmetric weighted adjacency matrix given as a dense square array, a scipy.sparse matrix (any format; indices sorted within each row), or an (indptr, indices, n) CSR tuple in which every edge weighs 1. A non-symmetric matrix or a graph without edges raises ValueError.

Parameters:
  • resolution (float, default 1.0) – Resolution of the modularity; larger values favor smaller communities.

  • threshold (float, default 1e-7) – Minimum modularity gain for another level.

  • max_level (int or None, default None) – Maximum number of levels (a positive integer); None means no limit.

  • seed (int or None, default None) – Accepted for networkx compatibility; unused.

  • numeric_mode ({‘identical’, ‘deterministic’, ‘fast’} or None, default None) – Numeric mode for this estimator ('fast' is Apple only). None uses the process default.

Variables:
  • labels_ (Array of shape (n,), int32) – Community of each node, numbered in order of each community’s lowest node.

  • n_communities_ (int) – Number of communities.

  • modularity_ (float) – Modularity of the partition (computed in float32).

  • n_levels_ (int) – Number of levels run.

Examples

>>> import numpy as np, mojolearn
>>> M = np.random.default_rng(0).random((500, 500)) < 0.02
>>> A = (M | M.T).astype(np.float32)
>>> labels = mojolearn.Louvain().fit_predict(A)
__init__(resolution=1.0, *, threshold=1e-07, max_level=None, seed=None)[source]#

Methods

fit(A[, y])

Detect communities in the graph.

fit_predict(A[, y])

Detect communities and return labels_.

get_params([deep])

numeric_mode_used()

The numeric mode this estimator runs in.

set_params(**params)

Set constructor parameters.

vendor_used()

The GPU backend this estimator runs on.

fit(A, y=None)[source]#

Detect communities in the graph.

Parameters:
  • A (array-like of shape (n, n), scipy.sparse matrix, or tuple) – Symmetric weighted adjacency matrix: a dense square array, a scipy.sparse matrix, or an (indptr, indices, n) CSR tuple.

  • y (None) – Ignored.

Returns:

self (Louvain) – The fitted estimator.

Raises:

ValueError – If max_level is not a positive integer or None, the matrix is not symmetric, or the graph has no edges.

fit_predict(A, y=None)[source]#

Detect communities and return labels_.

Parameters:
  • A (array-like of shape (n, n), scipy.sparse matrix, or tuple) – Symmetric weighted adjacency matrix.

  • y (None) – Ignored.

Returns:

labels (Array of shape (n,), int32) – Community of each node.