【问题标题】:Create weighted igraph Graph from numpy summetric 2D array as adjacency matrix从 numpy summetric 2D array 作为邻接矩阵创建加权 igraph Graph
【发布时间】:2016-08-05 13:56:26
【问题描述】:

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

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

【问题讨论】:

    标签: python arrays numpy matrix igraph


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      截至0.9.6版本,Weighted_Adjacency可以接收

      @param matrix: the adjacency matrix. Possible types are:
        - a list of lists
        - a numpy 2D array or matrix (will be converted to list of lists)
        - a scipy.sparse matrix (will be converted to a COO matrix, but not
          to a dense matrix)
      

      无需转换为list

      让我们为多个时间切片扩展可能的用例场景,比如 5

      from simeeg import rand_tril_arr as rt # pip install simeeg
      import leidenalg as la
      import igraph as ig
      from string import ascii_uppercase
          
      nsize=5
      all_arr=[rt ( nsize=nsize, overwite_val=True, kmax=4, val_rand=0 ) for _ in range (5)]
      nlabel=list(ascii_uppercase)[:nsize]
      all_G=[]
      for arr in all_arr:
          G = ig.Graph.Weighted_Adjacency ( arr)
          G.vs ['name'] = nlabel
          all_G.append(G)
      
      G_layers, G_interslice, G = la.time_slices_to_layers(all_G, interslice_weight=1e-1,slice_attr='slice',
                                                           vertex_id_attr='name',edge_type_attr='type',
                                                           weight_attr='weight')
      
      ig.plot(G,
              vertex_label = [f'{v["name"]}-{v["slice"]}' for v in G.vs])
      

      生产者:

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-10-19
        • 2018-07-03
        • 1970-01-01
        相关资源
        最近更新 更多