【问题标题】:How to convert numpy array adjacency list into numpy array adjacency matrix?如何将 numpy 数组邻接列表转换为 numpy 数组邻接矩阵?
【发布时间】:2026-02-12 05:05:01
【问题描述】:

我在 numpy 数组中有一个下表。

现在我想将其转换为以行为源、列为目标、值为权重的邻接矩阵。任何人都可以给我任何我可以利用的参考吗?

【问题讨论】:

    标签: numpy adjacency-matrix adjacency-list


    【解决方案1】:

    您的数据或多或少采用coo 格式,因此请使用scipy.sparse.coo_matrix 构造函数。生成的稀疏矩阵可以转换为多种格式。

    例子:

    >>> from pprint import pprint
    >>> import numpy as np
    >>> from scipy import sparse
    >>> 
    # create example
    >>> a = np.random.randint(-10, 4, (10, 10)).clip(0, None) * 0.24
    >>> a
    array([[0.  , 0.  , 0.  , 0.  , 0.  , 0.  , 0.  , 0.  , 0.  , 0.  ],
           [0.  , 0.  , 0.24, 0.  , 0.  , 0.  , 0.  , 0.  , 0.  , 0.  ],
           [0.72, 0.24, 0.  , 0.  , 0.  , 0.  , 0.  , 0.  , 0.  , 0.  ],
           [0.  , 0.  , 0.  , 0.  , 0.  , 0.24, 0.  , 0.  , 0.  , 0.  ],
           [0.  , 0.  , 0.24, 0.  , 0.72, 0.  , 0.48, 0.  , 0.  , 0.  ],
           [0.  , 0.  , 0.  , 0.  , 0.  , 0.  , 0.  , 0.48, 0.24, 0.  ],
           [0.  , 0.  , 0.  , 0.48, 0.48, 0.  , 0.  , 0.24, 0.  , 0.  ],
           [0.  , 0.  , 0.  , 0.  , 0.  , 0.24, 0.48, 0.  , 0.  , 0.24],
           [0.  , 0.48, 0.  , 0.  , 0.  , 0.  , 0.72, 0.  , 0.  , 0.72],
           [0.  , 0.  , 0.  , 0.  , 0.  , 0.  , 0.  , 0.  , 0.  , 0.  ]])
    >>>
    # construct coo representation
    >>> row, col = np.where(a)
    >>> coo = np.rec.fromarrays([row, col, a[row, col]], names='row col value'.split())
    >>> pprint(coo.tolist())
    [(1, 2, 0.24),
     (2, 0, 0.72),
     (2, 1, 0.24),
     (3, 5, 0.24),
     (4, 2, 0.24),
     (4, 4, 0.72),
     (4, 6, 0.48),
     (5, 7, 0.48),
     (5, 8, 0.24),
     (6, 3, 0.48),
     (6, 4, 0.48),
     (6, 7, 0.24),
     (7, 5, 0.24),
     (7, 6, 0.48),
     (7, 9, 0.24),
     (8, 1, 0.48),
     (8, 6, 0.72),
     (8, 9, 0.72)]
    >>>
    # create sparse matrix
    >>> out = sparse.coo_matrix((coo['value'], (coo['row'], coo['col'])), (10, 10))
    >>> out
    <10x10 sparse matrix of type '<class 'numpy.float64'>'
            with 18 stored elements in COOrdinate format>
    >>> 
    # convert back to dense format
    >>> out.A
    array([[0.  , 0.  , 0.  , 0.  , 0.  , 0.  , 0.  , 0.  , 0.  , 0.  ],
           [0.  , 0.  , 0.24, 0.  , 0.  , 0.  , 0.  , 0.  , 0.  , 0.  ],
           [0.72, 0.24, 0.  , 0.  , 0.  , 0.  , 0.  , 0.  , 0.  , 0.  ],
           [0.  , 0.  , 0.  , 0.  , 0.  , 0.24, 0.  , 0.  , 0.  , 0.  ],
           [0.  , 0.  , 0.24, 0.  , 0.72, 0.  , 0.48, 0.  , 0.  , 0.  ],
           [0.  , 0.  , 0.  , 0.  , 0.  , 0.  , 0.  , 0.48, 0.24, 0.  ],
           [0.  , 0.  , 0.  , 0.48, 0.48, 0.  , 0.  , 0.24, 0.  , 0.  ],
           [0.  , 0.  , 0.  , 0.  , 0.  , 0.24, 0.48, 0.  , 0.  , 0.24],
           [0.  , 0.48, 0.  , 0.  , 0.  , 0.  , 0.72, 0.  , 0.  , 0.72],
           [0.  , 0.  , 0.  , 0.  , 0.  , 0.  , 0.  , 0.  , 0.  , 0.  ]])
    

    【讨论】: