从numpy总和2D数组创建加权igraph图作为邻接矩阵 [英] Create weighted igraph Graph from numpy summetric 2D array as adjacency matrix

查看:100
本文介绍了从numpy总和2D数组创建加权igraph图作为邻接矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 numpy 2D数组,其值表示节点之间边缘的权重.矩阵是对称的,我将对角线设为零.我找不到如何将该矩阵转换为igraph Graph对象的示例.我已经尝试了以下方法,但是不起作用:

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())

推荐答案

如果要将矩阵中的原始值保留为权重,请使用 Graph.Weighted_Adjacency().权重将作为 weight 边缘属性附加到igraph创建的图形上.

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.

这篇关于从numpy总和2D数组创建加权igraph图作为邻接矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆