networkx.linalg.modularity_matrix¶
-
networkx.linalg.modularity_matrix(G, nodelist=None, weight=None)[source]¶ Return the modularity matrix of G.
The modularity matrix is the matrix B = A - <A>, where A is the adjacency matrix and <A> is the average adjacency matrix, assuming that the graph is described by the configuration model.
- More specifically, the element B_ij of B is defined as
- A_ij - k_i k_j / 2 * m
where k_i(in) is the degree of node i, and were m is the number of edges in the graph. When weight is set to a name of an attribute edge, Aij, k_i, k_j and m are computed using its value.
Parameters: G : Graph
A NetworkX graph
nodelist : list, optional
The rows and columns are ordered according to the nodes in nodelist. If nodelist is None, then the ordering is produced by G.nodes().
weight : string or None, optional (default=None)
The edge attribute that holds the numerical value used for the edge weight. If None then all edge weights are 1.
Returns: B : Numpy matrix
The modularity matrix of G.
See also
to_numpy_matrix,adjacency_matrix,laplacian_matrix,directed_modularity_matrixReferences
[R1182] M. E. J. Newman, “Modularity and community structure in networks”, Proc. Natl. Acad. Sci. USA, vol. 103, pp. 8577-8582, 2006. Examples
>>> import networkx as nx >>> k =[3, 2, 2, 1, 0] >>> G = nx.havel_hakimi_graph(k) >>> B = nx.modularity_matrix(G)