Create weighted igraph Graph from numpy summetric 2D array as adjacency matrix

陌路散爱 提交于 2021-02-19 05:36:12

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!