使用特征和邻接矩阵的 numpy 表示构建 networkx/dgl 图

Build networkx/dgl graph with from numpy representations of feature and adjacency matrix

描述

从邻接矩阵生成图形对象(DGL 或 NetworkX)并允许建立节点特征。

结果

我在下面生成了我的解决方案。但是,鼓励其他答案。

代码

import numpy as np
import dgl
import networkx as nx

def numpy_to_graph(A,type_graph='dgl',node_features=None):
    '''Convert numpy arrays to graph

    Parameters
    ----------
    A : mxm array
        Adjacency matrix
    type_graph : str
        'dgl' or 'nx'
    node_features : dict
        Optional, dictionary with key=feature name, value=list of size m
        Allows user to specify node features

    Returns

    -------
    Graph of 'type_graph' specification
    '''
    
    G = nx.from_numpy_array(A)
    
    if node_features != None:
        for n in G.nodes():
            for k,v in node_features.items():
                G.nodes[n][k] = v[n]
    
    if type_graph == 'nx':
        return G
    
    G = G.to_directed()
    
    if node_features != None:
        node_attrs = list(node_features.keys())
    else:
        node_attrs = []
        
    g = dgl.from_networkx(G, node_attrs=node_attrs, edge_attrs=['weight'])
    return g

例子

邻接矩阵被传递给函数。此外,其他特征(即特征向量、标签等​​)可以传入node_features

# mxm adjacency matrix
A = np.array([[0,0,0],
              [2,0,0],
              [5,1,0]])

# Each m row is a feature vector for node m
F = np.array([[1,0,1,4,4],
             [2,4,0,12,4],
             [5,1,-4,2,9]])


G = numpy_to_graph(A,type_graph='nx',node_features={'feat':F})

import matplotlib.pyplot as plt
pos=nx.spring_layout(G) # pos = nx.nx_agraph.graphviz_layout(G)
nx.draw_networkx(G,pos)
labels = nx.get_edge_attributes(G,'weight')
nx.draw_networkx_edge_labels(G,pos,edge_labels=labels)
plt.show()