问题
I am having a numpy
2D array, with the values representing the weights of edges between nodes. The matrix is symmetric, and I take the diagonal to be zero. I don't find an example of how to convert this matrix into igraph Graph object. I've tried the following approach, but it doesn't work:
import numpy as np
import igraph
def symmetrize(a):
return a + a.T - 2*np.diag(a.diagonal())
A = symmetrize(np.random.random((100,100)))
G = igraph.Graph.Adjacency(A.tolist())
回答1:
Use Graph.Weighted_Adjacency()
if you want to preserve the original values in the matrix as weights. The weights will be attached as the weight
edge attribute to the graph that igraph creates.
来源:https://stackoverflow.com/questions/36615878/create-weighted-igraph-graph-from-numpy-summetric-2d-array-as-adjacency-matrix