本文整理汇总了Python中networkx.modularity_matrix方法的典型用法代码示例。如果您正苦于以下问题:Python networkx.modularity_matrix方法的具体用法?Python networkx.modularity_matrix怎么用?Python networkx.modularity_matrix使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类networkx
的用法示例。
在下文中一共展示了networkx.modularity_matrix方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_base_modularity_matrix
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import modularity_matrix [as 别名]
def get_base_modularity_matrix(network):
'''
Obtain the modularity matrix for the whole network
Parameters
----------
network : nx.Graph or nx.DiGraph
The network of interest
Returns
-------
np.matrix
The modularity matrix for `network`
Raises
------
TypeError
When the input `network` does not fit either nx.Graph or nx.DiGraph
'''
if type(network) == nx.Graph:
return sparse.csc_matrix(nx.modularity_matrix(network))
elif type(network) == nx.DiGraph:
return sparse.csc_matrix(nx.directed_modularity_matrix(network))
else:
raise TypeError('Graph type not supported. Use either nx.Graph or nx.Digraph')
示例2: modularity_spectrum
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import modularity_matrix [as 别名]
def modularity_spectrum(G):
"""Return eigenvalues of the modularity matrix of G.
Parameters
----------
G : Graph
A NetworkX Graph or DiGraph
Returns
-------
evals : NumPy array
Eigenvalues
See Also
--------
modularity_matrix
References
----------
.. [1] M. E. J. Newman, "Modularity and community structure in networks",
Proc. Natl. Acad. Sci. USA, vol. 103, pp. 8577-8582, 2006.
"""
from scipy.linalg import eigvals
if G.is_directed():
return eigvals(nx.directed_modularity_matrix(G))
else:
return eigvals(nx.modularity_matrix(G))
# fixture for nose tests
示例3: test_modularity
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import modularity_matrix [as 别名]
def test_modularity(self):
"Modularity matrix"
B = numpy.matrix([[-1.125, 0.25 , 0.25 , 0.625, 0. ],
[ 0.25 , -0.5 , 0.5 , -0.25 , 0. ],
[ 0.25 , 0.5 , -0.5 , -0.25 , 0. ],
[ 0.625, -0.25 , -0.25 , -0.125, 0. ],
[ 0. , 0. , 0. , 0. , 0. ]])
permutation = [4, 0, 1, 2, 3]
assert_equal(nx.modularity_matrix(self.G), B)
assert_equal(nx.modularity_matrix(self.G, nodelist=permutation),
B[numpy.ix_(permutation, permutation)])
示例4: modularity_spectrum
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import modularity_matrix [as 别名]
def modularity_spectrum(G):
"""Returns eigenvalues of the modularity matrix of G.
Parameters
----------
G : Graph
A NetworkX Graph or DiGraph
Returns
-------
evals : NumPy array
Eigenvalues
See Also
--------
modularity_matrix
References
----------
.. [1] M. E. J. Newman, "Modularity and community structure in networks",
Proc. Natl. Acad. Sci. USA, vol. 103, pp. 8577-8582, 2006.
"""
from scipy.linalg import eigvals
if G.is_directed():
return eigvals(nx.directed_modularity_matrix(G))
else:
return eigvals(nx.modularity_matrix(G))
# fixture for nose tests
示例5: test_modularity
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import modularity_matrix [as 别名]
def test_modularity(self):
"Modularity matrix"
B = numpy.matrix([[-1.125, 0.25, 0.25, 0.625, 0.],
[0.25, -0.5, 0.5, -0.25, 0.],
[0.25, 0.5, -0.5, -0.25, 0.],
[0.625, -0.25, -0.25, -0.125, 0.],
[0., 0., 0., 0., 0.]])
permutation = [4, 0, 1, 2, 3]
assert_equal(nx.modularity_matrix(self.G), B)
assert_equal(nx.modularity_matrix(self.G, nodelist=permutation),
B[numpy.ix_(permutation, permutation)])
示例6: test_modularity_weight
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import modularity_matrix [as 别名]
def test_modularity_weight(self):
"Modularity matrix with weights"
B = numpy.matrix([[-1.125, 0.25, 0.25, 0.625, 0.],
[0.25, -0.5, 0.5, -0.25, 0.],
[0.25, 0.5, -0.5, -0.25, 0.],
[0.625, -0.25, -0.25, -0.125, 0.],
[0., 0., 0., 0., 0.]])
G_weighted = self.G.copy()
for n1, n2 in G_weighted.edges():
G_weighted.edges[n1, n2]["weight"] = 0.5
# The following test would fail in networkx 1.1
assert_equal(nx.modularity_matrix(G_weighted), B)
# The following test that the modularity matrix get rescaled accordingly
assert_equal(nx.modularity_matrix(G_weighted, weight="weight"), 0.5 * B)
示例7: get_base_modularity_matrix
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import modularity_matrix [as 别名]
def get_base_modularity_matrix(network):
'''
Obtain the modularity matrix for the whole network
Parameters
----------
network : nx.Graph or nx.DiGraph
The network of interest
Returns
-------
np.matrix
The modularity matrix for `network`
Raises
------
TypeError
When the input `network` does not fit either nx.Graph or nx.DiGraph
'''
if type(network) == nx.Graph:
return sparse.csc_matrix(nx.modularity_matrix(network))
elif type(network) == nx.DiGraph:
return sparse.csc_matrix(nx.directed_modularity_matrix(network))
else:
raise TypeError('Graph type not supported. Use either nx.Graph or nx.Digraph')
示例8: modularity_matrix
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import modularity_matrix [as 别名]
def modularity_matrix(G, nodelist=None):
"""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/m
where k_i(in) is the degree of node i, and were m is the number of edges
in the graph.
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().
Returns
-------
B : Numpy matrix
The modularity matrix of G.
Examples
--------
>>> import networkx as nx
>>> k =[3, 2, 2, 1, 0]
>>> G = nx.havel_hakimi_graph(k)
>>> B = nx.modularity_matrix(G)
See Also
--------
to_numpy_matrix
adjacency_matrix
laplacian_matrix
directed_modularity_matrix
References
----------
.. [1] M. E. J. Newman, "Modularity and community structure in networks",
Proc. Natl. Acad. Sci. USA, vol. 103, pp. 8577-8582, 2006.
"""
if nodelist is None:
nodelist = G.nodes()
A = nx.to_scipy_sparse_matrix(G, nodelist=nodelist, format='csr')
k = A.sum(axis=1)
m = G.number_of_edges()
# Expected adjacency matrix
X = k * k.transpose() / (2 * m)
return A - X
示例9: modularity_matrix
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import modularity_matrix [as 别名]
def modularity_matrix(G, nodelist=None, weight=None):
"""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.
Examples
--------
>>> import networkx as nx
>>> k =[3, 2, 2, 1, 0]
>>> G = nx.havel_hakimi_graph(k)
>>> B = nx.modularity_matrix(G)
See Also
--------
to_numpy_matrix
adjacency_matrix
laplacian_matrix
directed_modularity_matrix
References
----------
.. [1] M. E. J. Newman, "Modularity and community structure in networks",
Proc. Natl. Acad. Sci. USA, vol. 103, pp. 8577-8582, 2006.
"""
if nodelist is None:
nodelist = list(G)
A = nx.to_scipy_sparse_matrix(G, nodelist=nodelist, weight=weight,
format='csr')
k = A.sum(axis=1)
m = k.sum() * 0.5
# Expected adjacency matrix
X = k * k.transpose() / (2 * m)
return A - X