如何将numpy数组邻接列表转换为numpy数组邻接矩阵?

时间:2018-10-23 23:35:51

标签: numpy adjacency-matrix adjacency-list

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

enter image description here

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

1 个答案:

答案 0 :(得分:0)

您的数据或多或少为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.  ]])