to_scipy_sparse_array#

to_scipy_sparse_array(G, nodelist=None, dtype=None, weight='weight', format='csr')[源码]#

返回图的邻接矩阵,形式为 SciPy 稀疏数组。

参数:
G

用于构建稀疏数组的 NetworkX 图。

nodelist列表,可选

行和列按照 nodelist 中的节点顺序排列。如果 nodelist 是 None,则顺序由 G.nodes() 生成。

dtypeNumPy 数据类型,可选

用于初始化数组的有效 NumPy 数据类型。如果是 None,则使用 NumPy 的默认值。

weight字符串或 None,可选(默认值为 'weight')

存储用于边权重的数值的边属性。如果是 None,则所有边权重均为 1。

format字符串类型,取值范围为 {‘bsr’, ‘csr’, ‘csc’, ‘coo’, ‘lil’, ‘dia’, ‘dok’}

返回的稀疏数组的格式(默认为 ‘csr’)。对于某些算法,不同实现的稀疏数组可能表现更好。参见 [1] 了解详情。

返回值:
ASciPy 稀疏数组

图的邻接矩阵。

注意

对于有向图,矩阵项 i, j 对应于从 ij 的边。

邻接矩阵的值是使用参数 weight 中存储的边属性填充的。当边没有该属性时,该项的值为 1。

对于多重边,矩阵值为边权重的总和。

nodelist 不包含 G 中的所有节点时,邻接矩阵由 G 的子图构建,该子图由 nodelist 中的节点导出。

图中用于自环边的约定是将对角矩阵项的值赋给边的 weight 属性(如果边没有 weight 属性,则赋值为 1)。如果需要采用将边权重加倍的替代约定,可以按如下方式修改结果数组

>>> G = nx.Graph([(1, 1)])
>>> A = nx.to_scipy_sparse_array(G)
>>> A.toarray()
array([[1]])
>>> A.setdiag(A.diagonal() * 2)
>>> A.toarray()
array([[2]])

参考文献

[1]

Scipy 开发者参考,“稀疏数组”,https://docs.scipy.org.cn/doc/scipy/reference/sparse.html

示例

基本用法

>>> G = nx.path_graph(4)
>>> A = nx.to_scipy_sparse_array(G)
>>> A  
<Compressed Sparse Row sparse array of dtype 'int64'
    with 6 stored elements and shape (4, 4)>
>>> A.toarray()
array([[0, 1, 0, 0],
       [1, 0, 1, 0],
       [0, 1, 0, 1],
       [0, 0, 1, 0]])

注意

在这些示例中,toarray 方法用于更好地可视化邻接矩阵。对于邻接矩阵的密集表示,请改用 to_numpy_array

有向图

>>> G = nx.DiGraph([(0, 1), (1, 2), (2, 3)])
>>> nx.to_scipy_sparse_array(G).toarray()
array([[0, 1, 0, 0],
       [0, 0, 1, 0],
       [0, 0, 0, 1],
       [0, 0, 0, 0]])
>>> H = G.reverse()
>>> H.edges
OutEdgeView([(1, 0), (2, 1), (3, 2)])
>>> nx.to_scipy_sparse_array(H).toarray()
array([[0, 0, 0, 0],
       [1, 0, 0, 0],
       [0, 1, 0, 0],
       [0, 0, 1, 0]])

默认情况下,邻接矩阵的行/列顺序由 G 中的节点顺序决定。

>>> G = nx.Graph()
>>> G.add_nodes_from([3, 5, 0, 1])
>>> G.add_edges_from([(1, 3), (1, 5)])
>>> nx.to_scipy_sparse_array(G).toarray()
array([[0, 0, 0, 1],
       [0, 0, 0, 1],
       [0, 0, 0, 0],
       [1, 1, 0, 0]])

行的顺序可以通过 nodelist 更改。

>>> ordered = [0, 1, 3, 5]
>>> nx.to_scipy_sparse_array(G, nodelist=ordered).toarray()
array([[0, 0, 0, 0],
       [0, 0, 1, 1],
       [0, 1, 0, 0],
       [0, 1, 0, 0]])

如果 nodelist 包含 G 中的节点子集,则生成节点导出子图的邻接矩阵。

>>> nx.to_scipy_sparse_array(G, nodelist=[1, 3, 5]).toarray()
array([[0, 1, 1],
       [1, 0, 0],
       [1, 0, 0]])

邻接矩阵的值取自由 weight 参数指定的边属性。

>>> G = nx.path_graph(4)
>>> nx.set_edge_attributes(
...     G, values={(0, 1): 1, (1, 2): 10, (2, 3): 2}, name="weight"
... )
>>> nx.set_edge_attributes(
...     G, values={(0, 1): 50, (1, 2): 35, (2, 3): 10}, name="capacity"
... )
>>> nx.to_scipy_sparse_array(G).toarray()  # Default weight="weight"
array([[ 0,  1,  0,  0],
       [ 1,  0, 10,  0],
       [ 0, 10,  0,  2],
       [ 0,  0,  2,  0]])
>>> nx.to_scipy_sparse_array(G, weight="capacity").toarray()
array([[ 0, 50,  0,  0],
       [50,  0, 35,  0],
       [ 0, 35,  0, 10],
       [ 0,  0, 10,  0]])

任何没有 weight 属性的边,其默认值为 1。

>>> G[1][2].pop("capacity")
35
>>> nx.to_scipy_sparse_array(G, weight="capacity").toarray()
array([[ 0, 50,  0,  0],
       [50,  0,  1,  0],
       [ 0,  1,  0, 10],
       [ 0,  0, 10,  0]])

G 是多重图时,邻接矩阵中的值由每个边键的 weight 边属性的总和给出。

>>> G = nx.MultiDiGraph([(0, 1), (0, 1), (0, 1), (2, 0)])
>>> nx.to_scipy_sparse_array(G).toarray()
array([[0, 3, 0],
       [0, 0, 0],
       [1, 0, 0]])